# Using a quaternion and a vector to define a plane

This topic is 3894 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, I am doing an assignment where I need to create a simple raycast program. My problem is that the image plane (through which the rays are shot through from the camera to the objects) is to be defined by a quaternion and a (x,y,z) vector, and I don't know how to do this. Specifically, the quaternion and the (x,y,z) vector define the position and the orientation of the image plane. Can anybody offer any tips on how to approach this? Thanks

##### Share on other sites
If you're moving the camera through the scene, the camera has an orientation and a position in the scene.
If you're moving the scene around the camera, the scene will have a rotation and an offset.
These two modes are basically the same, but my examples below assume you're moving the world around the camera.
So, to represent the orientation of the scene, you need a rotation (which is what quaternions do). To represent the offset, you need 3 coordinates (x,y,z) - which is what the vector stores. So all you need to figure out is how to pass this data to OpenGL.

The translation / position is trivial : glTranslatef(vec.x, vec.y, vec.z);

The rotation is a bit trickier, because you need to convert the quaternion into data which OpenGL understands. In particular, you need to convert the quaternion data into rotation about an arbitrary axis:
float theta, ax, ay, az;Quaternion q;        // Use the camera's quaternion here!q.normalize();theta = 114.591559f * acos(q.w);float s = sqrt(1.0f - q.w*q.w);if(abs(s) < 0.0005f) s = 1;       // prevents problems near and at s == 0ax = q.x / s;ay = q.y / s;az = q.z / s;glRotatef(theta, ax, ay, az);

I Recommend you Put the above code block into a member function of the Quaternion class, so you can convert a quaternion into a rotation about an arbitrary axis any time you may need it.

Hope this helps!

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 16
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633360
• Total Posts
3011524
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!