Jump to content
  • Advertisement
Sign in to follow this  
michaelbeirne

PathFinding Rotation

This topic is 4440 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

Im using AStar to move units in an rts game. How do i make the units rotate to the correct angle of the destination tile? I presume i use the cross product of the destination tile and the forward vector of the unit. But which way does the tiles vector have to point? So if the Unit is moving up or down, do i compair the units forward vector to the next tiles forward vector, and if im moving left or right compare the units left vector to the nect tiles left vector? If thats not how you do it, any other suggestions are helpful. Thanks. [Edited by - michaelbeirne on April 22, 2006 11:08:31 AM]

Share this post


Link to post
Share on other sites
Advertisement
Evil Steve means 'atan'.

Assuming your character moves from (x1,y1) to (x2,y2), the movement vector is (x2-x1, y2-y1). Normalising this gives you (cos a, sin a), which you can plug into a matrix to make a rotation matrix directly.

Alternately you can get the angle from it, which is usually a good idea as you don't want to change direction immediately, but instead rotate over time.

Share this post


Link to post
Share on other sites
Ok , ihave been reading up on vectors and i now know how to normalize them, get dot and cross products etc but i still dont get how to apply it to this rotation.


pathfinder->ReadPath(unit->ID,unit->unitSprite->getXPos(),unit->unitSprite->getYPos(),unit->speed);
float endX = pathfinder->xPath[unit->ID];
float endY = pathfinder->yPath[unit->ID];
D3DXVECTOR3 point1 = D3DXVECTOR3(unit->unitSprite->getXPos(), unit->unitSprite->getYPos(), 0.0f);
D3DXVECTOR3 point2 = D3DXVECTOR3(endX, endY, 0.0f);
D3DXVECTOR3 distance = D3DXVECTOR3(point2.x-point1.x, point2.y-point1.y, 0.0f);
float maginitude = D3DXVec3Length(&distance);
D3DXVECTOR3 u;
D3DXVECTOR3 v;
D3DXVec3Normalize(&u,&distance);
D3DXVec3Normalize(&v,&distance);
float dot = D3DXVec3Dot(&u, &v);




Point1 is where the unit starts, Point2 is the next tile.

How exactly do i get the angle?

Thanks.

Share this post


Link to post
Share on other sites
As has been mentioned, you can construct a matrix directly given the current and goal positions, but it can also be useful to have the angle available. For this, the preferred method is:
angle = atan2(goal.y - position.y, goal.x - position.x);
Remember that atan2() will return the angle in radians, in the range [-pi, pi], and with a rotation of zero considered to be pointing along the positive x axis. If your conventions are at all different you'll have to adjust as necessary, but it shouldn't be a problem.

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!