Game Development Reference
In-Depth Information
4. Test the movie, and each time the ball bounces on the floor, you should see
the following output text:
a collision started
first body: Floor
second body: Ball
---------------------------
a collision ended
first body: Floor
second body: Ball
---------------------------
This is great, because now you know exactly when a collision starts and
when a collision ends, as well as the bodies that are colliding.
But the Box2D contact listener is giving us two more callbacks called
PreSolve and PostSolve .
Detect when you are about to solve a
collision and when you have solved it
The Pre-Solve Event is called after collision detection, but before collision resolution,
so you can interact with a collision before it's solved. The Post-Solve Event occurs
when a collision has been solved, and allows us to know the impulse of the collision.
1.
Add the following two functions to the customContact class:
override public function PreSolve(contact:b2Contact,
oldManifold:b2Manifold):void {
if (contact.GetManifold().m_pointCount>0) {
trace("a collision has been pre solved");
var fixtureA:b2Fixture=contact.GetFixtureA();
var fixtureB:b2Fixture=contact.GetFixtureB();
var bodyA:b2Body=fixtureA.GetBody();
var bodyB:b2Body=fixtureB.GetBody();
trace("first body: "+bodyA.GetUserData());
trace("second body: "+bodyB.GetUserData());
trace("---------------------------");
}
}
 
Search WWH ::




Custom Search