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 ...
We saw how to apply the physics to Actionscript.
I remember you some examples: spring, spring an friction, spring with friction and gravity.
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.
private var velocity_x:Number=0;
private var velocity_y:Number=0;
Adding the listeners
once controls the airplane
airplane_mc.addEventListener(Event.ENTER_FRAME,mov eAirplane);
and the others listen for key pressing of the keyboard ( left, right and up )
stage.addEventListener(KeyboardEvent.KEY_DOWN,onDo wn);
stage.addEventListener(KeyboardEvent.KEY_UP,onUp);
Methods
onDown
This method checks which key of keyboard has been pressed and add values of angular acceleration and thrust
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;
}
onUp
get back to zero the values of angular acceleration and thrust
rotational_velocity=0;
thrust=0;
moveAirplane
I add angular acceleration to airplane's rotation
evt.target.rotation+=rotational_velocity;
create a variable that contains the value of the rotation's angle airplane
var angle:Number=evt.target.rotation*Math.PI/180;
create both accelerations based on the value of sine and cosine of angle
var acceleration_x:Number=Math.cos(angle)*thrust;
var acceleration_y:Number=Math.sin(angle)*thrust;
adding the accelerations to respective velocities
velocity_x+=acceleration_x;
velocity_y+=acceleration_y;
adding velocities to x and y of the airplane
evt.target.x+=velocity_x;
evt.target.y+=velocity_y;
Source files:
Bookmarks