views:

273

answers:

1

I'm using Box2dx (ported to C#; optimized for XNA). It handles collision resolution, but how can I tell if two objects are currently colliding?

This is the function I'm trying to write:

public bool IsColliding(GameObjectController collider1, GameObjectController collider2)

Where collider1.Model.Body is the Box2d Body, and collider1.Model.BodyDef is the Box2d BodyDef. (The same goes for collider2, of course.)

UPDATE: Looks like contact listeners or this could be useful:

        AABB collisionBox;
        model.Body.GetFixtureList().GetAABB(out collisionBox);

Why does GetFixtureList() return one fixture?

A: 

If you know the shape types you can use any of the following functions to directly check for overlap.

public static void Collision.CollideCircles(ref Manifold manifold,
    CircleShape circle1, XForm xf1, CircleShape circle2, XForm xf2);
public static void Collision.CollidePolygonAndCircle(ref Manifold manifold,
    PolygonShape polygon, XForm xf1, CircleShape circle, XForm xf2);
public static void Collision.CollideEdgeAndCircle(ref Manifold manifold,
    EdgeShape edge, XForm transformA, CircleShape circle, XForm transformB);
public static void Collision.CollidePolyAndEdge(ref Manifold manifold,
    PolygonShape polygon, XForm transformA, EdgeShape edge, XForm transformB);
public static void Collision.CollidePolygons(ref Manifold manifold,
    PolygonShape polyA, XForm xfA, PolygonShape polyB, XForm xfB);

They all take two shapes and two transforms. The result is a Manifold object which contains a collection of points where the shape boundaries intersect. If the number of points is greater than zero you have a collision.

You can get the same information indirectly by implementing the ContactListener interface by a class.

public class MyContactListener : ContactListener {
    // Called when intersection begins.
    void BeginContact(Contact contact) {
        // ...
        // Make some indication that the two bodies collide.
        // ...
    }

    // Called when the intersection ends.
    void EndContact(Contact contact) {
        // ...
        // Make some indication that the two bodies no longer collide.
        // ...
    }

    // Called before the contact is processed by the dynamics solver.
    void PreSolve(Contact contact, Manifold oldManifold) {}

    // Called after the contact is processed by the dynamics solver.
    void PostSolve(Contact contact, ContactImpulse impulse) {}
}

The two bodies ban be found from Contact._fixtureA.Body and Contact._fixtureB.Body. You have to register the listener object with the World.

GetFixtureList(), GetBodyList(), GetJointList(), etc. return the first element in a linked list. The next element in the list is found by calling GetNext() on the element. You can iterate over the list with the following code. When GetNext() returns null there are no more elements.

// Given there is a Body named body.
for (Fixture fix = body.GetFixtureList(); fix; fix = fix.GetNext()) {
    // Operate on fix.
}
Staffan E