Search code examples
xnafarseer

Are BodyDef / FixtureDef available in Farseer Physics?


I've searched through all of the documentation that I can find for Farseer, and cannot find how to use BodyDef / FixtureDef in my projects. Have these been renamed to something else when they were ported over from Box2D?

My main concern is accomplishing something similar to the following:

FixtureDef fdef;
fdef.isSensor = true;

Solution

  • Although Farseer Physics has origins in Box2D, it is not an exact port of it. It has some similarities but does things it's own way (more familiar with C# programmers I guess). It doesn't use Def classes. I believe the following 2 examples are equivalent:

    C++

    // create a body
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;    
    bodyDef.position.Set(-10, 20);
    b2Body* body = world->CreateBody(&bodyDef);
    
    // create a shape
    b2CircleShape circleShape;
    circleShape.m_radius = 0.5f;
    
    // create a fixture
    b2FixtureDef myFixtureDef;
    myFixtureDef.shape = &circleShape;
    body->CreateFixture(&myFixtureDef);
    

    C#

    // create a body
    var body = world.CreateBody();
    body.BodyType = BodyType.Dynamic;
    body.Position = new Vector2(-10, 20);
    
    // create a shape
    var circleShape = new CircleShape(0.5f);
    
    // create a fixture
    var fixture = body.CreateFixture(circleShape);
    

    As you can see from the body, the main difference is that you set the properties after creation on the actual instance rather than a def class.

    Farseer Physics also makes use of handy Factory classes. Take a look at the documentation for more information.

    https://farseerphysics.codeplex.com/documentation