【问题标题】:Rotation and move to relative center in OpenGL旋转并移动到OpenGL中的相对中心
【发布时间】:2011-11-15 18:25:46
【问题描述】:

我有一个简单的模型,我想旋转 360° 并朝新的朝向移动。我的问题是当我翻译模型时,因为它在 (0,0,0) 而不是在它自己的中心旋转。这是我的代码:

void Car::move(void){
    if (car_direction > TWO_PI) 
        car_direction -= TWO_PI;
    else
        car_direction += TWO_PI;

    car_x_pos = sin(car_direction)*incMov;
    car_z_pos = cos(car_direction)*incMov;

    glPushMatrix();
        glTranslatef(car_x_pos, car_y_pos, car_z_pos);
        glRotatef(RAD_TO_DEG * car_direction, 0, 1, 0);
        drawCar();
    glPopMatrix();
}

**编辑

我发现了问题,我计算错了 x 和 z 正确的代码是:

void Car::move(void){
if (car_direction > TWO_PI) 
    car_direction -= TWO_PI;
else
    car_direction += TWO_PI;

car_z_pos += incMov * cos(car_direction); //correct calculation
car_x_pos += incMov * sin(car_direction); //correct calculation


glPushMatrix();
    glTranslatef(car_x_pos, car_y_pos, car_z_pos);
    glRotatef(RAD_TO_DEG * car_direction, 0, 1, 0);
    drawCar();
glPopMatrix();

}

现在汽车可以毫无问题地旋转和移动。

【问题讨论】:

  • 360 度旋转是无操作的。朝着你当前的方向前进。
  • 对,你不是说90度还是180度吗?
  • 我的意思是汽车/悬停可以在他的中心旋转 360° 然后移动到新的方向,但问题是当我翻译它时中心不是他的中心是(0,0 ,0) o 想要始终在他的中心旋转
  • @fmarin 然后将其翻译到原点,在那里执行旋转并将其翻译回来......
  • @Bart 这就是我在处理 OpenGL 项目时经常做的事情。平移、旋转、向后平移。

标签: c++ opengl matrix-multiplication


【解决方案1】:

我认为你只是把你的订单倒过来了。尝试先调用 Rotate,然后再调用 Translate。

【讨论】:

    猜你喜欢
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    相关资源
    最近更新 更多