Box2DAS3 Example – Mouse and Keyboard interaction, collision detection and reaction, camera follow and custom graphics

AS3, Physics 3 Comments

Here 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

3 Responses to “Box2DAS3 Example – Mouse and Keyboard interaction, collision detection and reaction, camera follow and custom graphics”

  1. Luis Says:
    July 15th, 2009 at 22:57

    Hello,
    This is really nice and thanks for posting it.
    However I tried to unzip your file and the fla appears to be corrupted can you try upload again.
    Best
    -Luis

  2. ab Says:
    July 16th, 2009 at 10:41

    Probably it was saved as CS4 and you only have CS3. I just opened the download to check and it was fine. So I saved it as CS3 and uploaded again, you should be able to open it now, unless your Flash is something before CS3.

    Please also note that you don’t need the FLA to use this class. Just pick another FLA and associate it with the “Main” class, and make three symbols in the library with the following linkage names:

    Linkage names: Symbol:

    PhysGround – A long rectangle 2000width x 2000height
    PhysCircle – A gray circle (100 x 100)
    PhysCircleBlue – A blue circle (100 x 100)

    All symbols should have centered registration points !

    Let me know if you got it working

  3. Rheal Says:
    November 26th, 2009 at 16:22

    Hello there, I am interested in collision detection inside a circle (as opposed to the PhysGround instances). I’m attempting to make a snow globe.
    Any help would be greatly appreciated.
    Thanks in advance

leave a reply

data recovery software