views:

1598

answers:

1

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)

+2  A: 

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.

Artur Soler
Hey thanks for the answer! I tried to implement what you mentioned like so: http://2dsquid.pastebin.com/m510b0130 Unfortunately it didn't work and results in my camera flying away from my planet until the whole scene is out of the frustrum. Maybe I have the order wrong in the multiplication... Any suggestions?
Carl Sverre
So I figured out the zooming out problem (my rotation matrix was computed on a sphere around 0,0,0 so I was transforming 1 to many times)... Now the camera sorta rotates around the planet like it should except it rotates at light speed, and if I rotate to the left for awhile and then try to rotate to the right it keeps rotating to the left until it seems to "normalize" and then start rotating to the right. And any up/down motion messes it all up. Any ideas?
Carl Sverre
If it is rotating too fast, you must configure your multiplication parameters to make it rotate slower.. I don't know exactly how are you getting the parameters, but it's a matter of "playing" with the values to get your desired effect.
Artur Soler
Well I solved the problem, but I went down a different path. I made sure that the sphere I was rotating about was at 0,0,0 and rather than rotating the camera I just rotated the scene. Anyways, long story short its fixed. I will give this one to you because no one else answered and your answer helped me get to my solution.Cheers
Carl Sverre