微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我的运动身体位置和比例与预期的位置和比例不同

如何解决我的运动身体位置和比例与预期的位置和比例不同

我正在尝试使用 Box2D 为我的“游戏引擎”创建一个物理系统,当我创建一个 b2_kinematicBody 时,位置大于预期位置,并且比例小于预期比例。

我尝试用 SetAsBox 更改 SetAsBox(this->transform->scale.x,transform->scale.y) 参数,但遇到同样的问题

注意:对于渲染,我使用 SDL2 并且位置基于 SDL2 位置。

Example of the error

这是问题的视频:https://youtu.be/0fMPh8Rk_pY

这是我的 KinematicBody2D 类的代码

KinematicBody2D.h

class KinematicBody2D : public Component 
{
    private:
        b2BodyDef k_bodydef;
        b2Body* k_body;
        b2FixtureDef k_fixtureDef;
        b2Fixture *k_fixture;
        b2World* k_world;
        b2polygonShape polygonShape;

    public:
        KinematicBody2D();
        KinematicBody2D(Scene scene);

        void Start() override;
        void Loop(float delta) override;
        void MovePosition(Vector2 target);
};

KinematicBody2D.cpp(只是重要的部分)

    void KinematicBody2D::Start()
    {
        this->k_bodydef.type = b2BodyType::b2_kinematicBody;
        this->k_bodydef.position = b2Vec2(this->transform->position.x,this->transform->position.y);
        this->k_bodydef.angle = Math::Deg2Rad(this->transform->rotation);

        this->k_body = this->k_world->CreateBody(&this->k_bodydef);

        this->polygonShape.SetAsBox((this->transform->scale.x / 2.0f),(this->transform->scale.y / 2.0f));

        this->k_fixtureDef.shape = &this->polygonShape;

        this->k_fixture = this->k_body->CreateFixture(&this->k_fixtureDef);
    }

    void KinematicBody2D::Loop(float delta)
    {
        this->transform->position.x = this->k_body->GetPosition().x;
        this->transform->position.y = this->k_body->GetPosition().y;
        this->transform->rotation = Math::Rad2Deg(this->k_body->GetAngle());

        this->polygonShape.SetAsBox((this->transform->scale.x / 2.0f),(this->transform->scale.y / 2.0f));
        this->transform->scale = Vector2(this->transform->scale.x,this->transform->scale.y);

        return;
    }

ma​​in.cpp(只是重要的部分)

int main()
{
    GameObject playerObj = GameObject("Player");
    Scene scene = Scene("Scene",&playerObj);

    playerObj.AddComponent<Sprite>("res/Player.png",Vector4(0,256,256),Vector2(64,64));
    playerObj.AddComponent<Rigidbody2D>(scene);
    playerObj.AddComponent<Player>(app.GetRaWrenderer());

    GameObject floor = GameObject("Floor","Floor");
    floor.AddComponent<Sprite>("res/World 1.png");
    floor.AddComponent<KinematicBody2D>(scene);
    floor.transform.position = Vector2(100,200);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。