PathFinding Rotation

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 on other sites
angle = tan(vForward.x/vForward.y)

I think, that's just off the top of my head.

Share on other sites
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 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 on other sites
I think this question is better off in "Maths & Physics"...

Cheers,
Jack

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 on other sites
Thanks jyk, i didnt understand the which positions ET3D was comparing but i have done it now.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628379
• Total Posts
2982344

• 10
• 9
• 15
• 24
• 11