Car Movement: Acceleration, Turning & Braking

Friday, June 25th, 2010 | Basics, Game Examples AS3

AS3 version of my previous GTA style top-down view car driving game example. This example shows you how to implement car movement; Acceleration, Braking & Turning.

?View Code ACTIONSCRIPT
/**
 * Car Movement - Accelerate, Brake & Rotate
 * ---------------------
 * VERSION: 1.0
 * DATE: 6/22/2010
 * AS3
 * UPDATES AND DOCUMENTATION AT: http://www.FreeActionScript.com
 **/
package com.freeactionscript 
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
 
	public class Car extends Sprite
	{
		//Settings
		private var speed:Number = 0;
		private var speedMax:Number = 4;
		private var speedMaxReverse:Number = -1;
		private var speedAcceleration:Number = .15;
		private var speedDeceleration:Number = .90;
		private var groundFriction:Number = .95;
 
		private var steering:Number = 0;
		private var steeringMax:Number = 1;
		private var steeringAcceleration:Number = .10;
		private var steeringFriction:Number = .98;
 
		private var velocityX:Number = 0;
		private var velocityY:Number = 0;
		private var radiance:Number = 180 / Math.PI;
 
		private var up:Boolean = false;
		private var down:Boolean = false;
		private var left:Boolean = false;
		private var right:Boolean = false;
 
		public function Car()
		{
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
 
		private function onAddedToStage(event:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
 
			init();
		}
 
		private function init():void
		{
			stage.addEventListener(Event.ENTER_FRAME, runGame);			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, myOnPress);
			stage.addEventListener(KeyboardEvent.KEY_UP, myOnRelease);
		}
 
		private function runGame(event:Event):void
		{
			if (up)
			{
				//check if below speedMax
				if (speed < speedMax)
				{
					//speed up
					speed += speedAcceleration;
					//check if above speedMax
					if (speed > speedMax)
					{
						//reset to speedMax
						speed = speedMax;
					}
				}
			}
 
			if (down)
			{
				//check if below speedMaxReverse
				if (speed > speedMaxReverse)
				{
					//speed up (in reverse)
					speed -= speedAcceleration;
					//check if above speedMaxReverse
					if (speed < speedMaxReverse)
					{
						//reset to speedMaxReverse
						speed = speedMaxReverse;
					}
				}
			}
 
			if (left)
			{
				//turn left
				steering -= steeringAcceleration;
				//check if above steeringMax
				if (steering > steeringMax)
				{
					//reset to steeringMax
					steering = steeringMax;
				}
			}
 
			if (right)
			{
				//turn right
				steering += steeringAcceleration;
				//check if above steeringMax
				if (steering < -steeringMax)
				{
					//reset to steeringMax
					steering = -steeringMax;
				}
			}
 
			// friction    
			speed *= groundFriction;
 
			// prevents drift
			if (speed < 0.01)
			{
				speed = 0;
			}
 
			// ideal changes
			var idealXMovement = speed * Math.cos(radiance);
			var idealYMovement = speed * Math.sin(radiance);
 
			// real change
			velocityX += (idealXMovement - velocityX) * groundFriction;
			velocityY += (idealYMovement - velocityY) * groundFriction;
 
			// update position
			this.x += velocityX;
			this.y += velocityY;
 
			// rotate
			radiance += (steering * speed) * .025;
			this.rotation = radiance * 180 / Math.PI;
 
			// Fix Steering (make car go straight after driver stops turning)
			steering -= (steering * 0.1);
 
		}
 
		/**
		 * Keyboard Handlers
		 */
		private function myOnPress(event:KeyboardEvent):void
		{
			switch( event.keyCode )
			{
				case Keyboard.UP:
					up = true;
					break;
 
				case Keyboard.DOWN:
					down = true;
					break;
 
				case Keyboard.LEFT:
					left = true;
					break;
 
				case Keyboard.RIGHT:
					right = true;
					break;
			}
 
			event.updateAfterEvent();
		}
 
		private function myOnRelease(event:KeyboardEvent):void
		{
			switch( event.keyCode )
			{
				case Keyboard.UP:
					up = false;
					break;
 
				case Keyboard.DOWN:
					down = false;
					break;
 
				case Keyboard.LEFT:
					left = false;
					break;
 
				case Keyboard.RIGHT:
					right = false;
					break;
			}
 
		}
 
	}
 
}
Download Fla Sample

Download Fla Sample


Tags: , , , , , , , , , , ,

3 Comments to Car Movement: Acceleration, Turning & Braking

MrMobz
July 12, 2010

I’ve looked through this code and I can’t find where it sets the vehicle’s rotation to that strange angle.

I’ve implemented this into my own game, but my vehicle is facing the wrong direction. How do I fix this?

EightySeven
July 12, 2010

If you open the FLA you will notice Car is linked to the Car class in com.freeactionscript. That means that the car instance is using the Car class located there as its base class. Thats why at the top of “Car” its ‘public class Car extends Sprite’

If you look down through the main loop of the game (Car.runGame) you will see the following lines which set the rotation of the MovieClip.
-
Keep in mind that the word ‘this’ is refering the the instance of the car on the stage.

radiance += (steering * speed) * .025;

‘radiance’, ’steering’, and ’speed’ are all declared at the top of the class. ‘radiance’ is just a variable to store the result of the calculation

this.rotation = radiance * 180 / Math.PI;

‘this.rotation’ refers to the Car sprite’s rotation. This is then changed by the caculation radiance *180/Math.PI which is really ((steering*speed*0.025)*180/Math.PI

Using this code will rotate your Car in the appropriate direction.

To speed this code up slightly you could pre-caculate Math.PI and then use the stored result instead of caculating it every frame.

static const PRECALC:Number = 180/Math.PI;

then change
this.rotation = radiance * PRECALC;

Not much of a speed boost but every bit helps

Mena__99
July 16, 2010

its a package type .. the codes are in the folders beside it

Leave a comment

You must be logged in to post a comment.