所以我在我的游戏中实现了拍摄,但是当我调整窗口大小或移动时(因为我的相机)鼠标位置偏移并且镜头显然没有击中我想要的位置,我不知道如何解决它。这是相关代码:
Shot.cs
public class Shot : Sprite {
private float Speed = 500;
private Vector2 Dir;
private Vector2 Velocity;
public Shot(Texture2D texture, Vector2 position)
: base(texture) {
this.Position = position;
this.Dir = Input.GetMousePos() - Position;
}
void Start() {
}
public void Update(GameTime gameTime) {
var delta = (float)gameTime.ElapsedGameTime.TotalSeconds;
Dir.Normalize();
Velocity = Dir * Speed;
Position += Velocity * delta;
}
}
Camera.cs GetTransform()(请注意我没有写这个)
public Matrix GetTransform(GraphicsDevice graphicsDevice) {
transform = Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0)) *
Matrix.CreateRotationZ(Rotation) *
Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
Matrix.CreateTranslation(new Vector3(graphicsDevice.Viewport.Width * 0.5f, graphicsDevice.Viewport.Height * 0.5f, 1));
return transform;
}
spriteBatch.Begin();
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, null, null, null, camera.GetTransform(GraphicsDevice));
还想补充一点,这不是拍摄特定问题,当我尝试拖动GUI窗口时,位置也会偏移,所以我想我需要修改我的GetMousePosition方法,但是我是不确定如何。
编辑:这里是GetMousePos()
public static Vector2 GetMousePos() {
return new Vector2(mouseState.X, mouseState.Y);
}