   Struggling with the dot product
  Hello,I'm having some trouble calculating the dot product between two vectors. As you can hopefully see in the image i have 2 characters (P1 and P2). P1 is facing north and i have shown a rough line of sight. i then have two vectors (V1 and V2) V1 is the facing vector of P1 and V2 is the vector from P1 to P2. I want to calculate the angle between these two vectors.I presume the dot product is best for this but i can't quite understand some of the values i am receiving from my code. I'm sure i have errors in the code but i am a begginer so please forgive me.Here's the sample of my dot product code so far. Am i calculating this correctly?Currently the P1 responds when P2 is within the circle but i want him only to respond if P2 is in the circle and in line of sight so i need this angle.// Guard to thief vectorfloat x1;float z1;float Vec1Length;x1 = thief->GetX() - guard->GetX();z1 = thief->GetZ() - guard->GetZ();Vec1Length = ( x1 * x1 + z1 * z1 );// Guard orientation vector ( facing vector )float guardMatrix[16];float x2;float z2;float Vec2Length;guard->GetMatrix( guardMatrix );x2 = guardMatrix[8];z2 = guardMatrix[10];Vec2Length = ( x2 * x2 + z2 * z2 );// Obtain the 'Dot Product'float dotProduct;float Angle;dotProduct = x1 * z1 + x2 * z2;// Obtain the angle between the two vectorsAngle = cos( dotProduct );// Checks states and alters themfloat alertState = 8.0f;float idleState = 12.0f;float collisionDist = sqrt( x1 * x1 + z1 * z1 );Thanks in advance.   
  This won't be a comprehensive answer, but:1. It should be 'Angle = acos( dotProduct )'.2. Both vectors need to be normalized (unit-length) in order for the above to return meaningful results.3. Be sure to clamp the dot product to [-1, 1] before submitting it to the acos() function.4. There's no need to separate variable declaration and initialization as you're doing; you can declare and initialize variables in the same statement.5. The variables that you're calling 'length' are actually squared lengths.6. This sort of thing will be so much easier if you use a proper math library (or at least a geometric vector class of some sort).7. This is IMO, but when working with a simulation that has a strong 2-d aspect to it (a first-person shooter, for example, would fall into this category), I think it can be easier and considerably more intuitive to consider Z to be the vertical axis (so that the sort of computations you're performing here will lie in the XY plane).   ;  Thanks for your quick reply.I will have another crack at it now taking into account all your suggestions. Hopefully i can sort it out. Thanks again mate.   ;  You're calculating the dot product correctly, but what it gives you is the result of cos(x), if x was the angle between the vectors.  In other words, if you need to find the angle, you need the inverse cosine of the dot product.Your calculation of length also appears to be incorrect.  If a vector U = (X, Y, Z), then the length would be sqrt(X*X + Y*Y + Z*Z).Hope that helps.   ;  Sorry to ask more questions but should i be expecting my Angle variable to have a value in the range of 0 - 360 degrees if i ensure the code is correct first.Thanks again.   ;Quote:Original post by Ronnie TRFCSorry to ask more questions but should i be expecting my Angle variable to have a value in the range of 0 - 360 degrees if i ensure the code is correct first.Thanks again.The math functions in the C++ standard library work with radians (not degrees).   ;  Thank you for your help, it works. I just did a conversion from radians to degrees so that it's easier for me to work with. Also thank you for your advice on other things you mentioned.Cheers mate.   