   The position of an object
  i would like one of my NPC looks in a specified position. i think i will have to use the following formula to know in what angle it has to look:v.w=||v||||w||cos(angle)angle=arccos[(v.w)/(||v||||w||)]my problem is i don't remember the way we construct a vector, when we only have some coordinate positions.   
  Don't know what you're doing(having taken a vector class), but here's something that should work(c++):int angle = atan2(posY-lookY,posX-lookY);If you don't have an atan2() function:int angle = atan((posY-lookY)/(posX-lookX));if(posX-lookX < 0){angle = -1*angle;}   ;Quote:Original post by EzbezDon't know what you're doing(having taken a vector class), but here's something that should work(c++):int angle = atan2(posY-lookY,posX-lookY);If you don't have an atan2() function:int angle = atan((posY-lookY)/(posX-lookX));if(posX-lookX < 0){angle = -1*angle;}imagine you're looking forward and there is a bird in your right side. knowing the bird coordinates, i would like to know in what angle you have to turn to see that bird.;Quote:Original post by jolyqrQuote:Original post by EzbezDon't know what you're doing(having taken a vector class)imagine you're looking forward and there is a bird in your right side. knowing the bird coordinates, i would like to know in what angle you have to turn to see that bird.;  your formula does not help me, because if you turn your head in another direction, the angle of the bird does not change...   ;  If the "forward" looking vector (in world space) is F, and the normalized direction vector towards the point you want to look is D, then the axis you want to turn about is  A = normalize(F cross D)the local "right" vector is  R = (A cross F) // guaranteed to be unit length alreadyand the angle you want to turn is  if (D dot F > 0)    w = asin(R dot D)  else    w = PI - asin(R dot D)If you know that the up axis is always constant (i e, your problem is in 2D) then you can obviously make some simplifications of the math.Btw: If the position of the viewer is Pv, and the position of the target is Pt, then the look direction vector is  D = normalize(Pt - Pv);Quote:Original post by hplus0603If the "forward" looking vector (in world space) is F, and the normalized direction vector towards the point you want to look is D, then the axis you want to turn about is  A = normalize(F cross D)the local "right" vector is  R = (A cross F) // guaranteed to be unit length alreadyand the angle you want to turn is  if (D dot F > 0)    w = asin(R dot D)  else    w = PI - asin(R dot D)If you know that the up axis is always constant (i e, your problem is in 2D) then you can obviously make some simplifications of the math.Btw: If the position of the viewer is Pv, and the position of the target is Pt, then the look direction vector is  D = normalize(Pt - Pv)All my calculations are in 3D, but the y cordinate is the same for all objects.how do you find the F vector?Does normalise mean dividing by the magnitude?;  why is A in "world space" and R in "local space"?   ;Quote:Original post by jolyqrwhy is A in "world space" and R in "local space"?All the vectors in question are in world space. The terminology can be a bit confusing, but usually when we refer to one of an object's local vectors, we mean as expressed in world space. The 'actual' local vectors are usually just the cardinal axes, which aren't of much interest when solving these sorts of problems.You mentioned how to find F. This is fundamental, and I'm guessing you're already doing it somewhere (you may just not know it). For example, if at any point a transformation matrix is being associated with your character (which is most likely the case if you're working with an API), then you already have all of the basis vectors for your character.Anyway, as has been mentioned this is essentially a 2D problem. Given the forward vector F and the non-normalized difference vector to the target D, the angle to turn to face the target is:angle = atan2(perp_dot(F,D),dot(F,D));Just ask if you're not sure what the 'perp dot' is. Note that the resulting angle is signed, and the sign tells you whether you need to turn right or left.   ;Quote:Original post by jykQuote:Original post by jolyqrwhy is A in "world space" and R in "local space"?All the vectors in question are in world space. The terminology can be a bit confusing, but usually when we refer to one of an object's local vectors, we mean as expressed in world space. The 'actual' local vectors are usually just the cardinal axes, which aren't of much interest when solving these sorts of problems.You mentioned how to find F. This is fundamental, and I'm guessing you're already doing it somewhere (you may just not know it). For example, if at any point a transformation matrix is being associated with your character (which is most likely the case if you're working with an API), then you already have all of the basis vectors for your character.Anyway, as has been mentioned this is essentially a 2D problem. Given the forward vector F and the non-normalized difference vector to the target D, the angle to turn to face the target is:angle = atan2(perp_dot(F,D),dot(F,D));Just ask if you're not sure what the 'perp dot' is. Note that the resulting angle is signed, and the sign tells you whether you need to turn right or left.there is no matrix associated with my character. so i still don't know how to find F...i don't know what the "perp_dot" is...   