Jump to content

Slow rotation towards target

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Maurice_Z

Maurice_Z

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
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 :).

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Maurice_Z

Maurice_Z

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
yes.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Maurice_Z

Maurice_Z

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
No, I still can't figure how to do this using this method.
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:+nDir
But 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 180
Now 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)
EndIf
So 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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You can also just use the -180 to 180. If your angle > 180 rotate 360-angle in the other direction.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog