2d 相机未正确跟随精灵
我需要相机跟随精灵的帮助。我有一个相机类没有正确跟随精灵。我的相机类是
camera cam;
cam.position = sprite.position;
这段代码没有正确执行。每次我运行此代码时,它都会重置我的精灵,就好像它位于位置 (0,0) 一样,然后跟随我的精灵。这是我正在谈论的内容的视频示例。
我的精灵的位置是(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.CreateTranslation(new Vector3(-_position.X, -_position.Y, 0));
return _transform;
}
}
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.CreateTranslation(new Vector3(-_position.X, -_position.Y, 0));
return _transform;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在考虑精灵偏移问题。精灵的位置和相机的位置之间没有什么区别。因此,您的精灵将显示为始终位于 {0, 0}。
如果您将相机的位置偏移其视图高度和宽度的一半,那么它将显示为将精灵居中。
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.