Box2D flying arrows engine – first attempt

This is an uncommented and unoptimized attempt to make an arrow engine. I used the third example of my Flying arrows simulation with Box2D, added the lines to have the camera following the latest arrow fired taken from Develop a Flash game like Angry Birds using Box2D – Following bird with the camera and skinning crates and played with a custom contact listener to add different behaviors to arrow-wall collision, arrow-crate collision and arrow-arrow collision.

I got also inspired by iforce2D sticky projectiles, and this is what I made:

Click on the stage to shoot an arrow from x=50, y=240 according to the angle between such coordinate and mouse pointer.

This is the main class:

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	import Box2D.Dynamics.Contacts.*;
	public class Main extends Sprite {
		private var world:b2World=new b2World(new b2Vec2(0,10),true);
		private var worldScale:int=30;
		private var arrowVector:Vector.=new Vector.();
		private var customContact=new CustomContactListener();
		public function Main():void {
			world.SetContactListener(customContact);
			debugDraw();
			wall(640,470,1280,20);
			wall(1270,240,20,4800);
			for (var i:Number=1; i<=10; i++) {
				crate(Math.random()*500+640,Math.random()*400,80,80);
			}
			addEventListener(Event.ENTER_FRAME, update);
			stage.addEventListener(MouseEvent.CLICK,addArrow);
		}
		private function addArrow(e:MouseEvent):void {
			var angle:Number=Math.atan2(mouseY-240,mouseX-50);
			var vertices:Vector.=new Vector.();
			vertices.push(new b2Vec2(-1.4,0));
			vertices.push(new b2Vec2(0,-0.1));
			vertices.push(new b2Vec2(0.6,0));
			vertices.push(new b2Vec2(0,0.1));
			var bodyDef:b2BodyDef= new b2BodyDef();
			bodyDef.position.Set(50/worldScale,240/worldScale);
			bodyDef.type=b2Body.b2_dynamicBody;
			bodyDef.userData={name:"arrow",freeFlight:false,follow:true};
			bodyDef.bullet=true;
			var polygonShape:b2PolygonShape = new b2PolygonShape();
			polygonShape.SetAsVector(vertices,4);
			var fixtureDef:b2FixtureDef = new b2FixtureDef();
			fixtureDef.shape=polygonShape;
			fixtureDef.density=1;
			fixtureDef.friction=0.5;
			fixtureDef.restitution=0.5;
			var body:b2Body=world.CreateBody(bodyDef);
			body.CreateFixture(fixtureDef);
			body.SetLinearVelocity(new b2Vec2(30*Math.cos(angle),30*Math.sin(angle)));
			body.SetAngle(angle);
			for (var i:Number=0; i=0; i--) {
				var body:b2Body=arrowVector[i];
				if (body.GetType()==b2Body.b2_dynamicBody) {
					if (! body.GetUserData().freeFlight) {
						var flyingAngle:Number=Math.atan2(body.GetLinearVelocity().y,body.GetLinearVelocity().x);
						body.SetAngle(flyingAngle);
					}
				}
				else {
					arrowVector.splice(i,1);
					body.SetBullet(false);
					body.GetUserData().follow=false;
				}
				if (body.GetUserData().follow) {
					var posX:Number=body.GetPosition().x*worldScale;
					posX=stage.stageWidth/2-posX;
					if (posX>0) {
						posX=0;
					}
					if (posX<-640) {
						posX=-640;
					}
					x=posX;
				}
			}
			world.DrawDebugData();
		}
	}
}

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 PreSolve(contact:b2Contact, oldManifold:b2Manifold):void {
			var contactPoint:b2Vec2;
			var weldJointDef:b2WeldJointDef;
			if (contact.IsTouching()) {
				var bodyA:b2Body=contact.GetFixtureA().GetBody();
				var bodyB:b2Body=contact.GetFixtureB().GetBody();
				var objA:Object=bodyA.GetUserData();
				var objB:Object=bodyB.GetUserData();
				if (objA.name=="arrow"&&objB.name=="arrow") {
					for (var j:b2JointEdge=bodyA.GetJointList(); j; j=j.next) {
						bodyA.GetWorld().DestroyJoint(j.joint);
					}
					for (j=bodyB.GetJointList(); j; j=j.next) {
						bodyB.GetWorld().DestroyJoint(j.joint);
					}
				}
				if (objA.name=="wall"&&objB.name=="arrow") {
					if (! objB.freeFlight) {
						weldJointDef = new b2WeldJointDef();
						weldJointDef.Initialize(bodyB,bodyA,bodyA.GetWorldCenter());
						bodyB.GetWorld().CreateJoint(weldJointDef);
					}
				}
				if (objB.name=="wall"&&objA.name=="arrow") {
					if (! objA.freeFlight) {
						weldJointDef = new b2WeldJointDef();
						weldJointDef.Initialize(bodyA,bodyB,bodyB.GetWorldCenter());
						bodyA.GetWorld().CreateJoint(weldJointDef);
					}
				}
				if (objA.name=="crate"&&objB.name=="arrow") {
					contactPoint=contact.GetManifold().m_points[0].m_localPoint;
					if (! objB.freeFlight&&Math.round(contactPoint.x*10)==6) {
						weldJointDef = new b2WeldJointDef();
						weldJointDef.Initialize(bodyB,bodyA,bodyA.GetWorldCenter());
						bodyB.GetWorld().CreateJoint(weldJointDef);
					}
				}
				if (objB.name=="crate"&&objA.name=="arrow") {
					contactPoint=contact.GetManifold().m_points[0].m_localPoint;
					if (! objA.freeFlight&&Math.round(contactPoint.x*10)==6) {
						weldJointDef = new b2WeldJointDef();
						weldJointDef.Initialize(bodyA,bodyB,bodyB.GetWorldCenter());
						bodyA.GetWorld().CreateJoint(weldJointDef);
					}
				}
				if (objB.name=="arrow") {
					objB.freeFlight=true;
				}
				if (objA.name=="arrow") {
					objA.freeFlight=true;
				}
			}
		}
	}
}

Next time, I'll add some improvements like a real bow firing arrow at different speed. Meanwhile feel free to give feedback or suggestions.

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

214 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
// 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