简体   繁体   English

确定身体是否发生碰撞-Cocos2d / Box2D

[英]Determine if a body has collided - Cocos2d/Box2D

Is it possible to determine if a b2body has collided with another b2body from a different layer? 是否可以确定b2body是否已与来自不同层的另一个b2body发生碰撞? Also, how would I do that? 另外,我该怎么做?

EG 例如

I have a ball on my main game scene layer that is fired at a bomb in my level one layer. 我在主游戏场景层上有一个球,该球在第一层的炸弹中射击。 They collide and the bomb disappears. 他们相撞,炸弹消失了。

Please let me know if I need to be more clear 请让我知道是否需要更清楚

Thanks! 谢谢!

If you want bodies to collide they must belong to the same b2World. 如果要使身体碰撞,它们必须属于同一个b2World。 On what layer they are drawn and how does not matter to the physics. 它们绘制在哪一层上,与物理无关。 To determine when collision happens subclass b2ContactListener and implement callback functions: 要确定何时发生碰撞,请子类b2ContactListener并实现回调函数:

class MyContactListener : public b2ContactListener
{
public:
MyContactListener() : b2ContactListener() {}

void    BeginContact (b2Contact *contact);
void    EndContact (b2Contact *contact);
void    PreSolve (b2Contact *contact, const b2Manifold *oldManifold);
void    PostSolve (b2Contact *contact, const b2ContactImpulse *impulse);

};

then add the object of this class to your b2World: 然后将此类的对象添加到您的b2World中:

MyContactListener *listener = new MyContactListener();
world->SetContactListener(listener);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM