   Calculating angle between two objects in 2D (Actionscript 3.0)
  Hey guys,I am currently building a small flash game, and I am trying to implement some basic AI.The point I am struggling with though, is the rotation of the objects.Basically, I have the object I wish to rotate, It has an attribute called rotation, which is the objects rotation in relation to its origin (in radians).I have the x and y positions of both objects.I also have the distance between the objects which i have previously worked out.Im stuck though, and the most frustrating thing is I should know this I did it only a couple of years ago... ThanksChaosgeneral   
 this is what I use:float vec2angles(vec3 *vec){float retval;retval= atan(vec->z / vec->x) * (180 / 3.141);if(vec->x < 0)retval += 180;return retval-90;}vec should be the target's x/y minus the targeter's x/y;Quote:Original post by radioteeth this is what I use:float vec2angles(vec3 *vec){float retval;retval= atan(vec->z / vec->x) * (180 / 3.141);if(vec->x < 0)retval += 180;return retval-90;}vec should be the target's x/y minus the targeter's x/yIf you have an 'atan2' function available, you should use it instead of atan. (It's more stable, and will eliminate the need for determining the quadrant manually.)Here's how to compute the signed relative angle between two 2-d vectors using atan2:angle = atan2(perp_dot(a,b), dot(a,b));The input vectors don't have to be unit length.   ; Wouldn't a dot product calculation involve a normalization w/ sqrt though ?   ;Quote:Wouldn't a dot product calculation involve a normalization w/ sqrt though ?It's not necessary in this case. The magnitudes of the dot and perp-dot products are proportional to the cosine and sine (respectively) of the angle between the two vectors; the scaling factor in each case is the product of the lengths of the two vectors. Since atan2() is concerned only with the ratio between (and signs of) the input values, it's not necessary for the input vectors to be unit length.   ;  Thanks guys, I worked out how to use atan2, but I have encountered another small problem.I do not wish my object to just instantly follow, I wish to move it by increment over time. My only problem is working out when to increment and when to decrement. This code here works almost perfect, but due to atan2's nature, 1/4 of the circle it will turn left, the other 3/4 will turn right. Not quite sure how to check this exactly.Edit:Here is my code sorryvar dir = (Math.atan2(target.mc.y - mc.y, target.mc.x - mc.x)* 180/Math.PI);if(dir < mc.rotation){mc.rotation  = mc.rotation - 1;}else{mc.rotation  = mc.rotation + 1;};  If you work with absolute angles, you'll have to deal with periodicity, which can be a pain. I recommend instead computing the relative angle to the target (using the technique I posted earlier), and checking the sign to determine which direction to turn.Also, if all you're interested in is which direction to turn (and not how far), you can just check the sign of the dot product of the vector to the target and the local side vector.   ;  Easy. Also this problem comes up a lot. Look at my post in this thread:Why way do I turn? threadIt rotates a vector. However a quick mc.rotation = Math.Atan2(y, x) will get the angle of the rotated vector.   