   Turning to face a position in 2D
  I've been trying to figure out a formula to calculate the angle an object needs to turn at to point towards another object, given their positions and the orientation of the turning object.  Unfortunately, I haven't had much success.The formula that I keep coming back to, which doesn't work unless the objects are at the same X position, goes like this:Take the asin of the vertical distance from each other divided by their total distance from each other, and subtract this value from the angle of orientation of the turning object to calculate how far to the right the object must turn to point at the second object.As I said, the above doesn't work unless the two objects are on a vertical line.This isn't homework; I'm actually trying to develop a flocking simulation.  This is the crucial code that I can't quite get working.  Any help you could give would be appreciated.  Thanks!   
  If p0 is the position vector of the turning object, v is its normalized orientation vector, and p1 is the position vector of the other object, then: v dot normalize(p1 - p0) will give you the cosine of the angle that you need to turn. Then you just need to figure out which way you need to turn.Edit: I guess you have an orientation angle and not a vector for the turning object. So instead you could just get the angle that (p1 - p0) makes with the y-axis (if that's where your orientation angle is measured from):acos(normalize(p1 - p0).y) And if I think about it, that's exactly what you are doing, except you are using inverse sine instead of inverse cosine.[Edited by - Hexus on June 2, 2006 5:21:34 PM];  What i did was find the signed difference between the object's direction, and the target point, then rotated the object accordingly. But remember that angles can not be just subtracted, (0 - 360 = -360, but really it is 0). I sure there's a better way by taking the dot product of the perpendicular direction vector and the vector pointing toward the target point, like:tar = pos - target_posnormalize(tar)dot=dir.x * tar.y - dir.y * tar.xif (dot<0)  turn_right()else  turn_left()   ;  Hmmm... I don't think the inverse cosine is going to make this formula work any better than the inverse sine (actually, it's going to make it off by ninety degrees).  I think there's something pretty fundamentally wrong with the way I'm approaching the problem; the way it works out, only the Y axis gets taken into account when determining the rotation.  It doesn't actually work almost at all, except for at a few specific points.Thanks for your thought and time, and if anyone else has any good ideas, please let me know.  Thanks!   ;  if you turn the negative of the orientation of the turning object you get to Y+.add that to the orientation of (the vector from p0 to p1) = atan( dx / dy ).so:atan( (p0.x - p1.x) / (p0.y - p1.y) ) - orientationfmod by 360 or 2piyou'll want to double-check that...   ;  Ummm... dot=dir.x * tar.y - dir.y * tar.x, actually gives the signed direction, this bit of code worked for medx=cos(dir)dy=sin(dir)//normalize vectortx=mouse_x-xty=mouse_y-yd=sqrt(tx*tx+ty*ty)tx/=dty/=d//signs=sign(dy*tx-dx*ty)//cosinedot=+dx*tx+dy*ty//angle of differenceneeded_to_turn=-arccos(dot)*s   