移动物体时,相机不会保持居中于物体上。
我似乎无法找到错误。请帮忙。
初始 -
`ConvertUnits.ToDisplayUnits(body.Position)-(Camera._screenCenter- Camera._cameraPosition)`
返回零。
经过一些运动后,它会改变。
我正在使用Farseer Physics Engine 3.5并使用力移动身体。
body.ApplyForce();
这是我的相机类 -
class Camera
{
private static Matrix _view;
public static Vector2 _cameraPosition;
public static Vector2 _screenCenter;
private static float _zoom;
private static float _rotation;
public static void Load(GraphicsDeviceManager _graphics)
{
_view = Matrix.Identity;
_cameraPosition = Vector2.Zero;
_screenCenter = new Vector2(_graphics.GraphicsDevice.Viewport.Width / 2f, _graphics.GraphicsDevice.Viewport.Height / 2f);
_rotation = 0f;
_zoom = 1.0f;
}
public static void HandleInput(KeyboardState state)
{
if (state.IsKeyDown(Keys.Add) && _zoom < 1.0f)
_zoom += 0.1f;
if (state.IsKeyDown(Keys.Subtract) && _zoom > 0.1f)
_zoom -= 0.1f;
Update();
}
public static void Update()
{
_view = Matrix.CreateTranslation(
new Vector3(_cameraPosition - _screenCenter, 0f)) *
Matrix.CreateRotationZ(_rotation) *
Matrix.CreateScale(new Vector3(_zoom, _zoom, 1)) *
Matrix.CreateTranslation(new Vector3(_screenCenter, 0f));
}
public static void Follow(Body body)
{
if (body != null)
_cameraPosition = _screenCenter - ConvertUnits.ToDisplayUnits(body.Position);
}
public static Matrix getView()
{
return _view;
}
}
答案 0 :(得分:0)
您的_screenCenter
是相机的原点,制作矩阵时无法正确计算。
尝试:
return Matrix.CreateTranslation(new Vector3(-Position, 0.0f)) *
Matrix.CreateTranslation(new Vector3(-Origin, 0.0f)) *
Matrix.CreateRotationZ(Rotation) *
Matrix.CreateScale(Zoom, Zoom, 1) *
Matrix.CreateTranslation(new Vector3(Origin, 0.0f));