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”:

package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	public class Main extends Sprite {
		public function Main() {
			var floor:Sprite = new Sprite();
			floor.graphics.beginFill(0x000000);
			floor.graphics.drawRect(0,0,640,20);
			floor.graphics.endFill();
			floor.x=0;
			floor.y=460;
			addChild(floor);
			stage.addEventListener(MouseEvent.CLICK,addBox);
		}
		private function addBox(e:MouseEvent):void{
			var box:Sprite = new Sprite();
			box.graphics.beginFill(0x000000);
			box.graphics.drawRect(0,0,50,50);
			box.graphics.endFill();
			box.x=mouseX-25;
			box.y=mouseY-25;
			addChild(box);	
		}		
	}
}

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:

package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import Box2D.Common.Math.b2Vec2;
	import com.reyco1.physinjector.PhysInjector;
	import com.reyco1.physinjector.data.PhysicsObject;
	import com.reyco1.physinjector.data.PhysicsProperties;
	public class Main extends Sprite {
		private var physics:PhysInjector;
		public function Main() {
			physics=new PhysInjector(stage,new b2Vec2(0,10),true);
			var floor:Sprite = new Sprite();
			floor.graphics.beginFill(0x000000);
			floor.graphics.drawRect(0,0,640,20);
			floor.graphics.endFill();
			floor.x=0;
			floor.y=460;
			addChild(floor);
			var box2dFloor:PhysicsObject=physics.injectPhysics(floor,PhysInjector.SQUARE,new PhysicsProperties({isDynamic:false,friction:0.5,restitution:0.5}));
			addEventListener(Event.ENTER_FRAME,update);
			stage.addEventListener(MouseEvent.CLICK,addBox);
		}
		private function addBox(e:MouseEvent):void {
			var box:Sprite = new Sprite();
			box.graphics.beginFill(0x000000);
			box.graphics.drawRect(0,0,50,50);
			box.graphics.endFill();
			box.x=mouseX-25;
			box.y=mouseY-25;
			addChild(box);
			var box2dBox:PhysicsObject=physics.injectPhysics(box,PhysInjector.SQUARE,new PhysicsProperties({isDynamic:true,friction:0.2,restitution:0.5}));
		}
		private function update(event:Event):void {
			physics.update();
		}
	}
}

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

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers