SFML 2.0 Look at

is there a way to make an object rotate until it looks at the sprite?
Again this is not really an SFML question, but is a general game logic / math question.

To "look at" another object, you'll need to know the angle of rotation. You can find that with the atan2 function in the <cmath> header.

Though note a few things.
1) SFML has Y coord inverted from normal (+Y is 'down' instead of the usual 'up')
2) SFML expects angles in degrees, whereas atan2 gives you the angle in radians.

1
2
3
4
5
6
7
8
double angle = atan2( 
          my_pos.y - target_pos.y,  // SFML has inverted Y
           target_pos.x - my_pos.x );

// convert that angle from radians to degrees
angle *= 180.0 / pi;

// give that angle to SFML with setRotation or whatever the function call is 
Topic archived. No new replies allowed.