2d 相机未正确跟随精灵

发布于 2024-09-28 20:52:59 字数 1036 浏览 1 评论 0原文

我需要相机跟随精灵的帮助。我有一个相机类没有正确跟随精灵。我的相机类是

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁我热情 2024-10-05 20:52:59

您正在考虑精灵偏移问题。精灵的位置和相机的位置之间没有什么区别。因此,您的精灵将显示为始终位于 {0, 0}。

如果您将相机的位置偏移其视图高度和宽度的一半,那么它将显示为将精灵居中。

cam.position = sprite.position;
cam.position.x -= cam.width / 2;
cam.position.y -= cam.height / 2;

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.

cam.position = sprite.position;
cam.position.x -= cam.width / 2;
cam.position.y -= cam.height / 2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文