Carl Sverre
Carl Sverre

Reputation: 1188

OpenGL Rotation Matrices And ArcBall

I have been tasked with creating a OpenGL scene implementing ideas such as simple movement, and an Arcball interface. The problem I am having is dealing with the rotation matrix which NeHe's Arcball class (http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=48) computes.

What I have so far is a very simple solar system (just the earth, moon and sun) which looks great. What I want is for the camera to follow whichever planet the user selects (by clicking on the one they want), and for them to be able to rotate around the planet at a fixed distance using mouse-drag (arcball). As I said at the beginning, NeHe's class is generating a rotation matrix based on the mouse clicking and dragging. What I want to apply the matrix to is the camera position. However, when I do that my camera just wobbles without ever rotating around the planet. So I am guessing that I am either missing some step, or that I have a horrible understanding of what I am trying to do.

Here is some code from my camera class to crunch on:

// transform is the matrix from NeHe's arcball interface
void camera::update(Matrix4fT transform) {
    glm::mat4 transform_m = glm::mat4(0.0f);

    // convert nehe's matrices to GLM matrix
    for(int i=0; i < 4; i++)
        for(int j=0; j < 4; j++)
            transform_m[i][j] = transform.M[i*4+j];

    // apply matrix to the position
    glm::vec4 pos4 = glm::vec4(this->pos, 1.0f);
    pos4 = transform_m * pos4;

    this->pos = glm::vec3(pos4);
}

void camera::apply(planet *target) {
    // called at the beginning of GLPaint
    gluLookAt(this->pos.x,this->pos.y,this->pos.z,       // cam->position
              target->pos.x,target->pos.y,target->pos.z, // moving
              this->up.x,this->up.y,this->up.z);         // (0,1,0)
}

Other than that, NeHe's functions are called in the right places (during click and drag)... So really, I have no idea where to go from here. I hope someone can help me with this, and if you want to see the whole code base (its programmed in C++ and pushed into a QTPanel) just send me an email.

Thanks,

Carl Sverre (carl at carlsverre dot com)

Upvotes: 2

Views: 6518

Answers (1)

Artur Soler
Artur Soler

Reputation: 3024

Well, maybe I am wrong, but what I think that is happening to you is that you are rotating around the center of coordinates and not around the planet (that it's what you want to do). To correct that what you have to do is:

  • Translate the point you want to rotate around (the center of the planet) to the center of coordinates, applying a translation of the negation of its position
  • Rotate as you are doing it
  • Undo the translation previously done.

The thing to understand is that rotations are done around the center of coordinates, and if you want to rotate around somewhere different, you must first move that point to the center of coordinates.

Hope that it helps.

Upvotes: 2

Related Questions