Sign in to follow this  

moving torwards a point

Recommended Posts

Hi, Please take a look at the following image : I'm tryng to make my units move like in that pic, however i have no clue where to start. If my unit is facing the black direction line, she would move in a curve from point A to point B. If my unit is already facing point A, she wouldn't bother making that curve, and would just go straight. How can i implement this ? I think i would need to find if i'm facing the point, and if not, i would need some kind of curve ? Any clues ? thanks, Bruno

Share this post

Link to post
Share on other sites
The further questions you must ask are,

a) can you turn in place? (i.e. must you be moving to turn)
b) do you have various speeds you can go?
c) does your turn rate vary based on your speed?

However, the general solution is what was mentioned above. Determine which way you need to turn, move forward (if necessary) while changing your facing direction gradually in the direction you need to turn. Continue doing this until your facing direction = the desired direction. The good news about this is that you will continue to turn if the target moves.

For info on this and more, check out Craig Reynolds site. His code is java, but you will understand it.

Share this post

Link to post
Share on other sites
Well... To find the direction of least angle change use the cross product:

A = (x_target-x_unit,y_target-y_unit) x ( cos(unit_dir), sin(unit_dir) )

The value A will be positive when the unit must turn clockwise, and negative to turn counterclockwise. If the unit is facing toward the target, then it will produce A=0.

Thus, you could do something like:

unit_dir -= A*k; //with k being some proportionality constant

To advance in the direction of theunit's current heading use simple trig:


Shabam your done. However you may encounter a few problems with the cross product. There are a few special conditions you need to handle to assure that everything works out... they are pretty rare so I leave them alone for now.

Good luck.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this