# 2d camera shaking back and forth

I'm trying to create a smooth moving 2d camera but the camera is shaking back and forth 1 pixel.

Player camera rectangle:

public Rectangle recCamPosition(Global global)
{
return new Rectangle((int)Position.X, (int)Position.Y , 320, 240);
}


Camera rectangle:

public Rectangle recCamera(Global global)
{
return new Rectangle((int)_pos.X / global.zoom, (int)_pos.Y / global.zoom, 320, 240);
}


The move method:

public void physic(Vector2 position, Player player, GameTime time, Global global)
{
if(player.recCamPosition(global).X!=recCamera(global).X)
{
_pos.X+=(player.Position.X-160-recCamera(global).X)/16;
}
}


## 3 Answers

Try this (assuming _pos is Vector):

return new Rectangle((int)(_pos.X / global.zoom), (int)(_pos.Y / global.zoom), 320, 240);


Because:

(int)0.5f / 1 == 0 / 1 == 0
1 / 2 == 0
(float)1 / 2 == 0.5f


I think you should try the Vector2.Lerp() method. It basically interpolates two Vector2 values, and it's great for camera movement. What you've got is fine - but this might just be a bit better. To use it you could simply have the position and size of the camera rectangle as two Vector2s and lerp them. Here's an example.

Vector2 CurrentPosition;
Vector2 CurrentSize;

//Speed should be a value from 0.0f to 1.0f
public Rectangle UpdateCamera(Vector2 TargetPosition, Vector2 TargetSize, float Speed)
{
CurrentPosition = Vector2.Lerp(CurrentPosition, TargetPosition, Speed);
CurrentSize = Vector2.Lerp(CurrentSize, TargetSize, Speed);
return new Rectangle((int)(CurrentPosition.X), (int)(CurrentPosition.Y), (int)(CurrentSize.X), (int)(CurrentSize.Y));
}


Okay i fix the shake but i need to use ints and dont shake for pixel per pixel movement

The solution of this was hereMatrix.CreateTranslation((int)-_pos.X,(int)-_pos.Y, 0)* and change by this

   Matrix.CreateTranslation(-_pos.X,-_pos.Y, 0)*