# Calculating Cross Product when using Unit Vector

## Recommended Posts

markgame66    132
Hi all, Thanks to sergamer1 in thread: http://www.gamedev.net/community/forums/topic.asp?topic_id=475134&whichpage=1� I'm calculating the initial angle correctly. However I'm not having much luck with getting the sign correct. I'm using the method described in thread: http://www.gamedev.net/community/forums/topic.asp?topic_id=474020&whichpage=1� The sign doesn't always work out as I expect. What I'm trying to do it this: if the difference between the actual path and calculated path drifts my more that 5 degrees then correct the agents orientation. Some times this works. An example of where it goes wrong is: If Actual angle = 52 degrees and required angle is 45 degrees then turn left (assuming a '-' from the x-product). The correction angle is always correct - i.e. 7 degrees, in this case. However the sign is not. Agent is at pos (2,2) and the next path cell is (3, 3). Therefore start vector is (1, 1). This is the code - not sure if it'll help with my explanation. // Work out the vector coordinates StartVector.X = (lPathCell->X - Agent->X); StartVector.Y = (lPathCell->Y - Agent->Y); // Find the length of the vector Length = SquareRoot(x*x + y*y) lStartVectorMagnatude= sqrt(static_cast<double>((StartVector.X * StartVector.X) + (StartVector.Y * StartVector.Y))); // This is calculating the correction angle. The position of the Agent is a unit vector, // i.e. where the length is 1 (We don't have another point from which to determine the // Agents vector. // Need to be measured anti-clockwise double lAgentOrientationRads = Utilities::Deg2Rad(Agent->Orientation); lPathCell->pParentCell->NavAngle = (StartVector.X*cos(lAgentOrientationRads)) + (StartVector.Y*sin(lAgentOrientationRads)); lPathCell->pParentCell->NavAngle = lPathCell->pParentCell->NavAngle/lStartVectorMagnatude; lPathCell->pParentCell->NavAngle = Utilities::Rad2Deg(acos(lPathCell->pParentCell->NavAngle)); //Depening on the sign of the cross product determine which way to turn CrossProductVector.Z = (Agent->X * StartVector.Y ) - (Agent->Y - StartVector.X); if (CrossProductVector.Z < 0.0) { lPathCell->pParentCell->NavAngle= -lPathCell->pParentCell->NavAngle; } Thanks Mark