| 版权声明:本文为博主原创文章,未经博主允许不得转载。
在Box2D中碰撞事件由b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象函数:
1 /// Called when two fixtures begin to touch.两个物体开始接触时会响应,但只调用一次。 2 virtual void BeginContact(b2Contact* contact) { B2_NOT_USED(contact); } 3 4 /// Called when two fixtures cease to touch.分离时响应。但只调用一次。 5 virtual void EndContact(b2Contact* contact) { B2_NOT_USED(contact); } 6 7 8 9 /// This is called after a contact is updated. This allows you to inspect a 10 /// contact before it goes to the solver. If you are careful, you can modify the 11 /// contact manifold (e.g. disable contact). 12 /// A copy of the old manifold is provided so that you can detect changes. 13 /// Note: this is called only for awake bodies. 14 /// Note: this is called even when the number of contact points is zero. 15 /// Note: this is not called for sensors. 16 /// Note: if you set the number of contact points to zero, you will not 17 /// get an EndContact callback. However, you may get a BeginContact callback 18 /// the next step.持续接触时响应,它会被多次调用。 19 virtual void PreSolve(b2Contact* contact, const b2Manifold* oldManifold) 20 { 21 B2_NOT_USED(contact); 22 B2_NOT_USED(oldManifold); 23 } 24 25 /// This lets you inspect a contact after the solver is finished. This is useful 26 /// for inspecting impulses. 27 /// Note: the contact manifold does not include time of impact impulses, which can be 28 /// arbitrarily large if the sub-step is small. Hence the impulse is provided explicitly 29 /// in a separate data structure. 30 /// Note: this is only called for contacts that are touching, solid, and awake. 31 32 /// 持续接触时响应,调用完preSolve后调用。 33 virtual void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) 34 { 35 B2_NOT_USED(contact); 36 B2_NOT_USED(impulse); 37 }