moving torwards a point

Started by
3 comments, last by Bruno 14 years, 7 months ago
Hi, Please take a look at the following image : http://img190.imageshack.us/img190/7079/moves.jpg 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
Advertisement
The simplest way is to limit the turn rate of the unit, and then turn towards the target while moving. This will create a natural curve, as you can't instantly turn to face the target.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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.

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

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:

x+=speed*cos(unit_dir);
y+=speed*sin(unit_dir);

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.
Thanks guys :)
You were very helpfull, it made me see the "light", got it all working :)

This topic is closed to new replies.

Advertisement