Home Contents Forum Links Tip Jar
Animation Math in Lingo       

Combining Motions Dependently

A secondary motion (offset) can be made dependent on the primary motion in various ways. In this demo, the sine offset is made perpendicular to the linear motion and scaled according to the length of the linear path. This gives the object's path a consistent shape.

To make the offset perpendicular, it is given x and y components that are dependent on the angle of the linear motion.

At this point it is helpful to start using the term "vector" (see Vectors).

The math involves a few steps:

1. Find the unit vector that is perpendicular to the linear path.
2. Multiply the unit vector by offset(p)
3. Use x and y of the resulting vector as the x and y offsets.

There are several ways to go about finding the unit vector.

In this demo, it is found using mathematics described in Sine & Cosine Definitions. First, the angle of the linear path is found using atan(). It is then rotated 90°. Then the sin() and cos() of this angle give the x and y components of the perpendicular unit vector. ( [sin t, cos t] is always a unit vector)

Click to move the dot
Perpendicular Offset - source movie - vector version

on exitframe()
  -- drive p
  p = eTimeOnce(2000) -- 2 seconds

  --no bias

  -- p drives animation
  lineLength = sqrt(power(dx,2) + power(dy, 2))
  offset = sine(p, sineCycles, lineLength/8.0)

  sp.loch = xLinear(p) + offset * xPerp
  sp.locv = yLinear(p) + offset * yPerp
  spBlack.loch = xLinear(p)
  spBlack.locv = yLinear(p)
end

on moveTo(me, pt)
  -- set start, end
  startX = sp.loch
  startY = sp.locv
  endX = pt.loch
  endY = pt.locv

  -- start timer
  millis = the milliseconds

  -- set unit vector
  dx = endX - startX
  dy = endY - startY
  angle = atan(dy/dx) + pi * (1 - dx/abs(dx)) / 2.0
  angle = angle - pi/2.0
  xPerp = cos(angle)
  yPerp = sin(angle)
end
See source movie for full script

Since the unit vector is the same for the whole path, it is calculated once and then used for all the offsets.

The size of the offset is made dependent on the length of the linear motion by scaling it according to the length. Linear length is calculated using the pythagorean theorem, hyp = sqrt(x2 + y2).

•How would you make the duration of the animation depend on the linear length?
•How would you keep the offset parallel to the linear motion?

Extra for philomaths:
Linear algebra is very useful in working with vectors. Rotating a vector 90° can

be done with the matrix multiplication . This is the method used in the vector version of the demo.

 
 


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.