## Howto detect collision Single Ray with single CollisionShape

MusgooD
Posts: 8
Joined: Wed Jun 17, 2009 7:52 am

### Howto detect collision Single Ray with single CollisionShape

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 ?
MusgooD
Posts: 8
Joined: Wed Jun 17, 2009 7:52 am

### Re: Howto detect collision Single Ray with single CollisionShape

Erwin Coumans
Posts: 4236
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

### Re: Howto detect collision Single Ray with single CollisionShape

Some variables might not be (properly) initialized.

Does the Demos\Raytracer demo work fine? If so, please compare it against your implementation.
Thanks,
Erwin
MusgooD
Posts: 8
Joined: Wed Jun 17, 2009 7:52 am

### Re: Howto detect collision Single Ray with single CollisionShape

Thanks for help and great Engine Erwin !

I found the issue. It seems like ObjectTransform is not update.

The Ray collides only with initial position thats sets when i great the rigibody:

ObjectTransform.setIdentity();
ObjectTransform.setOrigin(BtOgre::Convert::toBullet(StartPosition));

When object moves from init position the function still return true.

The function return false if rayTo coordinates set obviously wrong.
________________________

Any idea to fix this problem ?
MusgooD
Posts: 8
Joined: Wed Jun 17, 2009 7:52 am

### Re: Howto detect collision Single Ray with single CollisionShape

"The information we get from the raycast includes what shape we hit, how far away the shape is from the origin of the ray, the point of impact of the ray, the normal to the impact on the shape, and, if the shape hit is a triangle mesh, the surface triangle hit and the barycentric coordinates of the point of impact."

Could somebody explain how it works in Bullet or create demo with features from above ?
pico
Posts: 229
Joined: Sun Sep 30, 2007 7:58 am

### Re: Howto detect collision Single Ray with single CollisionShape

MusgooD wrote:"The information we get from the raycast includes what shape we hit, how far away the shape is from the origin of the ray, the point of impact of the ray, the normal to the impact on the shape, and, if the shape hit is a triangle mesh, the surface triangle hit and the barycentric coordinates of the point of impact."

Could somebody explain how it works in Bullet or create demo with features from above ?
Hi,

do you mean how to get that information? If yes, in the "RayResultCallback" you have public members that give you that information:

m_closestHitFraction // how far from the origin the ray has hit. 0 means at origin 1.0 means not at all
m_collisionObject // the collision object the ray have hit
m_hitNormalWorld // the normal of the collisionObject where the ray has hit
m_hitPointWorld // hit point in world space

The remaining information can be gathered from the LocalRayResult struct that is passed in RayResultCallback::addSingleResult
MusgooD
Posts: 8
Joined: Wed Jun 17, 2009 7:52 am

### Re: Howto detect collision Single Ray with single CollisionShape

Thanks for help, pico

It was my fault. But finally it's worked out