如何在OpenGL中旋转一定范围内的对象?

问题描述

我已经制造了一把锤子,我想快速旋转锤子以在按下特定键盘时产生打击。

我已经使用计时器功能来实现此目的。现在,当我按下特定的键盘时,锤子可以旋转360度,我的目标是将锤子旋转90至-90度。

我的代码

bool stopRotation = false;
void weaponController(int val) 
{
    if (stopRotation != false) {
           zr++;   //the default angle is 0
           glutPostRedisplay();
           glutTimerFunc(1,weaponController,1);
    } 
}
void specialkey(unsigned char key,int x,int y) 
{ 
    case 't':
    stopRotation = true;
    glutTimerFunc(10,0);
    break;
    
    case 'T':
    stopRotation = false;
    break;
}

我该怎么做?感谢您的帮助!

解决方法

如果zr是锤子的旋转,那么我会这样看:

int zr=0,dzr=+1; // might be a float I do not know as you did not share
bool stopRotation = false;
void weaponController(int val) 
{
    if (stopRotation != false) {
           zr+=dzr;   //the default angle is 0
           if (zr>+90){ dzr=-1; zr=+90+dzr; }
           if (zr<-90){ dzr=+1; zr=-90+dzr; }          
           glutPostRedisplay();
           glutTimerFunc(1,weaponController,1);
    } else { zr=0; dzr=+1; }
}

因此,dzr就是旋转方向,在+/- 90度障碍物的每个交叉点上反转。当不使用锤子时,其位置和方向会重置为开始条件....