• Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Getting the Forward Vector from a Camera's Current Rotation

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

#1RunsWithScissors  Members   -  Reputation: 100

Like
0Likes
Like

Posted 01 April 2012 - 10:32 PM

So I am working in C# using XNA and am trying to create a 6DOF camera for an airplane game using quaternions. I have a FPS quaternion camera made and working fine. The rotation of the aircraft camera is perfectly fine, but when I compute the forward vector of the aircraft to figure out the vector in which to apply forward movement, it doesn't always go in the correct direction. So in short, how can I find the current forward vector of a camera using the rotation quaternion in XNA?

I know there are tons of quaternion questions on here, but I haven't found any C# ones that have worked for me.

#2japro  Members   -  Reputation: 768

Like
0Likes
Like

Posted 02 April 2012 - 12:52 AM

I guess the quaternion is used to rotate the "world" into the camera viewspace, so applying the inverse rotation to the view direction in camera space (0,0,1) should give you the desired direction.

#3turch  Members   -  Reputation: 569

Like
0Likes
Like

Posted 02 April 2012 - 07:32 AM

Multiply the quaternion by whatever vector "forward" is in your coordinate system - most likely (0,0,1). You have to have a definition of forward - what direction do you want to get if the rotation is 0.

Edit: use this or this function

#4RunsWithScissors  Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 April 2012 - 05:42 PM

The problem with XNA is that there is no operator overload for multiplying a Vector3 and a quaternion. This is my current line of code to try to calculate the forward vector:

`Vector3 forwardVector = Vector3.Transform(Vector3.Forward, camera.RotationQuaternion);`

It only works in about half of the octants of the 3D space though.

#5RunsWithScissors  Members   -  Reputation: 100

Like
0Likes
Like

Posted 05 April 2012 - 09:51 PM

So I appreciate the help, but I was able to figure it out on my own. So for anyone who encounters this problem with quaternions in XNA, the solution follows:

```Quaternion temporaryQuaternion = camera.RotationQuaternion;
temporaryQuaternion.Conjugate();
Vector3 forwardVector = Vector3.Transform(Vector3.Forward, temporaryQuaternion);```

If anyone can explain the math behind why you use the invese of the rotation quaternion to apply the transform to the world forward, I'd love to hear it.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS