Original Post
I have a working matrix based camera, but I am trying to use quaternions since they are more efficient. Currently there is a phenomena where if i rotate upwards, than left, than downwards, then right, repeatedly, the camera seems to unexpectly start rolling on the z axis each time
I wonder if anyone has source for a 6DOF quaternion based camera I can look at?
All examples on the net seem to be FPS type that won't let you rotate 360 degrees up and down.
and or alternativly maybe someone can find the problem in my source:
#ifndef CAMERA_H
#define CAMERA_H
//-----------------------------------------------------------------------------------------------
// camera.h
//
// a quaternion based camera
//
// Christopher Pisz
// 03/14/04
//-----------------------------------------------------------------------------------------------
#include <d3dx9.h>
class Camera
{
public:
Camera();
~Camera();
D3DXMATRIX * Update(); // Updates the camera position
void Rotate(D3DXVECTOR3& axis, float angle); // Rotates the camera
void Translate(float distance, D3DXVECTOR3& direction); // Translates the camera position along a 6DOF vector
private:
D3DXVECTOR3 m_position; // Position of camera within world space
D3DXQUATERNION m_rotation; // Rotation quaternion
D3DXMATRIX m_view; // The view matrix
};
#endif // CAMERA_H
//-----------------------------------------------------------------------------------------------
// camera.cpp
//
// a quaternion based camera
//
// Christopher Pisz
// 03/14/04
//-----------------------------------------------------------------------------------------------
#include "camera.h"
#include <stdio.h>
Camera::Camera()
{
m_position = D3DXVECTOR3(0.0f, 0.0f, -5.0f);
D3DXQuaternionIdentity(&m_rotation);
D3DXMatrixIdentity(&m_view);
}
Camera::~Camera()
{
}
//-----------------------------------------------------------------------------------------------
void Camera::Rotate(D3DXVECTOR3& axis, float angle)
{
D3DXQUATERNION rotation_new; // The new rotation
// Build a new rotation quaternion
D3DXQuaternionIdentity(&rotation_new);
D3DXQuaternionRotationAxis(&rotation_new, &axis, angle);
// Add the effects of the new rotation to our old rotation
D3DXQuaternionMultiply(&m_rotation, &rotation_new, &m_rotation);
Update();
}
//-----------------------------------------------------------------------------------------------
void Camera::Translate(float distance, D3DXVECTOR3& direction)
{
D3DXVECTOR3 axis_x, axis_y, axis_z, amount;
D3DXVec3Normalize(&direction, &direction);
amount = direction * distance;
// Move in the X direction
axis_x.x = m_view._11;
axis_x.y = m_view._21;
axis_x.z = m_view._31;
axis_x *= amount.x;
m_position.x += axis_x.x;
m_position.y += axis_x.y;
m_position.z += axis_x.z;
// Move in the Y direction
axis_y.x = m_view._12;
axis_y.y = m_view._22;
axis_y.z = m_view._32;
axis_y *= amount.y;
m_position.x += axis_y.x;
m_position.y += axis_y.y;
m_position.z += axis_y.z;
// Move in the Z direction
axis_z.x = m_view._13;
axis_z.y = m_view._23;
axis_z.z = m_view._33;
axis_z *= amount.z;
m_position.x += axis_z.x;
m_position.y += axis_z.y;
m_position.z += axis_z.z;
Update();
}
//-----------------------------------------------------------------------------------------------
D3DXMATRIX * Camera::Update()
{
// Build a new view matrix
D3DXMATRIX translation;
D3DXMatrixTranslation(&translation, -m_position.x, -m_position.y, -m_position.z);
D3DXMATRIX rotation;
D3DXMatrixRotationQuaternion(&rotation, &D3DXQUATERNION(-m_rotation.x, -m_rotation.y, -m_rotation.z, m_rotation.w));
D3DXMatrixMultiply(&m_view, &translation, &rotation);
return &m_view;
}
[edited by - brekehan on March 29, 2004 12:37:02 AM]