This project has moved and is read-only. For the latest updates, please go here.

Collision Detection With Bodies

Topics: Developer Forum
Mar 4, 2013 at 5:11 PM
So basically i have a physics entity class for my game i'm making. I want to have a function that returns true/false depending on whether the object is on the ground.

My question is, how can i go about determining if the object is resting on the ground? I assume i'll need to be doing something with Body.ContactList. If there is a method for determining if an object is on the ground, then would it be efficient enough to call every update?
Mar 4, 2013 at 7:46 PM
Hi twoski,

I had to solve the same problem. I found some tutorials on Box2D that suggest adding contact/separation event handlers and manually keeping a counter (add one when contact handler is called, subtract one when separation handler is called -- you're in the air if the count is zero). However, I wasn't able to get this solution to work for me (the contact/separation counts were not consistent). Here's what I was able to get work:
  1. Add a rectangle body to the bottom of your object. Set the IsSensor property on this body to true and attach it so it passes through the ground to some degree. You want a separate sensor body for this because it will fix problems where you're object is skipping on the ground or otherwise not really in the air, but not really contacting the ground either.
  2. Each update you can walk the contactList of your sensor body. If any of the contacts return true for IsTouching(), then your body is on the ground (or at least not in the air).
Performance-wise this solution worked fine for my game -- I didn't notice any performance impact.

Hope that helps. I can add code if you need it.

Mar 4, 2013 at 11:16 PM
Thanks for the help! I've implemented something like what you suggested. If i attach a rectangle with the farseer FixtureFactory then set the rectangle to sensor, my physics object falls through the world. Otherwise, it works fine... Is there something i should be doing? I can post code if needed.
Mar 5, 2013 at 2:58 AM
How are you attaching the rectangle? Which physics object falls through the world? Setting IsSensor prevents the body from reacting to collisions with other bodies in the world, but you still get the collision events and contacts. To be clearer, lets call the object you want to determine whether or not is in the air your character. Let's call the rectangle sensor the "sensor". You attach the rectangle sensor with a joint (weld or revolute joint should work fine depending on the behavior you want) to the bottom of your character so that when your character is standing on the ground, the sensor is passing through the ground. Then you can check the contactlist on your sensor with each update. If none of the contacts return true for IsTouching, your character is in the air.

Hope that helps,
Mar 5, 2013 at 10:10 PM
Ah i did a little more research and it was an issue with parenting bodies to non-rotating bodies.

Here's a little video of my results, cheers!