programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Homing algorithm, make a object follow another one

Kalle Zander
Greenhorn
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.

Paul Clapham
Sheriff
Posts: 22374
42
Yes, you want to scale all of your angles to be between -pi and +pi.

Kalle Zander
Greenhorn
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
Saloon Keeper
Posts: 7722
142
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).

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
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).

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
Saloon Keeper
Posts: 7722
142
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
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
Saloon Keeper
Posts: 7722
142
Why don't you write several scenarios down on paper and work it out?

Kalle Zander
Greenhorn
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.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.