Sign in to follow this  
Agwan

Help 6dof Camera

Recommended Posts

Hello, I'm trying to make a 6DOF Camera for a space sim, this code seems to work for x or y axis but when a try and use both the result is not as expected. I've been trying to get this working but i just can't figure it out, can anyone see what i'm doing wrong? Quaternion q; public Matrix m; public Vector3 pos; public Vector3 dir; public Vector3 up; public Vector3 right; public void init() { q = Quaternion.Identity; pos.X = 242.2951f; pos.Y = 71.17056f; pos.Z = 87.79854f; dir.X = 0; dir.Y = 0; dir.Z = 1; up.X = 0; up.Y = 1; up.Z = 0; right.X = -1; right.Y = 0; right.Z = 0; } public void cm_run(Matrix View) { m = Matrix.RotationQuaternion( Quaternion.Conjugate(q)); right.X = m.M11; //m[0]; // new right vector right.Y = m.M12; //m[1]; right.Z = m.M13; //m[2]; right.Normalize(); up.X = m.M21; //m[4];// new up vector up.Y = m.M22; //m[5]; up.Z = m.M23; //m[6]; up.Normalize(); dir.X = m.M31; //m[8]; dir.Y = m.M32; //m[9]; dir.Z = m.M33; //m[10]; dir.Normalize(); } public void cm_pitch(float speed) { Quaternion c = Quaternion.RotationAxis(right, speed); q = Quaternion.Multiply(q, c); } public void cm_yaw(float speed) { Quaternion c = Quaternion.RotationAxis(up, speed); q = Quaternion.Multiply(q, c); }

Share this post


Link to post
Share on other sites
A couple of things you could try:

1. Reverse the order of the quaternion multiplications.

2. Build the rotation matrix directly from the quaternion, rather than its conjugate.

Since I'm not sure of the conventions of the math library you're using, these are just random guesses.

A few more questions/comments:

1. It doesn't look like the View argument to cm_run() is used. What's it for?

2. In cm_run(), why are you building the matrix from the quaternion conjugate?

3. You don't need to normalize vectors that have been extracted (indirectly, in this case) from a quaternion representing a pure rotation.

4. Don't forget to normalize your quaternion periodically (to prevent numerical drift).

Share this post


Link to post
Share on other sites
I checked the multiplication order and found nothing however i found a problem getting my vector directions from the matrix, (this is now fixed) but it still has a problem. rotating the camera in a clockwise or anti clockwise direction rolls the camera..? i have no idea what i'm doing wrong, so could u take a look at the code below.. and does anyone have a working demo or knows a link to one.. Thanks..

jyk: cm_run is used to extract the new camera orientation as i'm not doing a FPS style camera but one that's new orientation is based of it's old rotations

class CameraSpace
{

Quaternion q;
public Matrix m;
public Vector3 pos;
public Vector3 dir;
public Vector3 up;
public Vector3 right;

public void init()
{
q = Quaternion.Identity;
pos.X = 242.2951f;
pos.Y = 71.17056f;
pos.Z = 87.79854f;

dir.X = 0;
dir.Y = 0;
dir.Z = -1;

up.X = 0;
up.Y = 1;
up.Z = 0;

right.X = 1;
right.Y = 0;
right.Z = 0;
}


public void cm_run(Matrix View)
{
q.Normalize();
Quaternion t = q;
t.Invert(); // Normalize & Conjugate

m = Matrix.RotationQuaternion(t);

//xaxis
right.X = m.M11;
right.Y = m.M21;
right.Z = m.M31;

//yaxis
up.X = m.M12;
up.Y = m.M22;
up.Z = m.M32;

//zaxis
dir.X = m.M13;
dir.Y = m.M23;
dir.Z = m.M33;

}

public void cm_pitch(float speed)
{
Quaternion c = Quaternion.RotationAxis(right, speed); //createQAxisAngle
q = Quaternion.Multiply(q, c);
}

public void cm_yaw(float speed)
{
Quaternion c = Quaternion.RotationAxis(up, speed);
q = Quaternion.Multiply(q, c);
}

public void cm_roll(float speed)
{
Quaternion c = Quaternion.RotationAxis(dir, speed);
q = Quaternion.Multiply(q, c);
}

}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this