### 2D Sprite Shakes while being followed by camera

I'm making my view follow around my players ship, so it is always in the center, however at any angle other than 0/90/180/270 the player sprite starts shaking, more so when speed is increased and when the angle is closer to 45/135/225/315, any ideas?

I'm using SFML2, I would take a screenshot but it only captures one frame, which doesn't actually show it shaking.

 ``1234567891011121314151617181920212223242526`` ``````void ViewFollowPlayer(sf::View& View, sf::RenderWindow& Window, double Delta) { double pSpeed = Player.getSpeed(); if (Player.getPosition().x > View.getCenter().x) { View.move(1 * pSpeed * Delta, 0); } if (Player.getPosition().x < View.getCenter().x) { View.move(-1 * pSpeed * Delta, 0); } if (Player.getPosition().y > View.getCenter().y) { View.move(0, 1 * pSpeed * Delta); } if (Player.getPosition().y < View.getCenter().y) { View.move(0, -1 * pSpeed * Delta); } Window.setView(View); }``````

Last edited on
You're moving independently and applying speed along the X axis and Y axis, but the player speed (I'm assuming) is the total speed.

For example, if the player speed is 1, and the angle is 45 degrees, the player will actually be moving at a speed of ~0.707 along both X and Y axis.

Your camera code here will move the camera as if the player was moving 1.0 on each axis, rather than 0.7 on each axis, which means it will overshoot, then undershoot to compensate, then overshoot again, etc... causing the shaking you're seeing.

If you want a camera that keeps the player rigidly in the center of the screen, it's much simpler to do this.. you can just set the camera position to the player position directly:

 ``123`` ``````// not sure on the exact code here, too lazy to look up: View.setPosition( Player.getPosition() );``````

Or if you want to keep the camera more "fluid" so the player isn't necessarily in the exact center all the time... it'd be better to get the player speed as a sf::Vector2f where you have independent X and Y speeds that you can use to move the camera.
thanks again disch, you always seem to be able to solve my problems ^^
np
Topic archived. No new replies allowed.