This is a discussion on Angular Acceleration and thrust within the Tutorials forums, part of the Flash English category; We saw how to apply the physics to Actionscript .
I remember you some examples: spring , spring an friction , spring with ...
In this tutorial we'll see how to apply angular acceleration and thrust, very usefull for games development.
Using a small airplane which I add 3 keyboard controls:
left rotation
right rotation
thrust
Click the SWF and press LEFT or RIGHT or UP on your keyboard
I create a new FLA and save it as name "main.fla".
On the stage I have a MovieCLip with instance name "airplane_mc".
I create the Document Class, AS file that I save as name "Main.as", like the following:
Code:
package
{
import flash.display.*;
import flash.events.*;
import flash.ui.*;
public class Main extends MovieClip
{
private var rotational_velocity:int=0;
private var thrust:Number=0;
private var velocity_x:Number=0;
private var velocity_y:Number=0;
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(evt:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);
stage.frameRate=31;
airplane_mc.addEventListener(Event.ENTER_FRAME,moveAirplane);
stage.addEventListener(KeyboardEvent.KEY_DOWN,onDown);
stage.addEventListener(KeyboardEvent.KEY_UP,onUp);
}
private function onDown(evt:KeyboardEvent):void
{
switch(evt.keyCode)
{
case Keyboard.LEFT :
rotational_velocity=-5;
break;
case Keyboard.RIGHT :
rotational_velocity=5;
break;
case Keyboard.UP :
thrust=.2;
break;
default :
break;
}
}
private function onUp(evt:KeyboardEvent):void
{
rotational_velocity=0;
thrust=0;
}
private function moveAirplane(evt:Event):void
{
evt.target.rotation+=rotational_velocity;
var angle:Number=evt.target.rotation*Math.PI/180;
var acceleration_x:Number=Math.cos(angle)*thrust;
var acceleration_y:Number=Math.sin(angle)*thrust;
velocity_x+=acceleration_x;
velocity_y+=acceleration_y;
evt.target.x+=velocity_x;
evt.target.y+=velocity_y;
}
}
}
Analizing code.
Properties
one numeric variable that is the angular acceleration
private var rotational_velocity:int=0;
one numeric variable that is the thrust
private var thrust:Number=0;
two numeric variables that are velocity on x and y axis.