Question

J'utilise Box2dx (porté à C #, optimisé pour XNA). Il gère la résolution de collision, mais comment puis-je savoir si deux objets sont actuellement en collision?

Ceci est la fonction que je suis en train d'écrire:

public bool IsColliding(GameObjectController collider1, GameObjectController collider2)

collider1.Model.Body est le Body Box2d et collider1.Model.BodyDef est le BodyDef Box2d. (Va de même pour collider2, bien sûr.)

Mise à jour: On dirait que les auditeurs de contact ou cela pourrait être utile:

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

Pourquoi GetFixtureList() retourner un appareil?

Était-ce utile?

La solution

Si vous connaissez les types de forme que vous pouvez utiliser l'une des fonctions suivantes pour vérifier directement les chevauchements.

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);

Ils prennent tous deux formes et deux transformations. Le résultat est un objet collecteur qui contient une collection de points où se croisent les limites de forme. Si le nombre de points est supérieur à zéro, vous avez une collision.

Vous pouvez obtenir les mêmes informations indirectement en mettant en œuvre l'interface ContactListener par une classe.

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) {}
}

Les deux corps se trouve l'interdiction de Contact._fixtureA.Body et Contact._fixtureB.Body. Vous devez vous enregistrer l'objet d'écoute avec le monde.

GetFixtureList (), GetBodyList (), GetJointList (), etc. renvoyer le premier élément d'une liste chaînée. L'élément suivant de la liste se trouve en appelant GetNext () sur l'élément. Vous pouvez parcourir la liste avec le code suivant. Lorsque GetNext () retourne null il y a des éléments plus.

// Given there is a Body named body.
for (Fixture fix = body.GetFixtureList(); fix; fix = fix.GetNext()) {
    // Operate on fix.
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top