好吧,我在XZ平原有个数字。我想把它向前/向后移动,在它自己的Y轴上旋转,然后在旋转的方向上再次向前移动,就像GTA 1中的字符。
目前为止的代码:
Init:
spaceship_position = glm::vec3(0,0,0);
spaceship_rotation = glm::vec3(0,0,0);
spaceship_scale = glm::vec3(1, 1, 1);
Draw:
glm::mat4 transform = glm::scale<float>(spaceship_scale) *
glm::rotate<float>(spaceship_rotation.x, 1, 0, 0) *
glm::rotate<float>(spaceship_rotation.y, 0, 1, 0) *
glm::rotate<float>(spaceship_rotation.z, 0, 0, 1) *
glm::translate<float>(spaceship_position);
drawMesh(spaceship, texture, transform);
Update:
switch (key.keysym.sym) {
case SDLK_UP:
spaceship_position.z += 0.1;
break;
case SDLK_DOWN:
spaceship_position.z -= 0.1;
break;
case SDLK_LEFT:
spaceship_rotation.y += 1;
break;
case SDLK_RIGHT:
spaceship_rotation.y -= 1;
break;
}
因此,这只在Z轴上移动,但是我如何在对象所面对的Z轴和X轴上移动对象呢?
发布于 2013-11-10 11:05:05
你的矩阵乘法顺序看上去不正确。基本上,你所做的是:
transform = rot * trans
这意味着对象将首先被转换,然后旋转。在平移之后,物体不会在原点,但是你的旋转矩阵会围绕世界轴旋转,这不是你想要的。你想要的效果是把物体绕着它的局部轴旋转,因此,颠倒顺序,即反*腐烂。
https://gamedev.stackexchange.com/questions/65654
复制相似问题