In my game I initially had instant snap-to-new-angle turning for the enemies which worked fine. I then created a new enemy with turning that takes time to reach the direction at the player and I cannot work out why it turns the wrong way when the player passes underneath the enemy, especially when it works perfectly for all other angles???
This snippet of code in game/game.lua controls turning for missle ships which is the type of ship I'm having the bug with
if ship.shipType == "missle" then
if not ship.angle then ship.angle = math.atan2(dx,dy) end
ship.oldAngle = ship.angle
ship.idealAngle = math.atan2(dx,dy)
-- the code has no special case for turning to something beneath????
if ship.idealAngle > ship.oldAngle then
ship.angle = ship.angle + 0.1
else
ship.angle = ship.angle - 0.1
end
--ship.angle = (ship.oldAngle + adjustAngle) Uncomment this line and snap-to turning has no glitches
end
-- cap to unit circle (radius of 1)
dx, dy = -math.sin(ship.angle), -math.cos(ship.angle)
function math.lerpAngle(a, b, k)
local pi = math.pi
if (math.abs(a - b) > pi) then
if (b > a) then
a = a + pi * 2
else
b = b + pi * 2
end
end
local value = (a + ((b - a) * k))
local rangeZero = pi * 2
if (value >= 0 and value <= pi * 2) then
return value
end
return (value % rangeZero)
end
Thanks the lerping function helps doubly because I had to make the turning frame rate independent next anyway, though I also had to add a math.min(k,1) to the line assigning value to prevent some really weird movement with high dt multipliers (probably over turning).
airstruck wrote:Here's a nice short one that was posted and/or written by some combination of Jasoco and/or Davidobot (sorry guys, memory not so clear on this one).