Get the Body beneath my Vehicle?

Topics: Developer Forum
Nov 19, 2012 at 1:27 PM

I'm making a game with a flying Pod that needs to land on the ground and digg. How can i get the body or bodies beneath my Pod and how do i see if it has landed and not just bounching on it?

This should be simple right? 

Coordinator
Nov 19, 2012 at 3:52 PM

Once a body collides with another body, a persistant contact is created between the two. For you, this means that the OnCollision delegate is called when your body collides with something, and it only gets called once.

There is another delegate that gets called when the bodies separate: OnSeparation

In your game, I would recommend you set a boolean when there is a contact created on the Pod. Like this:

 

private bool OnCollisionWithGround(....)
{
  onGround = true;
  return true;
}

private void OnSeparationWithGround(...)
{
   onGround = false;
}

However, when the body 'skips' over the ground, it will create weird behavior, so I would recommend you create a timer, so that the Pod should stay on the ground for something like 100 ms before it can begin digging.

Also, to detect bodies that are nearby, you can use the World.Query() method. Just give it an AABB that is the area you wish to test for containment.

Nov 19, 2012 at 3:55 PM

That is totally what i needed.. thank you!

Nov 19, 2012 at 5:58 PM

Well i get the idea, but not how to do it. I got the "onGround" problem solved, no problem.. the problem is how to find the body below.

I got this. but it is not working... i think it has to do with the AABB. How do i get the box to be correct?

Second, how do i find the body BENEATH my pod, even if my Pod is surrounded by bodies (blocks to digg)  

 

 

private Body GetUnitToDigg()
{
      Body foundUnit = null;
      AABB aabb = new AABB(podBody.LocalCenter, podBody.LocalCenter);

      world.QueryAABB(p =>
      {
           foundUnit = p.Body;

           return true;

      }, ref aabb);

      return foundUnit;
}

 

Coordinator
Nov 20, 2012 at 8:15 PM

If your Pod is at position (10,15), you can use that as the center of the AABB, and then extend the lower and upper vertices of the AABB. Just like this:

Vector2 position = myBody.Position;
Vector2 extend = new Vector2(2,2);
AABB aabb = new AABB(position - extend, position + extend);

This will create an AABB with a center at (10,15) and it will be 4 meters wide, and 4 meters high. Once you give this to the query, it will find all bodies within that area until you return false.

To get the body directly below, you have several ways. I think the easiest is to check the y component of the position, on the body you collide with. Something like this:

private bool CharacterOnCollision(Fixture fixtureA, Fixture fixtureB, Contact contact)
{
    bool isBelow= fixtureB.Body.Position.Y < fixtureA.Body.Y;
    return true;
}

Note: fixtureA (the first parameter of the OnCollision delegate method) is always the fixture owned by the body that has the registered OnCollision delegate.

Nov 27, 2012 at 2:41 PM

Thank you Genbox, it was pretty much what i needed!