如果您知道形状类型,则可以使用以下任何功能直接检查重叠。
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 中找到这两个身体。您必须向 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.
}