Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Actionscript 3, Box2D, Flash and Game development.

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.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.