改变对象的原点点使其以不同的轴移动



我一直在使用此GameObject(使用Box2D)和Renderer2D类工作了一段时间,但我没有任何问题。直到我决定添加旋转对象的选项。所有这些都可以使用Box 2D工作正常(或似乎是),但是当在屏幕上看到的对象在错误的轴上移动时,在向下移动时移动时会变小(或向下移动时(或靠近距离远离相机))。<<<<<<<</p>

我添加到Renderer2D脚本中的仅有的三行是:

    model = math::translate(model, iVec3(0.5f * size.x, 0.5f * size.y, 0.0f));
    model = math::rotate(model, rotation, iVec3(0.0f, 0.0f, 1.0f));
    model = math::translate(model, iVec3(-0.5f * size.x, -0.5f * size.y, 0.0f));

如果我只是删除这些行,一切都很好,但是我希望对象旋转,以便我无法删除它们。

这是整个渲染函数:

void Renderer2D::Render(Texture & texture, iVec2 position, iVec2 size, float rotation, Color color, iVec2 tiling) {
    this->shader.Use();
    iMat4x4 model;
    model = math::translate(model, iVec3(position, 0.0f));
    model = math::translate(model, iVec3(0.5f * size.x, 0.5f * size.y, 0.0f));
    model = math::rotate(model, rotation, iVec3(0.0f, 0.0f, 1.0f));
    model = math::translate(model, iVec3(-0.5f * size.x, -0.5f * size.y, 0.0f));
    model = math::scale(model, iVec3(size, 1.0f));
    this->shader.SetMatrix4("model2D", model);
    this->shader.SetVector3f("spriteColor", iVec3(color.r, color.g, color.b));
    this->shader.SetVector2f("tiling", tiling);
    glActiveTexture(GL_TEXTURE0);
    texture.Bind();
    glBindVertexArray(this->QuadVAO);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
    glBindVertexArray(0);
}

我使用Box2D的速度变量移动GameObject,例如:

float horizontal, vertical;
        if (Input::GetKeyDown(Input::Key.A) || Input::GetKeyDown(Input::Key.D)) {
            if (Input::GetKeyDown(Input::Key.A))
                horizontal = -1;
            else if (Input::GetKeyDown(Input::Key.D))
                horizontal = +1;
        }
        else horizontal = 0;
        if (Input::GetKeyDown(Input::Key.W) || Input::GetKeyDown(Input::Key.S)) {
            if (Input::GetKeyDown(Input::Key.W))
                vertical = -1;
            else if (Input::GetKeyDown(Input::Key.S))
                vertical = +1;
        }
        else vertical = 0;
        Rigidbody2D->Velocity = iVec2(horizontal, vertical) * speed;

我真的已经尝试了所有事情,但仍然不知道它是相机问题,渲染器还是Box2D。尝试更改视图矩阵的远值和接近值,但仍然相同的结果。

解决了主要问题,在控制台上打印矩阵后,我意识到第四列被从x y 0 1到0 x 0 y替换。

更改了代码:

model = math::translate(model, iVec3(0.5f * size.x, 0.5f * size.y, 0.0f));
model = math::rotate(model, rotation, iVec3(0.0f, 0.0f, 1.0f));
model = math::translate(model, iVec3(-0.5f * size.x, -0.5f * size.y, 0.0f));

对此完成!

iMat4x4 tempModel;
iMat4x4 tempModel2;

tempModel = math::translate(tempModel, iVec3(0.5f * size.x, 0.5f * size.y, 0.0f));
model[3].z = model[3].w = 0;
model[3] = model[3] + tempModel[3];
model = math::rotate(model, rotation, iVec3(0.0f, 0.0f, 1.0f));
tempModel2 = math::translate(tempModel2, iVec3(-0.5f * size.x, -0.5f * size.y, 0.0f));
model[3].z = model[3].w = 0;
model[3] = model[3] + tempModel2[3];

仍然没有得到我想要的旋转,但至少其他功能可行。

最新更新