Lost Password?

Go Back   CodeCall Programming Forum > Software Development > General Programming > Programming Theory

Programming Theory Discuss programming theory, algorithm efficiency, logic, and other any other category where math and computer science overlap.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-28-2007, 06:25 AM
Maurice_Z Maurice_Z is offline
Learning Programmer
 
Join Date: Nov 2007
Location: Poland
Posts: 35
Rep Power: 3
Maurice_Z is on a distinguished road
Send a message via ICQ to Maurice_Z Send a message via AIM to Maurice_Z Send a message via MSN to Maurice_Z Send a message via Yahoo to Maurice_Z
Question Slow rotation towards target

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 .
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 12-28-2007, 11:10 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,050
Last Blog:
wxWidgets is NOT code ...
Rep Power: 24
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-28-2007, 12:28 PM
Maurice_Z Maurice_Z is offline
Learning Programmer
 
Join Date: Nov 2007
Location: Poland
Posts: 35
Rep Power: 3
Maurice_Z is on a distinguished road
Send a message via ICQ to Maurice_Z Send a message via AIM to Maurice_Z Send a message via MSN to Maurice_Z Send a message via Yahoo to Maurice_Z
Default

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-31-2007, 11:38 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,050
Last Blog:
wxWidgets is NOT code ...
Rep Power: 24
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

yes.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-01-2008, 08:40 AM
Maurice_Z Maurice_Z is offline
Learning Programmer
 
Join Date: Nov 2007
Location: Poland
Posts: 35
Rep Power: 3
Maurice_Z is on a distinguished road
Send a message via ICQ to Maurice_Z Send a message via AIM to Maurice_Z Send a message via MSN to Maurice_Z Send a message via Yahoo to Maurice_Z
Default

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
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:
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
Now I tried to work a bit on it. I have two directions variables, so I wrote this code:
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 .
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 01-02-2008, 11:00 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,050
Last Blog:
wxWidgets is NOT code ...
Rep Power: 24
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT -5. The time now is 11:25 PM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 67%

Ads