|
||||||
| Programming Theory Discuss programming theory, algorithm efficiency, logic, and other any other category where math and computer science overlap. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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 . |
| Sponsored Links |
|
|
|
|||||
|
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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
|
|||
|
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?
|
|
|||||
|
yes.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
|
|||
|
No, I still can't figure how to do this using this method.
At the moment I'm doing it using some found code: 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 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: Code:
'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 Code:
If ndir-dir>ndir-dir2 DrawText(Sgn(ndir-dir),100,100) Else DrawText(Sgn(ndir-dir2),100,100) EndIf 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 . |
| Sponsored Links |
|
|
|
|||||
|
You can also just use the -180 to 180. If your angle > 180 rotate 360-angle in the other direction.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Quaternion to Euler rotation convertion | chemart | C# Programming | 2 | 01-13-2008 07:50 PM |
| Trapped Rainbow: New Technique To Slow Down | Kernel | Programming News | 1 | 11-26-2007 05:48 PM |
| Help please: pic angle rotation | JGreen | JavaScript and CSS | 0 | 10-11-2007 01:46 PM |
| PHP Image rotation | ShortCircuit | PHP Forum | 4 | 08-06-2006 12:45 PM |
| Slow for June | NeedHelp | Marketing | 4 | 07-05-2006 09:34 AM |
| John | ........ | 223.00000 |
| dargueta | ........ | 168.00000 |
| Xav | ........ | 164.00000 |
| LogicKills | ........ | 20.00000 |
| sam | ........ | 20.00000 |
| gaylo565 | ........ | 18.00000 |
| |pH| | ........ | 15.00000 |
| WingedPanther | ........ | 15.00000 |
| Johnnyboy | ........ | 3.00000 |
| navghost | ........ | 1.00000 |
Goal: 100,000 Posts
Complete: 67%