2d Camera Not following Sprite Correctly
I need help with a camera following a sprite. I have a camera class which isn't following the sprite properly. My camera class is
camera cam;
cam.position = sprite.position;
this piece of the code isn't executing properly. everytime I run this code it resets my sprite as if it was in position (0,0) and then follows my sprite. Here's a video example of what I'm talking about.
The position of my sprite is at (60,515).class Camera2d
{
public float _zoom;
public Matrix _transform;
public Vector2 _position;
protected float _rotation;
public Camera2d()
{
_zoom = 1.0f;
_rotation = 0.0f;
_position = Vector2.Zero;
}
//public float Zoom { }
//public float Rotation { }
public void Move(Vector2 amount)
{
_position += amount;
}
public Vector2 CPos
{
get { return _position; }
set { _position = value; }
}
public Matrix get_tranformation(GraphicsDevice graphicsDevice)
{
_transform = Matrix.Create开发者_开发知识库Translation(new Vector3(-_position.X, -_position.Y, 0));
return _transform;
}
}
You're looking at a sprite offset problem. The difference between your sprite's position and your camera's position is nothing. Therefore your sprite will appear to always be located at {0, 0}.
If you offset your camera's position by half of it's view height and width then it will appear to center your sprite.
cam.position = sprite.position;
cam.position.x -= cam.width / 2;
cam.position.y -= cam.height / 2;
精彩评论