Skip to content


Actionscript Physics Engine (APE) Trajectory

I was searching for some trajectory code and came across this thread over at Flashkit:


http://board.flashkit.com/board/showthread.php?t=769839

With a few slight tweaks I was able to create this little demo:

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.TimerEvent;
    import flash.text.TextField;
    import flash.utils.Timer;
	import fl.controls.Button;
    import org.cove.ape.APEngine;
    import org.cove.ape.Group;
    import org.cove.ape.RectangleParticle;
    import org.cove.ape.Vector;

    public class Main extends Sprite
    {
        private var frontPart:RectangleParticle;

        private var btn:TextField;
        private var timer:Timer;

        private var lastPos:Vector;
        private var button:Button;

        private static var conversion:Number = 180 / Math.PI; //radian to degree conversion

        public function Main():void
        {
            lastPos = new Vector(0, 0);
            frontPart = new RectangleParticle(50, stage.stageHeight - 100, 50, 10, 0, false, 1);
            APEngine.container = this;
            APEngine.init();

            var g:Group = new Group();
            g.addParticle(frontPart);
            APEngine.addGroup(g);

			var g2:Group = new Group();
            g2.addParticle(new RectangleParticle(300, stage.stageHeight - 20, 1000, 50, 0, true));
            g2.addCollidable(g);
            APEngine.addGroup(g2);
            APEngine.addForce(new Vector(0, 1));

            // NEW BUTTON
            button = new Button();
            button.width = 150;
            button.height = 30;
            button.move (stage.stageWidth / 2 - button.width / 2, stage.stageHeight - 25 - button.height / 2 );
            button.label = "Run";    

            addChild(button);    

            button.addEventListener(MouseEvent.CLICK, beginMotion);  

            timer = new Timer(20);
            timer.addEventListener(TimerEvent.TIMER, step);
        }

        private function beginMotion(m:MouseEvent):void
        {
            button.label = "Stop";
            button.removeEventListener(MouseEvent.CLICK, beginMotion);

            frontPart.addForce(new Vector(Math.random() * 100, Math.random() * -80)); //change this vector to adjust firing angle/strength

            timer.start();
            button.addEventListener(MouseEvent.CLICK, stopMotion);
        }

        private function step(event:Event):void
        {
            lastPos.copy(frontPart.center);
            APEngine.step();
            var angle:Number = Math.atan2((frontPart.center.y - lastPos.y), (frontPart.center.x - lastPos.x));
            frontPart.angle = angle * conversion;
            APEngine.paint();
        }

        private function stopMotion(m:MouseEvent):void
        {
            button.label = "Run";
            button.removeEventListener(MouseEvent.CLICK, stopMotion);
            frontPart.position = new Vector(50, stage.stageHeight - 100);
            timer.reset();
            button.addEventListener(MouseEvent.CLICK, beginMotion);
        }
    }
}

Posted in APE - Actionscript Physics Engine, Actionscript 3.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.