gpt4 book ai didi

c++ - 更改对象的原点使其沿不同的轴移动

转载 作者:太空宇宙 更新时间:2023-11-04 12:38:50 26 4
gpt4 key购买 nike

我使用这个 GameObject(利用 Box2D)和 Renderer2D 类已有一段时间了,我没有遇到任何问题。直到我决定添加旋转对象的选项。使用 2D 框一切正常(或看起来如此),但在屏幕上看到的对象在错误的轴上移动,向上移动时变小,向下移动时变大(或离相机更近和更远)。

我添加到 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 的 Velocity 变量移动游戏对象,如下所示:

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 问题。尝试改变 View 矩阵的 far 和 near 值,结果仍然相同。

最佳答案

解决了主要问题,在控制台上打印矩阵后我意识到第四列已从 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];

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

关于c++ - 更改对象的原点使其沿不同的轴移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55268921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com