如果您知道形狀類型,可以使用以下任何函數直接檢查重疊。
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);
他們都採取兩個形狀和兩個變換。結果是一個Manifold對象,其中包含形狀邊界相交的點的集合。如果點數大於零,則發生碰撞。
通過類實現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) {}
}
這兩個機構禁止從Contact._fixtureA.Body和Contact._fixtureB.Body中找到。您必須向世界註冊偵聽器對象。
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.
}