Add Box2D physics to your projects in a snap with PhysInjector

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).

  • lol i’ve been doing this since i started with box2d 3 years ago. no need for fancy-named libs, just a few lines of code.

  • Awesome. Could be very useful for mobile apps.

  • Need to learn this fast. I’m sure this will be useful to our future projects.

  • kek

    Why not use Quickbox2D ?

  • benben

    How to remove the box when the box out of stage?

  • foster

    @kek Qbox2D is dead bro :(

  • @benben to remove physics from any object just do

    physics.removePhysics( myObj );

    You can then remove your box manually with removeChild.

  • benben

    In,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{

  • @benben all that does is remove the physics properties from the display object. Do removePhysics and then perform a removeChild.

  • benben

    @reyco1,Thank you!
    I get it!

  • You should start your own youtube channel man. I’d watch your guides!

  • 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?

  • faisal

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

  • 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

  • Konstantin

    How to disable drag and drop?

  • Pingback: PhysInjector game prototype in only 100 lines of code - Emanuele Feronato()

  • Pingback: Starling + TexturePacker + PhysInjector + usaha = SERU! | DigitalCrawler()

  • 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