Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Homing algorithm, make a object follow another one

 
Kalle Zander
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far I managed to put up this:


What's missing (I think or if not more) is that the follower need to know what's the shortest way to the target, i.e left rotating or right rotating. Now if angleToTarget is 350 degrees and rotationAngle (the followers current angle) is 0 degrees, it will turn all the way around instead of just backing -10 degrees. I think I need to put in a if-statement to check this, something like (rotationAngle-angletoTarget) > 180. I tried to search but couldn't find a understandable solution on this.

Thanks in advance, //Kalle
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you want to scale all of your angles to be between -pi and +pi.
 
Kalle Zander
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Yes, you want to scale all of your angles to be between -pi and +pi.


I think I see where you are heading. A hint on how to implement this? Something like this?



 
Stephan van Hulst
Bartender
Pie
Posts: 5810
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just calculate how much your angle has to increase (in one direction) in order to get to the angle you want. Then, if the angle is greater than Pi, you only have to decrease the angle by 2*Pi (full circle).

[edit]

Essentially, what you wrote, minus the else clause (because the rotation angle should never be below 0). Also, you have to deduct by 2*Pi, instead of Pi.
 
Kalle Zander
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Just calculate how much your angle has to increase (in one direction) in order to get to the angle you want. Then, if the angle is greater than Pi, you only have to decrease the angle by 2*Pi (full circle).

[edit]

Essentially, what you wrote, minus the else clause (because the rotation angle should never be below 0). Also, you have to deduct by 2*Pi, instead of Pi.


Ah, ok. Yes I saw now the 2*PI mistake by the way too.
 
Stephan van Hulst
Bartender
Pie
Posts: 5810
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you shouldn't have 2*Pi in the while clause. If the angle is greater than 180 degrees, you have to deduct 360 degrees, see?
 
Kalle Zander
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:No, you shouldn't have 2*Pi in the while clause. If the angle is greater than 180 degrees, you have to deduct 360 degrees, see?


typo, changed it.

But okay where are we now?

rotationAngleToTarget-rotationAngle > Math.PI

Then don't go the long way around?
 
Stephan van Hulst
Bartender
Pie
Posts: 5810
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you write several scenarios down on paper and work it out?
 
Kalle Zander
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Why don't you write several scenarios down on paper and work it out?


Hehe, I'm not very clever. But in fact did I eventually come up with this after turning on my brain:

Works alright.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic