相机问题

时间:2014-03-17 14:11:32

标签: c# xna 2d perspectivecamera

我希望屏幕跟随播放器,看到很多人使用Matrix来让相机按照自己的意愿进行操作,我有点陷入困境。我有一个名为Game1.cs的主类,一个Player类是持有的运动,大多数人都有摄像机功能的运动系统。我想要的只是跟随播放器的相机,没有变焦或旋转(此刻)。

我开始使用Camera类:

class Camera
{
    public Matrix Transform;
    Vector2 Position;
    Viewport viewPort;

    public Camera(Viewport viewport)
    {
        viewPort = viewport;
        Position = Vector2.Zero;
    }

    public void Update(GameTime gameTime)
    {
        Transform = Matrix.CreateTranslation(Position.X, Position.Y, 0);
    }
}

Game1.cs:

protected override void LoadContent()
{
     camera = new Camera(GraphicsDivice.Viewport);
}

Protected override void Draw(GameTime gameTime)
{
     spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.Transform);

     //Stuff Here

     spriteBatch.End();
     base.Draw(gameTime);
}

我错过了什么?如何解决此问题?

我应该如何添加我的Player.cs类?

编辑:2014年3月3日10:36

我通过将矩阵变换更改为方法并将Transform()添加到SpriteBatch.Begin()来使其跟随播放器。我还添加了public Player thing(Player player)以使位置等于玩家位置。但是播放器卡在屏幕的左侧。

如何将播放器置于屏幕中央?

代码现在看起来像这样:

class Camera
{
    Player player;

    public Vector2 Position;
    Viewport viewPort;

    public Matrix Transform()
    {
        var translationMatrix = Matrix.CreateTranslation(new Vector3(-Position.X, 0, 0));

        return translationMatrix;
    }

    public Player thing(Player player)
    {
        Position = player.Position;

        return player;
    }

    public Camera(Viewport viewport)
    {
        viewPort = viewport;
    }

    public void Update(GameTime gameTime)
    {
    }
}

1 个答案:

答案 0 :(得分:0)

播放器卡在屏幕左侧,因为您将播放器位置设置为与摄像机位置相同。您需要设置偏移量:

Vector2 offset = new Vector2(GraphicsDeviceManager.PreferredBackBufferWidth / 2, 0); //or whatever you like; new Vector2(100, 0) for an instance
Position = player.Position + offset; // plus or minus don't know just test