1
\$\begingroup\$

I have a body named PersonA. If PersonA collides with another body, the width and height of that body should be saved. I tried it like this but I always get error messages that "Width" and "Height" don't exist.

bool PersonA_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
{
//How tall and broad is the body that collided with PersonA 
float Width = fixtureB.Body.Width;
float Height = fixtureB.Body.Height;

return true;
}

How can I get the width and height of another body? What should I change?

\$\endgroup\$
0
\$\begingroup\$

Bodies in Farseer have no "shape" of their own. The Body defines the position, velocity, mass, etc (basically a point-mass). Fixtures give that body a "shape" that can collide with things.

You can get the AABB (axis-aligned bounding-box) of a Fixture with Fixture.Shape.ComputeAABB(...). Don't use Fixture.GetAABB, which is a "loose" AABB used by Farseer for collision detection.

(Note that the LoopShape can have multiple child shapes, see Fixture.Shape.ChildCount. For other shapes just use childIndex = 0.)

Depending on your needs, you might want to get the AABB of the transformed Fixture. For example, to get the AABB in world space, use the transform from Body.GetTransform(...). Otherwise use:

Transform t = new Transform();
t.R.SetIdentity();

Note that a Body can have multiple Fixtures (Body.FixtureList). If this is the case, you will want to combine the AABBs of each Fixture using AABB.Combine(...).


If you want to guarantee that the resulting width and height are equal to something you set-up at object-creation time, you may want to just use the Body.UserData field to store that information.

\$\endgroup\$
  • \$\begingroup\$ For example, if I create a rectangle, how could I store it's width and height in the Body.UserData field? \$\endgroup\$ – Homer_Simpson Mar 30 '14 at 10:43
  • \$\begingroup\$ Body.UserData can store any object. So you can simply store it in that field as a Vector2. Then use a cast like (Vector2)Body.UserData to access it. \$\endgroup\$ – Andrew Russell Mar 30 '14 at 14:51
  • \$\begingroup\$ Thanx. I store the width and height in the Body.UserData field. It's the easiest way. \$\endgroup\$ – Homer_Simpson Mar 30 '14 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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