Using openGL (just 2d), I’m trying to rotate a texture so it’s pointing towards a point on screen. I’ll show an image first to help me explain.
http://img692.imageshack.us/img692/3088/probe.png
Say my texture is the blue dot at point 1 and it is moving to its destination at point 2. I want to rotate #1 so that it is “pointing” towards point 2 (the texture is a bird so it has a defined “front”). To do this, I need to find out angle 3. Similarly, if my bird is at point #4 travelling towards point 5, I need to work out angle 6.
What’s the secret to doing this?
The solution is the super-useful
std::atan2function. Subtract the current position from the target position of a bird, and stuffyandx(note the order!) intoatan2to get the angle.Edit: Note that atan usually assumes 0° to be at the X+ axis (right). However, you seem to align your ‘base direction’ to Y+ instead (up), so you might want to subtract 90° or fiddle with the order and signs of parameters to the atan functions (using the basic symmetries in a circle, i.e.
atan2(-x,y)).