Box2DAS3 Example – Mouse and Keyboard interaction, collision detection and reaction, camera follow and custom graphics
May 3rd, 2009AS3, Physics 3 CommentsHere is an example of the fine Physics engine for Flash (AS3) named Box2DAS3.
This example demonstrates the implementation of some features required for real game or application development.
These are:
- Collision detection (and reaction)
- Mouse interaction (for all objects)
- Keyboard interaction (for the main character - blue ball)
- Camera follow (for the main character - blue ball)
- Custom graphics (taken at run-time from the Flash library)
Collision detection (and reaction):
To implement collision detection and reaction, one must first initialize a ContactListener class and then modify it to have the collision reactions the way we need. The initial setup is pretty easy:
contact_listener_var = new ContactListener()
m_world.SetContactListener(contact_listener_var);
Check the source code to see how to reactions are handled.
Mouse Interaction:
Mouse interaction is easy to implement, one has only to activate it with the appropriate Box2DAS3 code, check the source for details.
Keyboard interaction:
The keyboard interaction had to be implemented manually, at least it was the fastest way I had to obtain the behaviour I required from the main character (the blue ball). To do this I used the great Key class from CASALIB check the source code for details.
Camera follow:
It basically consists in moving the Sprite where your Box2DAS3 "world" is nested. Pretty easy. I added a slight easing effect to make it smoother. NOTE: in this example it is disabled when the user clicks an object.
Custom Graphics:
Box2DAS3 objects have a body definition property (class b2BodyDef) which in it's turn has a sub property named "userData" that contains the visual content of your Box2DAS3 object. It's basically a MovieClip. So you may draw graphics in it or addChild a MovieClip from your Flash library into it. Like bodyDef.userData = new SymbolLinkageName();
Get the SOURCE
Recent Comments