Home Contents Forum Links Tip Jar
Animation Math in Lingo       

Direction Of Velocity Using Sine & Cosine

In this demo, the velocity of the object is specified by variables for direction and speed rather than x and y components. The direction is a value for t, as described in the definition of sine and cosine. When it comes time to move the object, x and y velocity components are found by cos(t) and sin(t), respectively. Programming it this way makes it easier to "steer" the object.

Since a point (cos(t), sin(t)) is a distance of 1 from the origin, moving x by cos(t) and y by sin(t) will move the object a distance of 1. If the components are multiplied by speed, the object will move a distance of speed.

Steer with left/right keys, gas with ctrl key
Direction of velocity - source movie

property sp
property
x, y
property tCar

on beginsprite(me)
  sp = sprite(me.spritenum)
  x = sp.loch --initialize position
  y = sp.locv
  tCar = 0 --car starts pointed to right
end

on exitframe()

  --key input
  k = the keypressed
  case chartonum(k) of
    28: tCar = tCar +.1 --left key (steer left)
    29: tCar = tCar -.1 --right key (steer right)
  end case
  speed = 0
  if the controlDown then speed = 3 --control key (gas)
  
  --set velocity, increment position
  xVelo = cos(tCar) * speed
  yVelo = -sin(tCar) * speed
  x = x + xVelo
  y = y + yVelo

  --set sprite properties
  sp.loch = x
  sp.locv = y
  sp.rotation = -tCar * 180 / pi --convert to degrees
end

How is the car steered in terms of the variables in the program?

Why is yVelo set to negative sine? The unit circle is drawn on a standard coordinate system, with y getting larger upward. The y coordinates get flipped upside-down on the Director stage since locv gets larger downward. Taking negative sine compensates for this.

Similarly, why is sprite rotation set to negative tCar? A sprite turns clockwise as its rotation property increases, while in the definition of sine and cosine, t increases in a counter-clockwise direction.

Another way to compensate for both these is to write the math as if t gets larger going clockwise around the unit circle, rather than counter-clockwise. So that steering left means subtracting from t, and steering right means adding.

Because the direction t=0 points to the right, the car cast member was created pointing to the right. This makes it possible to use t to set the sprite rotation without having to shift it. For example, if the car was created pointing upward, 90 degrees (or pi/2 radians) would have to be added when setting the sprite rotation property. Otherwise the car would appear to travel sideways.

•Why don't xVelo, yVelo, or speed need to be declared as properties or initialized in this program? How would you make the car accelerate smoothly, instead of starting and stopping suddenly?

 
 


Copyright © 2003 JM Harward 
 jmckell~at~jmckell~dot~com
All lingo provided on this site may be used freely for educational purposes. Not for redistribution as uncompiled code. Instructional materials may not be reproduced without permission.