# Learn About Linear Kinematics

Read Time: 9 min

Picturing animation in terms of vectors is intuitive, but understanding vector mathematics is a pain. In this tutorial, I hope to ease that pain and provide a solution to animation problems using a custom written Vector2D class. We will look at some fundamental concepts of linear kinematics in the Eulerian approach: displacement, velocity and acceleration. Then, we'll build a simple application with it.

## Final Result Preview

Let's take a look at the final result we will be working towards. Click on the Flash panel below and control the arrowhead by pressing the four directional keys.

## Step 1: Vector Quantity

All vector quantities have two components: magnitude and direction.

## Step 2: Change in Vector Quantity

A change in vector quantities refers to one of these cases:

1. Change in direction
2. Change in magnitude
3. Change in both magnitude and direction

## Step 3: Displacement as a Vector Quantity

Displacement, velocity and acceleration are vector quantities. Their definitions are as follows:

• Displacement - Vector of the shortest path pointing from origin to destination. I define origin as point (0, 0) and destination as the particle's location relative to this point. Basically, it refers to the Cartesian Coordinate system as implemented by Flash.
• Velocity - Velocity is the change of displacement over time.
• Acceleration - Acceleration is the change of velocity over time.

The animation below shows displacement as we are going to implement in Flash later.

## Step 4: Velocity as a Vector Quantity

Velocity is illustrated by the animation below. Note velocity is constant, which means acceleration is absent in this scenario. If velocity is zero, displacement will remain constant throughout.

## Step 5: Acceleration as a Vector Quantity

Acceleration is illustrated by the animation below. Note: kinematics implies constant acceleration. If acceleration changes over time, it falls under the topic of dynamics. Dynamics is the study of forces that cause of acceleration to vary over time. One such force is gravity, and I've written a post on animating that.

## Step 6: Start Building a Projectile

Now that you have gotten a brief understanding of linear kinematics quantities and able to related them to vectors, we can start building our Projectile class. We would like the projectile be able to capture all these quantities: displacement, velocity and acceleration - so that it can be manipulated on each frame.

Below is the data we shall record in our Projectile class:

 1 2 private var displace:Vector2D;  3 private var velo:Vector2D;  4 private var acc:Vector2D; 

## Step 7: Initialize Projectile

Upon initiation of this Projectile class, we shall initialise the mentioned variables and draw its graphical representation.

 1 2 public function Projectile()  3 {  4  //draw graphics  5  this.draw();  6 7  //init all vector quantities  8  displace = new Vector2D(this.x, this.y);  9  velo = new Vector2D(0, 0);  10  acc = new Vector2D(0, 0);  11 }  12 13 protected function draw():void  14 {  15  //drawing the arrowhead  16  var height:Number = 30;  17  var width:Number = 60;  18  graphics.beginFill(0x0000FF);  19  graphics.moveTo(0, 0);  20  graphics.lineTo(width / -3, height / -2);  21  graphics.lineTo(width / 2, 0);  22  graphics.lineTo(width / -3, height / 2);  23  graphics.lineTo(0, 0);  24  graphics.endFill();  25 } 

## Step 8: Accessors of Vector Quantities

The following are accessors of our private variables - displace, velo,  acc - in the Projectile class.

 1 2 public function setDisp(mag:Number, angle:Number):void  3 {  4  displace.redefine(mag, angle);  5 }  6 7 public function getDisp():Vector2D  8 {  9  return displace;  10 }  11 12 public function setVelo(mag:Number, angle:Number):void  13 {  14  velo.redefine(mag, angle);  15 }  16 17 public function getVelo():Vector2D  18 {  19  return velo;  20 }  21 22 public function setAcc(mag:Number, angle:Number):void  23 {  24  acc.redefine(mag, angle);  25 }  26 27 public function getAcc():Vector2D  28 {  29  return acc  30 } 

## Step 9: Updaters of Vector Quantities

Upon refreshing every frame, we need to update velocity (using acceleration) and update displacement (using the said velocity). This can be achieved using the following functions. For a thorough explanation on Vector addition, do visit this great post from Daniel Sidhon.

 1 2 public function applyVelo():void  3 {  4  this.displace = this.displace.add(velo);  5 }  6 7 public function applyAcc():void  8 {  9  this.velo = this.velo.add(acc);  10 }  11 12 //update sprite's position by displacement.  13 public function animate():void  14 {  15  this.x = this.displace.x;  16  this.y = this.displace.y;  17 } 

## Step 10: Updater for Sprite'S Orientation

We will also need to update the orientation of the Sprite. This can be achieved through the rotation property of Sprite.

 1 2 public function orient():void  3 {  4  this.rotation = Math2.degreeOf(velo.getAngle());  5 } 

I have also implemented a Math2 static class, in which I've written a function to easily convert back and forth from the angle's units of degrees and radians.

 1 2 public static function radianOf (deg:Number):Number  3 {  4  return deg/180*Math.PI;  5 }  6 7 public static function degreeOf (rad:Number):Number  8 {  9  return rad/Math.PI*180;  10 } 

## Step 11: The Main Class

Now that we have established our Projectile and Math2 class, we can start to code our Main class. We will need a Vector2D class as well although thorough explanation is not included due to the aforementioned article on Vectors by Daniel Sidhon. I assume readers understand the Vector2D class after reading it. However, if clarifications are needed, do prompt me with your queries.

First of all, we need to know private variables of this class.

 1 2 private var b1:Projectile;  3 4 //keypress flags  5 private var UP:Boolean = false;  6 private var DOWN:Boolean = false;  7 private var LEFT:Boolean = false;  8 private var RIGHT:Boolean = false; 

## Step 12: Initializing Main

Upon initialization of Main, function init will be launched. This function will create a new Projectile and set its initial velocity. Then, listeners to events will be assigned.

 1 2 private function init(e:Event = null):void  3 {  4  removeEventListener(Event.ADDED_TO_STAGE, init);  5  // entry point  6 7  b1 = new Projectile();  8  stage.addChild(b1);  9 10  //setting initial velocity  11  b1.setVelo(5, Math2.radianOf(30));  12 13  //setting event listeners  14  b1.addEventListener(Event.ENTER_FRAME, proj_enterFrame);  15  stage.addEventListener(KeyboardEvent.KEY_DOWN, handle_keyDown);  16  stage.addEventListener(KeyboardEvent.KEY_UP, handle_keyUp);  17 } 

## Step 13: Keyboard Event Listeners

I have defined user control as keypresses of Up, Left, Down and Left arrow keys. Upon pressing and releasing those keys, flag variables of Main (Step 11) will be turned true and false. Based on these flags, the Vector quantities will be manipulated on every frame. Note as well I have divided controls into horizontal and vertical axis manipulators.

 1 2 private function handle_keyDown(e:KeyboardEvent):void  3 {  4  if (e.keyCode == Keyboard.UP) UP = true;  5  else if (e.keyCode == Keyboard.DOWN) DOWN = true;  6 7  if (e.keyCode == Keyboard.LEFT) LEFT = true;  8  else if (e.keyCode == Keyboard.RIGHT) RIGHT = true;  9 }  10 11 private function handle_keyUp(e:KeyboardEvent):void  12 {  13  if (e.keyCode == Keyboard.UP) UP = false;  14  else if (e.keyCode == Keyboard.DOWN) DOWN = false;  15 16  if (e.keyCode == Keyboard.LEFT) LEFT = false;  17  else if (e.keyCode == Keyboard.RIGHT) RIGHT = false;  18 } 

## Step 14: EnterFrame Event Listeners

Upon refresh of each frame the following code will be executed. It is long, but don't worry; just read on.

 1 2 private function proj_enterFrame(e:Event):void  3 {  4  //define acceleration  5  var accMag:Number = 0.1;  6  if (UP) {  7  b1.setAcc(accMag, Math2.radianOf(-90));  8  b1.applyAcc();  9  }  10 11  else if (DOWN) {  12  b1.setAcc(accMag, Math2.radianOf(90));  13  b1.applyAcc();  14  }  15 16  if (LEFT) {  17  b1.setAcc(accMag, Math2.radianOf(180));  18  b1.applyAcc();  19  }  20 21  else if (RIGHT) {  22  b1.setAcc(accMag, Math2.radianOf(0));  23  b1.applyAcc();  24  }  25 26  //decelerate as nothng is pressed to simulate friction.  27  if (UP + DOWN + LEFT + RIGHT == 0) {  28  var currentVeloMag:Number = b1.getVelo().getMagnitude();  29  var currentVeloAng:Number = b1.getVelo().getAngle();  30 31  if(currentVeloMag > 1){  32  b1.setAcc(accMag * -1, currentVeloAng);  33  b1.applyAcc();  34  }  35  }  36 37  b1.applyVelo();  38 39  //restricting sprite to borders of the stage  40  b1.getDisp().x = Math2.implementBound(0, stage.stageWidth, b1.getDisp().x);  41  b1.getDisp().y = Math2.implementBound(0, stage.stageHeight, b1.getDisp().y);  42 43  b1.animate();  44  b1.orient();  45 } 

## Step 15: Update Motion

Updating the motion should be done in the following order:

1. Define new acceleration according to user keypress.
2. Using acceleration, update current velocity.
3. Using current velocity, update current displacement.
4. Refine displacement to keep object inside boundaries.

I've highlighted the codes for easy identification of these steps.

 1 2 private function proj_enterFrame(e:Event):void  3 {  4  //define acceleration  5  var accMag:Number = 0.1;  6  if (UP) {  7  b1.setAcc(accMag, Math2.radianOf(-90));  8  b1.applyAcc();  9  }  10 11  else if (DOWN) {  12  b1.setAcc(accMag, Math2.radianOf(90));  13  b1.applyAcc();  14  }  15 16  if (LEFT) {  17  b1.setAcc(accMag, Math2.radianOf(180));  18  b1.applyAcc();  19  }  20 21  else if (RIGHT) {  22  b1.setAcc(accMag, Math2.radianOf(0));  23  b1.applyAcc();  24  }  25 26  //decelerate as nothing is pressed to simulate friction.  27  if (UP + DOWN + LEFT + RIGHT == 0) {  28  var currentVeloMag:Number = b1.getVelo().getMagnitude();  29  var currentVeloAng:Number = b1.getVelo().getAngle();  30 31  if(currentVeloMag > 1){  32  b1.setAcc(accMag * -1, currentVeloAng);  33  b1.applyAcc();  34  }  35  }  36 37  b1.applyVelo();  38 39  //restricting sprite to borders of the stage  40  b1.getDisp().x = Math2.implementBound(0, stage.stageWidth, b1.getDisp().x);  41  b1.getDisp().y = Math2.implementBound(0, stage.stageHeight, b1.getDisp().y);  42 43  b1.animate();  44  b1.orient();  45 } 

## Step 16: Slowing Down Motion

You may find that there are other functions slotted in between these highlighted codes. What are they? One is to apply another vector to slow down our projectile as the user does not press on any keys. This is applied before we add velocity to our displacement.

 1 2 //decelerate as nothng is pressed to simulate friction.  3 if (UP + DOWN + LEFT + RIGHT == 0) {  4  var currentVeloMag:Number = b1.getVelo().getMagnitude();  5  var currentVeloAng:Number = b1.getVelo().getAngle();  6 7  if(currentVeloMag > 1){  8  b1.setAcc(accMag * -1, currentVeloAng);  9  b1.applyAcc();  10  }  11 } 

## Step 17: Stay Inside

The next one is to restrict our projectile to always stay on the stage, otherwise it will fly out of the screen. Again, implementBound is a function I've included in the Math2 static class. Given an upper bound, lower bound and a random value, implementBound will return a value that is within the boundaries.

After applying this constraints onto our displacement (and only after that), we update the Sprite's position with this displacment value.

 1 2 //restricting sprite to borders of the stage  3 b1.getDisp().x = Math2.implementBound(0, stage.stageWidth, b1.getDisp().x);  4 b1.getDisp().y = Math2.implementBound(0, stage.stageHeight, b1.getDisp().y); 

## Step 18: Orient Sprite

Before we leave this sprite as it is, we should orient it so that it always points in the position it's heading using function orient.

## Step 19: Get Set and Go!

Now everything is set to go. As you launch this piece by pressing on Ctrl + Enter, you will see an arrow that gradually slows down as it heads diagonally down the screen. Press on the four directional keys to move the arrow about. Don't worry about losing your arrow; it'll stay inside your view.

## Conclusion

This article should get you familiar with using vectors to animate motion. Once you have understood kinematics, do proceed to read up on my post on dynamics. Let me know how it goes. Terima Kasih.