IntelliJ Java IDE
The moose likes Game Development and the fly likes Homing algorithm, make a object follow another one Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Game Development
Reply Bookmark "Homing algorithm, make a object follow another one" Watch "Homing algorithm, make a object follow another one" New topic
Author

Homing algorithm, make a object follow another one

Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
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


Code first, then think.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 13842

Yes, you want to scale all of your angles to be between -pi and +pi.
Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
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

Joined: Sep 20, 2010
Posts: 2771

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

Joined: Nov 16, 2010
Posts: 28
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

Joined: Sep 20, 2010
Posts: 2771

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

Joined: Nov 16, 2010
Posts: 28
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

Joined: Sep 20, 2010
Posts: 2771

Why don't you write several scenarios down on paper and work it out?
Kalle Zander
Greenhorn

Joined: Nov 16, 2010
Posts: 28
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.
 
 
subject: Homing algorithm, make a object follow another one
 
Threads others viewed
Rotate about origin
Collision detection with rotating shapes
3d vectors
Rotating the JLabel
Circular motion.......
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture