相机问题
本文关键字:问题 相机 | 更新日期: 2023-09-27 17:50:41
我想让屏幕跟随玩家,看到许多人使用矩阵式让相机做他们想做的,我有点卡住了。我有一个名为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类?
我通过将矩阵变换更改为方法并将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)
{
}
}
玩家被卡在屏幕左侧,因为你将玩家位置设置为与摄像机位置相同。你需要设置offset:
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