5

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

Jarrett
  • 1,767
  • 25
  • 47

1 Answers1

6

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).

Chris Mennie
  • 564
  • 3
  • 7
  • Hi @Chris Mennie, thanks for posting! ok, so I changed the `tick(..)` function to perform rotations using your suggestion. It now performs rotations like this: `Quaternion pitch; pitch.buildFromAxisAngle(1, 0, 0, (xRot_ * time * rotSpeed_) * math::DEGTORAD); Quaternion heading; heading.buildFromAxisAngle(0, 1, 0, (yRot_ * time * rotSpeed_) * math::DEGTORAD); rotation_ = heading * pitch;` And also, `xRot_` and `yRot_` now accumulate (i.e. `xRot_ += degreeChange;`) changes in degrees. Unfortunately, I'm getting pretty much the same behaviour as before :S – Jarrett Jun 16 '11 at 00:08
  • Forgot to change the `rotSpeed_` to see if that would help, and sure enough, it does! The rotations are now slow enough that I can at least see what is happening..the rotations are still messed up though :) – Jarrett Jun 16 '11 at 00:16
  • got it all fixed now, thanks Chris! The problem was 1) as you said, because this is a Camera, I needed to use the Conjugate of the `Quaternion` and 2) apparently, some of the math I was using to get the change in mouse movement was producing a HUGE `float` value, so I had to cast a couple results to `float`s and it was then producing the correct values. That was a freaking nightmare haha. Glad I fixed it, and learned some good lessons. Thanks alot @Chris Mennie, I really appreciated your feedback! – Jarrett Jun 16 '11 at 00:41
  • Glad I could help. Often it's a combination of things going wrong, which makes it really difficult to figure out :). – Chris Mennie Jun 16 '11 at 01:11
  • fantastic Q+A that helped me (rotating around 1 axis only) to infinately rotate a Quaternion in XNA, to simplify, keep a global of ... float lastRot = 10; and then do the following... Rotation = Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), MathHelper.ToRadians(lastRot)); lastRot += 10; – Marc Jul 19 '11 at 06:25