Jump to content
  • Advertisement
Sign in to follow this  
Bruno

moving torwards a point

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

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:

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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!