PhysInjector is a collection of powerful factory and wrapper classes developed to work specifically with Box2D. It 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 uses display objects that have already been added to the stage and “injects†them with physics properties, instead of you having to create a physics object and apply a display object as a skin. It will also handle the display object’s position, scale or rotation according to the physics.
Sample
package com.reyco1.box2dwrapper.samples.tutorials { import Box2D.Common.Math.b2Vec2; import com.reyco1.physinjector.PhysInjector; import com.reyco1.physinjector.data.PhysicsObject; import com.reyco1.physinjector.data.PhysicsProperties; import flash.display.Sprite; import flash.events.Event; [SWF(width="800", height="600", frameRate="60")] public class SimpleTest extends Sprite { protected var floor:Sprite; protected var box:Sprite; protected var physics:PhysInjector; public function SimpleTest() { super(); addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage); } protected function handleAddedToStage(event:Event):void { removeEventListener(Event.ADDED_TO_STAGE, handleAddedToStage); createSprites(); injectPhysics(); addEventListener(Event.ENTER_FRAME, onUpdate); } private function createSprites():void { floor = new Sprite(); floor.graphics.beginFill(0xFF0000); floor.graphics.drawRect(0, 0, stage.stageWidth - 20, 20); floor.graphics.endFill(); floor.x = stage.stageWidth * 0.5 - floor.width * 0.5; floor.y = stage.stageHeight - floor.height - 10; addChild( floor ); box = new Sprite(); box.graphics.beginFill(0x00FF00); box.graphics.drawRect(0, 0, 50, 50); box.graphics.endFill(); box.x = stage.stageWidth * 0.5 - box.width * 0.5; box.y = 10; addChild( box ); } private function injectPhysics():void { physics = new PhysInjector(stage, new b2Vec2(0, 60), true); var floorObject:PhysicsObject = physics.injectPhysics(floor, PhysInjector.SQUARE, new PhysicsProperties({isDynamic:false, friction:0.5, restitution:0.5})); var boxP:PhysicsObject = physics.injectPhysics(box, PhysInjector.SQUARE, new PhysicsProperties({isDynamic:true, friction:0.2, restitution:0.5})); } protected function onUpdate(event:Event):void { physics.update(); } public function clear():void { removeEventListener(Event.ENTER_FRAME, onUpdate); physics.dispose(); physics = null; } } }
Does this work with Starling as well? That would be really good to know…
Yes, it does! According to the author, all you have to do is use
StarlingPhysInjector
.Hi Adam, It does work with Starling as Dovyski said, just Use StarlingPhysInjector instead of PhysInjector. Now, there is one caveat and it’s something I’m currently working on: your Starling display object must have a centered registration point (pivotX and pivotY). Hopefully, in the upcoming version, that wont be an issue. Check out the starling sample and download the code here : http://goo.gl/THQhw
Thanks!