Listening to contact events in Box2D AS3
From Box2D Wiki
This article will explain how to listen for contact events in Box2D AS3 a little more comprehensively than the Manual does. Example code is given, but it is assumed that you know how to create a world and bodies in it.
Overview
The method we use consists of a number of steps.
- Step 1
- A class has to be created that extends b2ContactListener, and an instance of this class has to be added to the world.
- Step 2
- The custom class has to make deep copies of the contacts, or it has to copy the relevant attributes, and add these to a stack.
- Step 3
- There has to be a method for working through the contact stack, and using this information to do something in your application.
Example Code
First we have to create a class that extends b2ContactListener. Suppose we are only interested in the first occurrence of a collision, so we will only handle "Add" events. We will copy the relevant attributes of the collision to our own object (CustomContactPoint), also given below.
class CustomContactPoint { public var shape1:Actor; public var shape2:Actor; public var separation:Number; public var position:b2Vec2; public function CustomContactPoint (s1:b2Shape,s2:b2Shape, f:Number, p:b2Vec2){ shape1 = s1; shape2 = s2; separation = f; position = p; } }
The previous class will only store the information that is relevant to us, in this case the shapes that have collided, how deeply they penetrate eachother, and the position where the collision occurred. The contact listener class below stores these relevant attributes in a new object, and adds it to the stack.
Note: Do NOT store the b2ContactPoint. You must copy and store information in a new object as the b2ContactPoint is reused by Box2d.
class ContactListener extends b2ContactListener{ public var contactStack:Array = new Array(); override public function Add(point:b2ContactPoint):void{ var shape1:b2Shape = point.shape1; var shape2:b2Shape = point.shape2; var separation:Number = point.separation; var position:b2Vec2 = point.position.Copy(); contactStack.push(new CustomContactPoint(shape1,shape2,separation,position)); } }
What remains to be done now is add an instance of this contact listener to the world. As follows.
var myContactListener = new ContactListner(); myWorld.SetContactListener(myContactListener);
Now we can do something with the contacts stored on the stack. The reason that we don't use the contact information immediately is that it is not a good idea to manipulate the world inside the timestep. Now suppose we want to add an explosion to the screen at the location where the two shapes have collided. Imagine that we can simply call new Explosion(x,y,size). We should execute the following while loop every frame, to handle the contacts that have occurred.
//As long as there is still an unhandled contact on the stack while(myContactListener.contactStack[0]) { var currentContact:CustomContactPoint = myContactListener.contactStack.pop(); var xPos:Number = currentContact.position.x * physScale; var yPos:Number = currentContact.position.y * physScale; var penetration:Number = -currentContact.separation; addChild(new Explosion(xPos, yPos, penetration)); }
Another example of using the contact points can be found in the Manual.
| Tutorials
General AS3
|
