I have a Player
class that contains a physics body represented by a sphere, and I want to be able to tell if the player is on the ground, on a wall, or in the air. So far I have a bool IsOnGround
that I set with these event handlers for the physics body:
void PhysicsBody_OnSeparation(Fixture fixtureA, Fixture fixtureB)
{
IsOnGround = false;
PhysicsBody_OnCollision(fixtureA, fixtureB, null);
}
bool PhysicsBody_OnCollision(Fixture fixtureA, Fixture fixtureB,
FarseerPhysics.Dynamics.Contacts.Contact contact)
{
if (fixtureB.UserData is JumpableFixtureData &&
fixtureB.Body.Position.Y > fixtureA.Body.Position.Y)
{
IsOnGround = true;
}
else
{
IsOnGround = false;
}
return true;
}
I have my level so that each level object sets the user data of their physics body to be a JumpableFixtureData
(just an empty class), and though most of the objects are rectangular, there are a few shapes with cave-like openings, such that the player is 'inside' the shape.
My problem is that sometimes the IsOnGround
bool is set to true when the object is still in the air, and sometimes it remains false after touching the ground. This setup clearly isn't working. What should I do instead? I found this tutorial in the Box2D tutorials, but I haven't used C++ in forever and I can't find any way to create a separate instance of a fixture so I can make it a 'foot'.