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