Home Contents Forum Links Tip Jar
Animation Math in Lingo       

Motion Along a Spline

There are a variety of functions that generate spline curves. They all use a set of points to make the curve, but differ in how they use the points. A well known type of spline is the bezier curve, which is used in this demo.

Click to add a segment to the path. Close path by clicking on start point. Points are draggable.
Spline path - source movie

global bezierObj

property sp
property eTime

on beginsprite(me)
  sp = sprite(me.spritenum)
  eTime = script("eTime").new(1000)
end

on exitframe()
  -- drive p
  if bezierObj.closed then
    p = eTime.p(#cycle)
  else
    p = eTime.p(#BF)
  end if

  -- no bias

  -- p drives anim
  sp.loch = bezierObj.x(p)
  sp.locv = bezierObj.y(p)

  doRotation(p)
end

on updateTime()
  eTime.setPeriod(1000 * (bezierObj.ptList.count - 1))
end
For full script see source movie

There are a few new developments in the way the programming is structured:

Drivers
In this demo the elapsed-time drivers are in a separate script, which is better code organization and makes them more portable. This demo uses two different types of time drivers depending on if the curve is closed or open. One cycles and the other goes back and forth between 0 and 1.

Animator Function
The bezier animator function is very different from a simple linear animator function, yet, as this demo shows, they both work in parametric animation in the same way. They take a value of p and return a value for a property or variable that is being animated, which are the two requirements for any animator function.

The bezier animator function is in its own script, for the same reasons as the time drivers (organization and portability). The variable for the bezier instance is made global because there are several scripts that alter the bezier curve, and need access to the object instance. In addition, as a global the same curve can be used by multiple objects.

Bezier takes some calculation to set up that only needs to be done once for a curve. It then provides two functions, x(p) and y(p), that return x and y coordinates for a given p, using the bezier function it has set up.

What would happen if two objects used the same bezier instance, but had different drivers or biases? Try making two objects use the same instance and see what happens.

This demo showed how another type of animator function fits into the parametric animation paradigm, and some new code organization ideas. For an overview of the bezier function, see Bezier Curves.

 
 


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.