题
我使用Box2dx(移植到C#;对于XNA优化)。它处理冲突解决,但我怎么能知道,如果两个物体碰撞当前?
这是我试图写的函数:
public bool IsColliding(GameObjectController collider1, GameObjectController collider2)
其中collider1.Model.Body
是Box2D的Body
,和collider1.Model.BodyDef
是Box2D的BodyDef
。 (这同样适用于collider2
,当然。)
<强>更新强>像接触听众或这可能是有用的外貌:
AABB collisionBox;
model.Body.GetFixtureList().GetAABB(out collisionBox);
为什么GetFixtureList()
返回一个夹具?
解决方案
如果你知道的形状的类型可以使用以下任何一种功能的直接检查重叠。
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);
它们都采取两个形状和两个变换。其结果是流形对象包含其中形状边界相交的点的集合。如果点的数量大于零你有一个碰撞。
可以间接由类实现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.
}
不隶属于 StackOverflow