Rotations with QQuaternion and QSlider

1.3k views Asked by At

I have implemented an Objectrotation with QQuaternion and QPushButton. As long as the plus_x_button is pushed the slot rotate_plus_x() is activated. Respectively for minus_x.

void OpenGLScene::rotate_plus_x()
{
  OpenGLScene::anglex = 2;
  test->rotation *= QQuaternion::fromAxisAndAngle(QVector3D(1,0,0),OpenGLScene::anglex);
  update();
}

void OpenGLScene::rotate_minus_x()
{
  OpenGLScene::anglex = -2;
  test->rotation *= QQuaternion::fromAxisAndAngle(QVector3D(1,0,0),OpenGLScene::anglex);
  update();
}
void OpenGLScene::rotate_plus_y(){...}
void OpenGLScene::rotate_minus_y(){...}

void OpenGLScene::rotate_plus_z(){...}
void OpenGLScene::rotate_minus_z(){...}

Now I would like to realize the functionality with a Qslider instead of the QPushButton. With a range between -180° and 180° But then I have the problem that I get weird outcomes because the value of the Qslider is changed and the QQuaternion expexts a nonchangeble angle. Have you an idea how to achieve that? I have tried it with if-statements. Something like:

if(slidervalue<0){
OpenGLScene::anglex = -2;
test->rotation *= QQuaternion::fromAxisAndAngle(QVector3D(1,0,0),OpenGLScene::anglex);}
else{
OpenGLScene::anglex = 2;
test->rotation *= QQuaternion::fromAxisAndAngle(QVector3D(1,0,0),OpenGLScene::anglex);}

Unfortunately its not working. Do you have an idea, how to achieve that?

Thank you

1

There are 1 answers

1
JCooper On BEST ANSWER

I think that to get what you're looking for, you should assign rather than multiply.

That is, if you slider is set to have values from -179 to +180 degrees, then you can just say:

OpenGLScene::anglex = slidervalue;
test->rotation = QQuaternion::fromAxisAndAngle(QVector3D(1,0,0),OpenGLScene::anglex);

Note that I'm using = instead of *=. That makes that the slider behave intuitively.