Sign in to follow this  
michaelbeirne

PathFinding Rotation

Recommended Posts

michaelbeirne    122
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
ET3D    810
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
michaelbeirne    122
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
jyk    2094
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

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