Home Contents Forum Links Tip Jar
Animation Math in Lingo       

The Round-Off Effect

When either the vertical or horizontal position of a sprite is set in Director, it is automatically rounded to the nearest integer. If sp.locv is set to 10.4, its value will be rounded to 10.

The result of this is that the common statement to increment a sprite's position:

sp.locv = sp.locv + velocity    (or loch)

will allow only integral velocities. If velocity is set to 1.4, the resulting velocity on screen will actually be 1 px/frame because the .4 gets rounded off every time. This is most noticeable when accelerating at speeds close to zero.

Round-off effect - source movie

Notice how the upper circle jerks from 1 to 2 to 3 px/frame as it accelerates. The solution is to use horizontal and vertical position variables which are initialized to the sprite position. Those are then incremented during the animation, and loch and locv are set from those. The thing to avoid is using loch or locv in the calculation of the next loch or locv. The idea is shown in simple form here for loch:

Round-off Effect: Solution:
property sp

on beginsprite(me)
  sp = sprite(me.spritenum)
end

on exitframe()
  sp.loch = sp.loch + 1.5
end

property sp
property x  --horizontal loc

on beginsprite(me)
  sp = sprite(me.spritenum)
  x = sp.loch --initialize
end

on exitframe()
  x = x + 1.5 --increment x
  sp.loch = x --set property
end

The script on the left moves a sprite 4 pixels in the first 2 frames. The script on the right moves it 3 pixels in the first 2 frames which is the desired velocity of 1.5 px/frame.

This concept applies to any property that Director automatically rounds off, which includes loch, locv, width, height, and others.

Parametric animation does not rely on previous positions to calculate new ones, and so is not affected significantly by round-off.

 
 


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.