# moving torwards a point

This topic is 3375 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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.

##### 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?

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 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:

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.

##### Share on other sites
Thanks guys :)
You were very helpfull, it made me see the "light", got it all working :)

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633682
• Total Posts
3013306
×