Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a rigidbody that is on top of a plane or another rigidbody. How do I check if said rigidbody is on top of it?

I tried to check the Y-part of the velocity but it is never 0, it is always a small value.

What would be the best way to detect this?

share|improve this question
"it is always a small value" Could it be as simple as checking to see if the value is less than .5? – TheNickmaster21 Sep 22 '13 at 2:53
up vote 0 down vote accepted

You could do a ray test straight down from the rigid body and test the distance of the ray hit.

One way is to simply use the rigid body position and subtract 10,000 or some arbitrary number to get a destination for the ray test.

btVector3 btFrom(pos.x, pos.y, pos.z);
btVector3 btTo(pos.x, -10000.0, pos.z);
btCollisionWorld::ClosestRayResultCallback res(btFrom, btTo);

btWorld->rayTest(btFrom, btTo, res); // m_btWorld is btDiscreteDynamicsWorld

    /* Use Ogre::Vector3::squaredDistance() to find the difference from the rigid body to the ray hit */
share|improve this answer
rayTest isn't detecting a rigidbody, just a plane. Do you know why? – Pacha Jun 24 '13 at 0:10
Are you setting the mask and group when you add collision objects and rigid bodies? For rigid bodies, I leave them as default, and anything else uses group 2 and a mask of 1, like so: btWorld->addCollisionObject(obj, 2, 1); – unixunited Jun 24 '13 at 2:19
If you use this method I would suggest a much smaller "to" vector. Maybe something like -1, otherwise you will have to call the distance function much more than necessary (although it probably doesn't consume much CPU time). You could however use a larger vector if you wanted to do something like play an animation in preparation for the rigidbody/ground collision (like if you needed a character to brace themselves for impact). – Benjamin Danger Johnson Jul 24 '13 at 21:45
I'm not sure how Ogre works, but you might only be getting the first collision of the ray rather than all of them. If that is the case you could be getting a hit on the collider attached to the rigid body since it is closer than the ground. – Benjamin Danger Johnson Jul 24 '13 at 21:46
btCollisionWorld::ClosestRayResultCallback retrieves the first collision in the physics world. If you want to get all of the collisions from a ray, you can subclass RayResultCallback and store a list of collisions. In my case the player's rigid body isn't included in the ray test because it is added without a group and mask (kind of odd). – unixunited Aug 5 '13 at 20:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.