Skip to main content
gamedev.net
🔒 Locked

FPS Camera

Started by VanillaSnake21 5 replies 850+ views
Original Post
VanillaSnake21
VanillaSnake21
Hi, is there any point in using Quaternions for a first person camera? Or is better to use matrices? My camera can use both but, with quaternions its not "smoothly" rotated or translated (for example if I translate the camera back and go up at the same time i get little gerks to the left or righ) but with matricies its fine. Thnx, Tim.
You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts
dv
dv
With Quaternions you can only represent rotations, so you must be using a separate vector for translations. Anyway, how do you interpolate rotation? Do you use slerp? Or nlerp? How do you interpolate the translation?
~dv();
VanillaSnake21
VanillaSnake21
Quote:
Original post by dv
With Quaternions you can only represent rotations, so you must be using a separate vector for translations. Anyway, how do you interpolate rotation? Do you use slerp? Or nlerp? How do you interpolate the translation?


Basically I neither slerp nor nlerp, why would I do that? I mean I could slerp and I do slerp in some cases, for example if I want to reset my camera to a starting rotation, i slerp to that rotation. But for movement I still use the 3 vectors, Look, Up, right, but at every step I calculate the quaternion rotation around these exes and then I make a matrix from that quaternion. Im not sure how I can slerp if its a first person camera and it moves with the mouse.
You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts
sirGustav
sirGustav
With quaternions i usually do something like this

// initQuaternion oldRotation = Identity();Quaternion newRotation = Identity();// stepMouseMovement mouse = grabLatestMouseMovement();oldRotation = newRotation;newRotation *= QuaternionFromAxisAngle(yPositive, mouse.dy * config.sensitivity_y * deltaT);newRotation *= QuaternionFromAxisAngle(oldRotation.getRight(), mouse.dx * config.sensitivity_x * deltaT);// renderorientCameraAcordingWithRotation( interpolate(oldRotation, deltaT_render, newRotation) );


where getRight() (and similar functions such as getUp() & getIn() roughly look like:

vec3 Quaternion::getRight() {  return rotatePointAroundOrigin(xPositive);}


hth
Zipster
Zipster
Quaternions are really overkill for a FPS camera. The mouse's vertical movement controls pitch, and its horizontal movement controls yaw, which makes Euler angles the most logical representation. You let YPRx (or xRPY) be your transformation order and expand out the matrix multiplication symbolically to get rid of multiplication by 0. Plug in yaw, pitch, roll, and the offset vector and you have the camera-space matrix. Invert using the special case method for homogeneous transformation matrices, and you have the matrix you pass to the API. All in all it goes something like this:

matrix4x4f getViewMatrix(){   matrix4x4f m = matrix4x4f::identity();   float cy = cos(yaw);   float sy = sin(yaw);   float cp = cos(pitch);   float sp = sin(pitch);   float cr = cos(roll);   float sr = sin(roll);   m[0][0] = (some expression using the above values);   m[0][1] = (some expression using the above values);   // etc. etc. for the upper-left 3x3 elements   if(isAPIOpenGL())      m[right column] = offset;   else      m[bottom row] = offset;   return homogeneousInverse(m);}


I made some liberties in the code just to demonstrate the process. You can actually eliminate the inverse completely if you set up the initial matrix correctly. Consider that a quaternion->matrix conversion is just as long, minus the offset part. And you still need to perform the original quaternion construction.

Quaternions and FPS cameras are a pet peeve of jyk so he'll probably add anything I've missed [grin]
Zakwayda
Zakwayda
Quote:
Original post by Zipster
Quaternions are really overkill for a FPS camera. The mouse's vertical movement controls pitch, and its horizontal movement controls yaw, which makes Euler angles the most logical representation. You let YPRx (or xRPY) be your transformation order and expand out the matrix multiplication symbolically to get rid of multiplication by 0. Plug in yaw, pitch, roll, and the offset vector and you have the camera-space matrix. Invert using the special case method for homogeneous transformation matrices, and you have the matrix you pass to the API.
Ah, you beat me to it! I guess I'll just have to lie in wait for the next 'quaternion-based FPS camera' post (muhaha).

:)
VanillaSnake21
VanillaSnake21
K, thanks for responses guys
You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.