Have fun and help me to get to 1,000,000 downloads!

Add Box2D physics to your projects in a snap with PhysInjector

Emanuele Feronato Actionscript 3, Box2D, Flash

Do you want to add physics to your Flash projects with almost no code, keeping your scripts simple with only a few modifications, injecting physics attributes directly on your Display Ojects?

Then PhysInjector is the library you’ll love.

PhysInjector is a collection of powerfull factory and wrapper classes developed to work specifically with Box2D and does not do any collision detection on its own, rather it significantly simplifies the use of Box2D within your Flash games and applications while providing an array of handy plugins and helper classes.

PhysInjector takes a totally different approach than other conventional physics libraries. Instead of you having to create a physics object and apply a display object as a skin, it actually uses display objects that have already been added to the stage and “injects” them with physics properties. You don’t have to worry about the display object’s position, scale or rotation! PhysInjector will handle that for you!

I made a little example to let you see how easy is to ijnect physics into Display Objects.

The following script just adds black squares when you click the mouse, with no physics at all, it just places black squares on the screen.

Something like “my first AS3 script”:

And this is the result:

Click on the stage to add a black square.

Now, look at the same example, with physics injection: it’s very easy to understand:

And see what happens:

Click on the stage to add black squares which will turn into dynamic bodies.

Did you see how quick and easy it is? Download the source code with all needed libraries.

Next time I’ll show you how to code a complete physics game prototype just using a couple of lines (well, maybe a bit more, but you got the point).

From null to full HTML5 cross platform game

I will take you by hand from the bare bones of JavaScript programming through the creation of a full cross platform HTML5 game, with detailed explainations and source code.

If you don't know where to start, then From null to full HTML5 cross platform game is the book for you.

Comments 18

  1. benben

    In ContactTest.as,I want to remove the obj(bodyA),
    add the Code [physics.removePhysics(bodyA);],
    But can not remove bodyA.
    I’m not Sure If I need to remove the bodyA userData at first.

    The code as follow:

    private function handleContact(bodyA:b2Body, bodyB:b2Body, fixtureA:b2Fixture, fixtureB:b2Fixture):void{

  2. benben

    If I set the box.rotation = 45;,And show the debug Draw.
    The displayObject can not at the position as the debug object.
    That is Why?

  3. faisal

    Can i add physics to irregular display objects ? rather than squares and circles etc. Also can we create terrain using this library ?

  4. steven yeung

    I have a question, in box2d how do i add a shadow underneath the object so that it simulates / calculates it is falling from a great height

  5. Pingback: PhysInjector game prototype in only 100 lines of code - Emanuele Feronato

  6. Pingback: Starling + TexturePacker + PhysInjector + usaha = SERU! | DigitalCrawler

  7. akinori

    thank you for providing so much infos and tutorials about box2d. I am testing PhysInjector and I do not find a way to manage a camera.
    Creating and adding all my objects in a specific sprite, when trying to move the sprite like a camera, all my objects still stay at the their same position as if all coordinates were calculated relative to the document stage.

    Is their a way to prevent this behavior and to move the PhysInjector’s stage ?

    Thank you for your help

Leave a Reply

Your email address will not be published. Required fields are marked *