Rotateto2

An improved version of the default rotateTo() function. Allows for scaled rotation amount based on the difference in angles, and fixes a slight overlook that caused angles that weren't close to each other to immediately set.

rotateTo2(angle, speed, [scaling])

- Like rotateTo(), angle is the angle you want to rotate the object to and speed is how much to turn each step.
- Scaling is optional. If omitted or set to 0 the object will turn regularly. Recommended values are between 1 and 9. 1 is practically unnoticeable, while 18 will have you lined up almost instantly. Its function is to rotate faster if there is a great difference in the current and declared angle, and if they are somewhat lined up the rotation levels off. The current formula for scaled rotation speed is shown below
speed/scaling + difference/(180/scaling)

```
function rotateTo2(angle,delta,amp)
local dl=0
local dr=0
local dif = 0
local ret=false
dl=angle-getAngle()
if amp==nil then amp = 0 end
if amp>0 then
if math.abs(dl) < 360-math.abs(dl) then
dif = math.abs(dl)
else
dif = 360-math.abs(dl)
end
delta = delta/amp + dif/(180/amp)
end
if dl<0 then
dl=0-dl
dr=360-dl
if (dl>dr and dr<=delta and dr>=-delta) or (dl<dr and dl<=delta and dl>=-delta) then
setAngle(angle)
ret=true
else
if dr<dl then
turn(delta)
else
turn(-delta)
end
end
else
dr=360-dl
if (dl>dr and dr<=delta and dr>=-delta) or (dl<dr and dl<=delta and dl>=-delta) then
setAngle(angle)
ret=true
else
if dr>dl then
turn(delta)
else
turn(-delta)
end
end
end
return ret
end
```

page revision: 1, last edited: 13 May 2007 03:04