Helicopter trajectory formula for Actionscript three?

Go To StackoverFlow.com

0

Right, I'm trying to implement this game,

2012-04-04 22:26
by Adzi
what you need to learn is projectile motion in physics. : - hjpotter92 2012-04-04 22:32


1

That game uses really simple physics; basically just subtracting a value from a velocity property when you hold the mouse, otherwise adding to it.

The y of the helicopter is increased by the velocity (will being to rise once velocity is below 0). This will also cause the gradual speeding up / slowing down effect that you see.


Sample Code:

Game.as (this would be your document class).

package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class Game extends MovieClip
    {

        // Properties.
        private var _updating:Array = [];
        private var _mouseDown:Boolean = false;

        // Objects.
        private var _helicopter:Helicopter;


        /**
         * Constructor.
         */
        public function Game()
        {
            // Listeners.
            stage.addEventListener(Event.ENTER_FRAME, _update);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, _mouseAction);
            stage.addEventListener(MouseEvent.MOUSE_UP, _mouseAction);


            // Helicopter.
            _helicopter = new Helicopter();
            stage.addChild(_helicopter);
        }


        /**
         * Mouse action manager.
         * @param e MouseEvent instance.
         */
        private function _mouseAction(e:MouseEvent):void
        {
            switch(e.type)
            {
                default: _mouseDown = false; break;
                case MouseEvent.MOUSE_DOWN: _mouseDown = true; break;
            }
        }


        /**
         * Update the game.
         * @param e Event instance.
         */
        private function _update(e:Event):void
        {
            _helicopter.update(_mouseDown);
        }

    }
}

Helicopter.as

package
{
    import flash.display.Sprite;

    public class Helicopter extends Sprite
    {

        // Properties.
        private var _velocity:Number = 0;


        /**
         * Constructor.
         */
        public function Helicopter()
        {
            // Temp graphics.
            graphics.beginFill(0x00FF00);
            graphics.drawRect(0, 0, 60, 35);
            graphics.endFill();
        }


        /**
         * Update the helicopter.
         * @param mouseDown Boolean representing whether the mouse is held down or not.
         */
        public function update(mouseDown:Boolean):void
        {
            if(mouseDown) _velocity -= 1;
            else _velocity += 1;

            y += _velocity;
        }

    }
}
2012-04-04 22:46
by Marty
'1120: Access of undefined property velocity.' Do I have to set a speed for velocity beforehand - Adzi 2012-04-04 22:57
@Adzi No.. You need to define velocity. My code is more of a rough example. Do you need complete code? - Marty 2012-04-04 23:02
Yes please, sorry :(. Thank you for your help, I really appreciate it - Adzi 2012-04-04 23:08
@Adzi See edit mate - Marty 2012-04-04 23:37
Wow, thank you for your help - Adzi 2012-04-05 08:16


0

I remember playing that game so I think I understand the kind of motion it has implemented. Here is a brief idea of how to implement it yourself.

What you need is a simple formula in physics , This simple formula is as follows.

v = u+at;

Right. Thats your formula where v = final velocity u = initial velocity a = acceleration t = time.

Here time is your every step. The distance travelled at every step can be calculated using

s = u*t+ (0.5 * a*t*t);

s = distance travelled at a time step u = initial velocity t = time. a = acceleration ( which in your case will be either the gravity - which will come into effect when you leave the mouse. )

Each enter frame you see if the mouse is down, if the mouse is down for more than say 20 frames, after the 20th frame the acceleration starts going up by 1 unit ( you define the unit! ).

The same logic applies when you stop holding the mouse. After 20 frames ( or whatever time decay you define! ) the acceleration starts going down by 1 unit.

Refer : http://en.wikipedia.org/wiki/Equations_of_motion

2012-04-05 09:26
by ganaraj
Ads