0

Box2dx (C# に移植、XNA 用に最適化) を使用しています。衝突の解決を処理しますが、2 つのオブジェクトが現在衝突しているかどうかはどうすればわかりますか?

これは私が書こうとしている関数です:

public bool IsColliding(GameObjectController collider1, GameObjectController collider2)

collider1.Model.Bodyは Box2dでBodycollider1.Model.BodyDefは Box2dBodyDefです。(もちろん も同じcollider2です。)

更新:連絡先リスナーのように見えるか、これが役立つ可能性があります:

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

なぜGetFixtureList()1 つのフィクスチャを返すのですか?

4

1 に答える 1

0

シェイプ タイプがわかっている場合は、次の関数のいずれかを使用してオーバーラップを直接チェックできます。

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

それらはすべて 2 つの形状と 2 つの変換をとります。結果は、形状の境界が交差するポイントのコレクションを含む Manifold オブジェクトです。ポイントの数が 0 より大きい場合、衝突があります。

ContactListener インターフェースをクラスで実装することで、間接的に同じ情報を取得することができます。

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

2 つのボディは、Contact._fixtureA.Body と Contact._fixtureB.Body から見つけることができます。リスナー オブジェクトを World に登録する必要があります。

GetFixtureList()、GetBodyList()、GetJointList() などは、リンクされたリストの最初の要素を返します。リスト内の次の要素は、要素で GetNext() を呼び出すことによって検出されます。次のコードを使用して、リストを反復処理できます。GetNext() が null を返す場合、それ以上要素はありません。

// Given there is a Body named body.
for (Fixture fix = body.GetFixtureList(); fix; fix = fix.GetNext()) {
    // Operate on fix.
}
于 2010-07-02T07:54:46.753 に答える