Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quaternion rotation not working

I'm just starting to work with Quaternions, but I'm having some annoying difficulties getting a simple FPS camera to work properly using Quaternions.

Basically, whenever I try to move the mouse, a triangle I draw on the screen goes crazy and moves off and on the screen faster than I can see. The movement keys (wsad) are working as expected, UNTIL I move the mouse, and then it gets all messed up. I think the problem is with the rotation, but I've been pouring over this issue for the past couple of days to no avail.

Here's a video of what's going on: Quaternion Test Video

  • To see mouse input, watch from the beginning.
  • To see keyboard input (go forward, backward, etc) skip to 0:51 seconds.

Just for reference, here are the 3 main classes I'm using:
Quaternion.h
CameraSceneNode.h
CameraSceneNode.cpp

The most relevant sections are tick() (where I update the rotation Quaternion), and render() (where I render the 'camera', which consists of rotating and translating the scene).

Here is the tick() method:

void CameraSceneNode::tick(float32 time) {
    // movement direction
    if (movement_[MOVE_DIR_FORWARD] == 1)
        pos_ += rotation_ * vmath::Vector3f(0, 0, -moveSpeed_ * time);

    if (movement_[MOVE_DIR_BACKWARD] == 1)
        pos_ += rotation_ * vmath::Vector3f(0, 0, moveSpeed_ * time);

    if (movement_[MOVE_DIR_LEFT] == 1)
        pos_ += rotation_ * vmath::Vector3f(-moveSpeed_ * time, 0, 0);

    if (movement_[MOVE_DIR_RIGHT] == 1)
        pos_ += rotation_ * vmath::Vector3f(moveSpeed_ * time, 0, 0);

    // rotation
    if (xRot_ != 0) {
        Quaternion quatRotation = Quaternion();
        quatRotation.buildFromAxisAngle(1, 0, 0, (xRot_ * time * rotSpeed_) *         math::DEGTORAD);
        //quatRotation.normalize();
        rotation_ = rotation_ * quatRotation;
        xRot_ = 0;
        rotation_.normalize();
    }
    if (yRot_ != 0) {
        Quaternion quatRotation = Quaternion();
        quatRotation.buildFromAxisAngle(0, 1, 0, (yRot_ * time * rotSpeed_) *         math::DEGTORAD);
        //quatRotation.normalize();
        rotation_ = quatRotation * rotation_;
        yRot_ = 0;
        rotation_.normalize();
    }
}

and here is the render() method:

void CameraSceneNode::render() {
    if (isActive()) {
        float32 matrix[16];
        rotation_.fillMatrix(matrix);

        glMultMatrixf(&matrix[0]);

        //glRotatef(rotYTest, 0, 1, 0);
        //glRotatef(rotXTest, 1, 0, 0);

        glTranslatef(-pos_.x, -pos_.y, -pos_.z);
    }
}

Also, before I call render() on the camera, I run this bit of code:

glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

Both tick(..) and render() are called every frame from my main game loop. There are functions in the CameraSceneNode class that accept changes in movement direction and camera rotation (i.e. that change the movement_ buffer or xRot_ and yRot_) .

I'm sorry if this isn't enough information. Some of the functions (like fillMatrix(..)) are defined in those 3 .h/.cpp files I referenced above. I've checked over the Quaternion class several times, and it looks to me like it's correct. But then again, all the code I'm using looks correct to me, so who knows :S

I'm totally at my wits end with this. If anyone can shed some light on why this is not working, I would greatly appreciate it!!

Cheers

Jarrett

like image 552
Jarrett Avatar asked Jun 15 '11 22:06

Jarrett


1 Answers

From the looks of it, you're progressively accumulating your rotation every tick (rotation_ = quatRotation * rotation_;), which makes sense at first because all you're doing is adjusting the rotation slightly and so it should be safe to tack on another degree or two of rotation to what's already there, right?

However, each time you rotate (by what you're doing), you're actually slightly changing the axis of rotation each time, which is why you see this erratic behaviour.

What I would suggest is to keep track of the overall X/Y rotations, incrementing or decrementing as the mouse moves, and then computing the rotation directly each time instead of accumulating rotations. So if you rotated in X by 1 degree 15 times, your current code essentially says "rotate 1 degree in X, add a rotate 1 degree in X, etc, etc". What I'm suggesting is to keep track of a single rotation factor X, and as it changes set the rotation directly "rotate 13 degrees, rotate 14 degrees, rotate 15 degrees, etc".

From your video it also looks like the rotations are too large, so you may wish to scale the final rotations used by a factor of 10 or 50 (or whatever).

I could be way off, but I've run into similar things in my 3D coding adventures and this is my best suggestion. If that's not the issue, since you're dealing with a camera rotation, you may need to use the matrix inverse. Worse comes to worse, look at the modelview matrix for certain rotations and see if it matches what you expect (a pain, but also a valuable debugging exercise).

like image 110
Chris Mennie Avatar answered Sep 27 '22 18:09

Chris Mennie