[SOLVED] Help with rotation/maths issue

In my game I have a number of enemies who follow a critter. What I would like is for the enemies to auto rotate so that they are always facing the critter.
I work out the angle towards the critter in order for the enemies to move in a direct line by using the math.atan2 function and this works perfectly. I then use the same angle to work out how much to rotate the enemy by subtracting the old angle from the new one and using that as the new angle delta.
However, the enemies will not rotate at all. I can’t see any reason why this would be so.

Here’s my code for updating the enemies…
[lua]function updateEnemy()
for f=1, #enemies do
angle= math.atan2(critter.y - enemies[f].y, critter.x - enemies[f].x) – work out angle between enemy and critter
enemies[f].x = enemies[f].x + (math.cos(angle) * enemies[f].speed) – update x pos in relation to angle
enemies[f].y = enemies[f].y + (math.sin(angle) * enemies[f].speed) – update y pos in relation to angle
if (enemies[f].spec == 1) or (enemies[f].spec == 2) then – Species 1 & 2 rotate constantly
enemies[f]:rotate(5)
end
if (enemies[f].spec == 3) or (enemies[f].spec == 4) then – Species 3 & 4 rotate to face the critter
angledelta = angle - enemies[f].oldangle – How many degrees to rotate the enemy
enemies[f]:rotate(angledelta) – Rotate enemy
enemies[f].oldangle=angle – Store the angle for next use
end
end

end [/lua]

Any help at all would be very very appreciated.
Cheers [import]uid: 7841 topic_id: 14439 reply_id: 314439[/import]

Just a thought:
Wouldn’t it be easier to find the angle between the enemy and the critter and set the rotation with
enemies[f].rotation
…instead of using enemies[f].rotate and delta angles?
[import]uid: 13632 topic_id: 14439 reply_id: 53426[/import]

the angel you got from atan2 is in radians, you have to convert it to degrees, by multiplying it with (180 / math.pi) [import]uid: 46529 topic_id: 14439 reply_id: 53443[/import]

Cheers guys.
@ojnab - That was the first thing I tried :slight_smile:
@culutas - Due to the fact that I’m allergic to maths I have no idea at all what a radian is, but it worked perfectly. Thank you so very very much. +1 respect :slight_smile: [import]uid: 7841 topic_id: 14439 reply_id: 53529[/import]