OpenGL中旋转和移动到相对中心
我有一个简单的模型,我想旋转 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();
}
现在汽车可以旋转和移动,没有任何问题。
I have a simple model which I want to rotate 360° and move in the new facing direction. My problem is when I translate the model because it rotates in (0,0,0) rather than in its own center. This is my code:
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();
}
**EDIT
I found the problem, I was calculating wrong the x and z the correct code is:
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();
}
Now the car can rotate and move without any problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你只是把顺序搞反了。尝试先调用 Rotate,然后调用 Translate。
I think you just have your order backwards. Try calling Rotate first, then Translate.