Howto detect collision Single Ray with single CollisionShape
Posted: Thu Jun 18, 2009 12:58 pm
I'm trying to detect collision - single ray with single CollisionShape
I took this from Raytracer demo
btVector3 worldNormal(0,0,0);
btVector3 worldPoint(0,0,0);
btVector3 aabbMin,aabbMax;
bool DynamicPhysicObject::singleObjectRaytest(const btVector3& rayFrom,const btVector3& rayTo,btVector3& worldNormal,btVector3& worldHitPoint)
{
btScalar closestHitResults = 1.f;
btCollisionWorld::ClosestRayResultCallback resultCallback(rayFrom,rayTo);
hasHit = false;
btConvexCast::CastResult rayResult;
//btSphereShape pointShape(0.0f);
btTransform rayFromTrans;
btTransform rayToTrans;
rayFromTrans.setIdentity();
rayFromTrans.setOrigin(rayFrom);
rayToTrans.setIdentity();
rayToTrans.setOrigin(rayTo);
//for (int s=0;s<3;s++)
//comment-out next line to get all hits, instead of just the closest hit
//resultCallback.m_closestHitFraction = 1.f;
//do some culling, ray versus aabb
ObjectShape->getAabb( ObjectTransform,aabbMin,aabbMax);
btScalar hitLambda = 1.f;
btVector3 hitNormal;
btCollisionObject tmpObj;
tmpObj.setWorldTransform( ObjectTransform);
if (btRayAabb(rayFrom,rayTo,aabbMin,aabbMax,hitLambda,hitNormal))
{
//reset previous result
btCollisionWorld::rayTestSingle(rayFromTrans,rayToTrans, &tmpObj, ObjectShape, ObjectTransform, resultCallback);
if (resultCallback.hasHit())
{
//float fog = 1.f - 0.1f * rayResult.m_fraction;
resultCallback.m_hitNormalWorld.normalize();//.m_normal.normalize();
worldNormal = resultCallback.m_hitNormalWorld;
//worldNormal = transforms[s].getBasis() *rayResult.m_normal;
worldNormal.normalize();
hasHit = true;
}
}
return hasHit;
}
and function call
...
hasHit = false;
hasHit = Object->singleObjectRaytest(BtOgre::Convert::toBullet(Vector3(0,10,0)),BtOgre::Convert::toBullet(Vector3(50,20,0)),worldNormal,worldPoint);
if (hasHit)
{LogManager::getSingletonPtr()->logMessage("*** hasHit ***");}
break;
But it doesn't work, the function always return true even the shape not in 50,20,0, what i do wrong ?
I took this from Raytracer demo
btVector3 worldNormal(0,0,0);
btVector3 worldPoint(0,0,0);
btVector3 aabbMin,aabbMax;
bool DynamicPhysicObject::singleObjectRaytest(const btVector3& rayFrom,const btVector3& rayTo,btVector3& worldNormal,btVector3& worldHitPoint)
{
btScalar closestHitResults = 1.f;
btCollisionWorld::ClosestRayResultCallback resultCallback(rayFrom,rayTo);
hasHit = false;
btConvexCast::CastResult rayResult;
//btSphereShape pointShape(0.0f);
btTransform rayFromTrans;
btTransform rayToTrans;
rayFromTrans.setIdentity();
rayFromTrans.setOrigin(rayFrom);
rayToTrans.setIdentity();
rayToTrans.setOrigin(rayTo);
//for (int s=0;s<3;s++)
//comment-out next line to get all hits, instead of just the closest hit
//resultCallback.m_closestHitFraction = 1.f;
//do some culling, ray versus aabb
ObjectShape->getAabb( ObjectTransform,aabbMin,aabbMax);
btScalar hitLambda = 1.f;
btVector3 hitNormal;
btCollisionObject tmpObj;
tmpObj.setWorldTransform( ObjectTransform);
if (btRayAabb(rayFrom,rayTo,aabbMin,aabbMax,hitLambda,hitNormal))
{
//reset previous result
btCollisionWorld::rayTestSingle(rayFromTrans,rayToTrans, &tmpObj, ObjectShape, ObjectTransform, resultCallback);
if (resultCallback.hasHit())
{
//float fog = 1.f - 0.1f * rayResult.m_fraction;
resultCallback.m_hitNormalWorld.normalize();//.m_normal.normalize();
worldNormal = resultCallback.m_hitNormalWorld;
//worldNormal = transforms[s].getBasis() *rayResult.m_normal;
worldNormal.normalize();
hasHit = true;
}
}
return hasHit;
}
and function call
...
hasHit = false;
hasHit = Object->singleObjectRaytest(BtOgre::Convert::toBullet(Vector3(0,10,0)),BtOgre::Convert::toBullet(Vector3(50,20,0)),worldNormal,worldPoint);
if (hasHit)
{LogManager::getSingletonPtr()->logMessage("*** hasHit ***");}
break;
But it doesn't work, the function always return true even the shape not in 50,20,0, what i do wrong ?