PathFinding Rotation

Started by
5 comments, last by michaelbeirne 17 years, 11 months ago
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]
Advertisement
angle = tan(vForward.x/vForward.y)

I think, that's just off the top of my head.
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.
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.
I think this question is better off in "Maths & Physics"...

Cheers,
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

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

This topic is closed to new replies.

Advertisement