I am creating a 2D C++ game engine from scratch minus making calls to the OS directly. For that, I am using SFML. I am essentially only using SFML to draw to the screen and collect input, so I am not looking for help with anything related to SFML.
Right now, I can pan the camera up and down and the sprites translate from world coordinates to screen coordinates correctly. The sprites also, for the most part, translate screen coordinates from world coordinates relative to camera rotation. The problem comes in when a rendered game object has a higher y world coordinate than the camera. When this happens it would appear that the sprite is reflected on the x-axis.
I will note that this does not happen if I comment out the rotation code shown below.
//shape's position has been set relative to camera position first
Vector2 screenCenter(windowWidth / 2, windowHeight / 2);
Vector2 shapePosition = shape->GetPosition ();
//create vector from center screen to shape position
Vector2 relativeVector = shapePosition - screenCenter;
float distance = relativeVector.Magnitude ();
if ( distance == 0 ) { return; }
float angle = Vector2::AngleInRads ( Vector2 ( 1, 0 ), relativeVector );
//rotation of camera in radians
float targetRotation = camera.GetFollowTarget ()->GetTransform ().GetRotation() * (M_PI / 180);
//combine rotation of camera and relative vector
float adjustedRotation = angle + targetRotation;
//convert rotation into a unit vector
Vector2 newPos ( cos ( adjustedRotation ), sin ( adjustedRotation ) );
//extend unit vector to be the same distance away from the camera
newPos *= distance;
//return vector origin to screen center
newPos += screenCenter;
shape->SetPosition ( newPos );
Below are some screenshots.
You can consider the blue square as my camera's focus. The purple circle is (0,0) world coordinates The arrow is pointing up in world space and as you can see is rendered incorrectly while the camera is below it.
It would be hard to show the rotation in action so you'll have to take my word for it. The rotation works as intended at any position of the camera aside from what I've described.
View while camera is at origin
View while camera is at the mid point of arrow
View while camera is above arrow
Please let me know if there's anything else I can provide that would be helpful.