Yea, that is something which always proved to be one of the worst nightmares of me. An algorithm to calculate slow rotation of something.
For example, I have a turret in the center of the screen. This turret follows the mouse.
One way to do it is to set the rotation of turret to always be Atan2 between their position. It solves the problem, but the turret ALWAYS faces the right direction, so it is quite unrealistic (and bad looking if you ask me).
-Static - the turret always turns either right or left trying to face the mouse. It has fixed turn direction, so once it turns right it can't turn left and vice versa. Let's leave this with silence.
-Fluid - Turret turns either right or left always choosing the closer direction. Now that's the thing I'm talking about. (I think I like to write a lot).
How to code this? And even better - what is the most efficient way to do this? Considering my angle range is from 180 to -180, where right is 0, up 90, left 180 and down -90 degrees.
And also a lil' explanation would be nice - I like to understand what I write ^^.
Thanks in advance :).
Slow rotation towards target
Started by Maurice_Z, Dec 28 2007 04:25 AM
5 replies to this topic
#1
Posted 28 December 2007 - 04:25 AM
|
|
|
#2
Posted 28 December 2007 - 09:10 AM
Instead of simply looking at an angle range of -180 to 180, try looking at -360 to 360, then convert to the -180 to 180 range. Have the mouse position in the -180 to 180 range and compare it's position with BOTH turret positions in the -360 to 360 range. This should tell you whether to rotate CW or CCW.
#3
Posted 28 December 2007 - 10:28 AM
But if I have range -360 to 360 then 0 is also equal to 360, 90 is equal to -270, etc. So you mean I should use two different variables, one indicating negative angle, and one positive value?
#4
Posted 31 December 2007 - 09:38 AM
#5
Posted 01 January 2008 - 06:40 AM
No, I still can't figure how to do this using this method.
At the moment I'm doing it using some found code:
As I said I tried to write the code you mentioned, but hell, I really can't figure out anything to do with it. You say I have to make it range from -360 to 360, so I need two variables:
But it doesn't work. I'm pretty sure the code should look more or less like this, but I can't figure out what's wrong :).
At the moment I'm doing it using some found code:
Local nDir:Float=ATan2(Player.Y+5-Y,Player.X+5-X) nDir=(nDir+360-Dir) mod 360; If nDir<=180 nDir:*0.05 Else nDir=(nDir-360)*0.05 EndIf dir:+nDirBut I'd like something faster (If something faster exists).
As I said I tried to write the code you mentioned, but hell, I really can't figure out anything to do with it. You say I have to make it range from -360 to 360, so I need two variables:
'dir is current Direction 'Sgn is of course Sign function dir2=360*Sgn(-dir)+dir 'Ignore the situation when dir=0 ndir=ATan2(MouseY-Y,MouseX-X) 'range is -180 to 180Now I tried to work a bit on it. I have two directions variables, so I wrote this code:
If ndir-dir>ndir-dir2 DrawText(Sgn(ndir-dir),100,100) Else DrawText(Sgn(ndir-dir2),100,100) EndIfSo it should draw 1 when I have to turn clockwise, -1 if counter clockwise.
But it doesn't work. I'm pretty sure the code should look more or less like this, but I can't figure out what's wrong :).
#6
Posted 02 January 2008 - 09:00 AM
You can also just use the -180 to 180. If your angle > 180 rotate 360-angle in the other direction.


Sign In
Create Account


Back to top









