Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Angular Acceleration and thrust

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 ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-10-08, 04:59
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Angular Acceleration and thrust

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:
Attached Files
File Type: zip AngularAcceleration.zip (8.0 KB, 33 views)

__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Acceleration with Actionscript 3.0 Flep Tutorials 0 08-10-07 16:29


All times are GMT. The time now is 17:06.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap