PhysInjector game prototype translated in pure Box2D

When I made the PhysInjector game prototype in only 100 lines of code I told you you would need a lot more lines (and work) in order to make the same thing run with pure Box2D.

Today I am showing you how much work.. this is the main class:

package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.text.TextField;
	import Box2D.Dynamics.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	import Box2D.Dynamics.Contacts.*;
	import Box2D.Dynamics.Joints.*;
	public class Main extends Sprite {
		private var world:b2World=new b2World(new b2Vec2(0,10),true);
		private var worldScale:Number=30;
		private var joint:b2RevoluteJoint;
		private var bodyFloor:b2Body;
		private var floor:Sprite= new Sprite();
		private var colorArray:Array=[0xff0000,0x00ff00,0x0000ff,0xffff00,0xff00ff,0x00ffff,0x000000,0xffffff];
		private var boxColor:Number=Math.floor(Math.random()*colorArray.length);
		private var lives:Number=3;
		private var score:Number=0;
		private var scoreText:TextField = new TextField();
		public function Main() {
			var customContact:CustomContactListener=new CustomContactListener();
			world.SetContactListener(customContact);
			graphics.lineStyle(2,0x000000);
			graphics.moveTo(0,100);
			graphics.lineTo(640,100);
			addChild(scoreText);
			floor.x=50;
			floor.y=420;
			addChild(floor);
			fillFloor();
			var bodyDef:b2BodyDef=new b2BodyDef();
			bodyDef.position.Set(320/worldScale,430/worldScale);
			bodyDef.type=b2Body.b2_dynamicBody;
			bodyDef.userData={name:"floor",asset:floor};
			var polygonShape:b2PolygonShape=new b2PolygonShape();
			polygonShape.SetAsBox(270/worldScale,10/worldScale);
			var fixtureDef:b2FixtureDef=new b2FixtureDef();
			fixtureDef.shape=polygonShape;
			fixtureDef.density=10;
			fixtureDef.restitution=0;
			fixtureDef.friction=0.2;
			bodyFloor=world.CreateBody(bodyDef);
			bodyFloor.CreateFixture(fixtureDef);
			var revoluteJointDef:b2RevoluteJointDef=new b2RevoluteJointDef();
			revoluteJointDef.localAnchorA.Set(0,0);
			revoluteJointDef.localAnchorB.Set(320/worldScale,430/worldScale);
			revoluteJointDef.bodyA=bodyFloor;
			revoluteJointDef.bodyB=world.GetGroundBody();
			revoluteJointDef.enableMotor=true;
			joint=world.CreateJoint(revoluteJointDef) as b2RevoluteJoint;
			var sensor:Sprite=new Sprite();
			sensor.graphics.beginFill(0x000000);
			sensor.graphics.drawRect(-640,-10,1280,20);
			sensor.graphics.endFill();
			addChild(sensor);
			sensor.x=-320;
			sensor.y=500;
			bodyDef.position.Set(320/worldScale,510/worldScale);
			bodyDef.type=b2Body.b2_staticBody;
			bodyDef.userData={name:"sensor",asset:sensor};
			polygonShape.SetAsBox(640/worldScale,10/worldScale);
			fixtureDef.shape=polygonShape;
			fixtureDef.isSensor=true;
			var sensorBody:b2Body=world.CreateBody(bodyDef);
			sensorBody.CreateFixture(fixtureDef);
			addEventListener(Event.ENTER_FRAME,update);
			stage.addEventListener(MouseEvent.CLICK,addBox);
		}
		private function fillFloor():void {
			floor.graphics.clear();
			floor.graphics.beginFill(colorArray[boxColor]);
			floor.graphics.drawRect(-270,-10,540,20);
			floor.graphics.endFill();
		}
		private function addBox(e:MouseEvent):void {
			if (mouseY<100) {
				var box:Sprite = new Sprite();
				box.graphics.beginFill(colorArray[boxColor]);
				box.graphics.drawRect(-25,-25,50,50);
				box.graphics.endFill();
				box.x=mouseX-25;
				box.y=mouseY-25;
				addChild(box);
				var bodyDef:b2BodyDef=new b2BodyDef();
				bodyDef.position.Set(mouseX/worldScale,mouseY/worldScale);
				bodyDef.type=b2Body.b2_dynamicBody;
				bodyDef.userData={name:"box",asset:box,val:boxColor,hit:false,destroy:false};
				var polygonShape:b2PolygonShape=new b2PolygonShape();
				polygonShape.SetAsBox(25/worldScale,25/worldScale);
				var fixtureDef:b2FixtureDef=new b2FixtureDef();
				fixtureDef.shape=polygonShape;
				fixtureDef.density=1+score/100;
				fixtureDef.restitution=0;
				fixtureDef.friction=0.2;
				var body:b2Body=world.CreateBody(bodyDef);
				body.CreateFixture(fixtureDef);
				boxColor=Math.floor(Math.random()*colorArray.length);
				fillFloor();
			}
		}
		private function update(event:Event):void {
			world.Step(1/30,10,10);
			for (var currentBody:b2Body=world.GetBodyList(); currentBody; currentBody=currentBody.GetNext()) {
				if (currentBody.GetUserData()) {
					currentBody.GetUserData().asset.x=currentBody.GetPosition().x*worldScale;
					currentBody.GetUserData().asset.y=currentBody.GetPosition().y*worldScale;
					currentBody.GetUserData().asset.rotation=currentBody.GetAngle()*(180/Math.PI);
					if (currentBody.GetUserData().hit) {
						currentBody.GetUserData().hit=false;
						score+=0.5;
						currentBody.GetUserData().asset.alpha-=0.25;
						if (currentBody.GetUserData().asset.alpha<=0) {
							score+=2.5;
							removeChild(currentBody.GetUserData().asset);
							world.DestroyBody(currentBody);
						}
						scoreText.text=score.toString()+" - "+lives.toString()+" lives";
					}
					if (currentBody.GetUserData().destroy) {
						removeChild(currentBody.GetUserData().asset);
						world.DestroyBody(currentBody);
						lives--;
						scoreText.text=score.toString()+" - "+lives.toString()+" lives";
						if (lives==0) {
							removeEventListener(Event.ENTER_FRAME,update);
							stage.removeEventListener(MouseEvent.CLICK,addBox);
						}
					}
				}
			}
			joint.SetMotorSpeed(bodyFloor.GetUserData().asset.rotation);
			joint.SetMaxMotorTorque(Math.abs(bodyFloor.GetUserData().asset.rotation)*100);
			world.ClearForces();
		}
	}
}

… and this is the custom contact listener

package {
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Dynamics.Joints.*;
	import Box2D.Dynamics.Contacts.*;
	import Box2D.Common.Math.b2Vec2;
	class CustomContactListener extends b2ContactListener {
		override public function BeginContact(contact:b2Contact):void {
			var bodyA:b2Body=contact.GetFixtureA().GetBody();
			var bodyB:b2Body=contact.GetFixtureB().GetBody();
			if (bodyA.GetUserData().name=="box"&&bodyB.GetUserData().name=="box"&&bodyA.GetUserData().val==bodyB.GetUserData().val) {
				bodyA.GetUserData().hit=true;
				bodyB.GetUserData().hit=true;
			}
			if (bodyA.GetUserData().name=="sensor"&&bodyB.GetUserData().name=="box") {
				bodyB.GetUserData().destroy=true;
			}
			if (bodyA.GetUserData().name=="box"&&bodyB.GetUserData().name=="sensor") {
				bodyA.GetUserData().destroy=true;
			}
		}
	}
}

As you can see, the whole script is 138+23 = 161 lines long, moreover I had to override a function, create one more class, and spend more time.

That’s why I am using PhysInjector library for a quick game I am going to release next week.

Anyway, this is the result of the Box2D version:

Click with mouse button above the black line and match boxes of the same color.

Download the source code.

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