Skip to content

Instantly share code, notes, and snippets.

@pixelnerve
Last active December 19, 2015 17:59
Show Gist options
  • Save pixelnerve/5995780 to your computer and use it in GitHub Desktop.
Save pixelnerve/5995780 to your computer and use it in GitHub Desktop.
Simple Quaternion Camera
void ViewFromPositionAndOrientation( Vec3& p, Quat& q, Mat4* dst )
{
Vec3 negP = q.toMatrix44() * -p;
*dst = Mat4::createTranslation( negP ) * q.toMatrix44();
}
---- Update Method
qPitch.set( RightVector, Math::DegToRad(pitch) );
qYaw.set( UpVector, Math::DegToRad(yaw) );
qRoll.set( ForwardVector, Math::DegToRad(roll) );
qOrientation = qOrientation * qYaw * qPitch * qRoll;
camMove.set( 0, 0, 0 );
if( isKeyPressed )
{
camMove = qOrientation.toMatrix44().inverted() * moveVector;
camMove *= moveSpeed * frameTime;
}
else
{
// Framerate dependent. FIX
moveSpeed *= 0.85f;
}
yaw *= 0.85f;
pitch *= 0.85f;
roll *= 0.85f;
camPos += camMove;
ViewFromPositionAndOrientation( camPos, qOrientation, &viewMatrix );
--- KeyDown Method
isKeyPressed = true;
moveSpeed = 6.0f;
if( event.getCode() == KeyEvent::KEY_UP )
{
moveVector = ForwardVector;
}
if( event.getCode() == KeyEvent::KEY_DOWN )
{
moveVector = -ForwardVector;
}
if( event.getCode() == KeyEvent::KEY_q )
{
moveVector = UpVector;
}
if( event.getCode() == KeyEvent::KEY_a )
{
moveVector = -UpVector;
}
if( event.getCode() == KeyEvent::KEY_LEFT )
{
moveVector = -RightVector;
}
if( event.getCode() == KeyEvent::KEY_RIGHT )
{
moveVector = RightVector;
}
if( event.getCode() == KeyEvent::KEY_t )
{
roll += 3.0f;
}
else if( event.getCode() == KeyEvent::KEY_g )
{
roll -= 3.0f;
}
---- MouseDrag Method
prevMouseX = mouseX;
prevMouseY = mouseY;
mouseX = x;
mouseY = y;
mouseXDelta = mouseX - prevMouseX;
mouseYDelta = mouseY - prevMouseY;
pitch += mouseYDelta * frameTime;
yaw += mouseXDelta * frameTime;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment