# 6DOF movement for a space game

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

## Recommended Posts

Hello, I had a question on how to use 6DOF movement in my game. I currently, and very recently, know how to use matrix rotations with vectors but need the 6DOF movement to do what I want correctly. Any help on this subject, be it a link to a website with the math for it or how to make the movement, a post on how to do it, or what ever. Thanks for readin, hope to hear back soon.

##### Share on other sites
There was a good article online about this by Ron Levine, but I can't seem to find it at the moment. Meanwhile, here is source code that implements what you want; let me know if you have any questions about it, or need help converting it for a particular API or math library.

##### Share on other sites
thanks for the quick reply :) I'm working in JOGL so it'll be tricky to rewrite, but it might get me on my way :) if there is anymore information you or anyone else has, feel free to post. :D

##### Share on other sites
I don't know much about JOGL, but the thing about 6DOF motion (unlike Euler-angle based motions such as in a first-person shooter) is that you pretty much have to do some math on your side of things, whether it be with vectors, matrices, or quaternions.

What that translates to in Java I'm not sure, but in any case it can be fairly awkward to implement 6DOF motion exclusively via OpenGL function calls.

##### Share on other sites
lol, i dont want it in OpenGL functions. I want to write the constructors and functions of the vectors, matricies, and quaternions to do 6DOF. I like to be able to see it happening so I want everything in my own classes (compiled source files). I have a vector class written, I believe thats the basic start of a rotation system. If anyone has information on how to accomplish 6DOF using vectors, matrices, and quaternions (studying quaternions but dont comprehend yet). I dont care if you decide to show source code in a different language such as c, c++ because its easy enough to rewrite. Thanks for everyones help :) lookin forward to next post.

##### Share on other sites
I've never done 6DOF movement myself (just done the typical FPS camera which is pretty straightforward), but I have an idea as to how it might be done. Correct me anyone if i'm wrong here, i'm just speculating- but this is how I would approach it myself. This technique uses matrices, its probably easier to implement with quaternions seeing as they are very easy to rotate about any arbitrary axis, but since you are probably more familiar with matrices I'll describe how to do it with them...

Ok, so with 6DOF the basic idea is that for every rotation you rotate about the previous orientation or axes of the camera instead of just the normal xyz axes.

You may or may not be already be aware that the orientation of a camera, or indeed any rotation is made up of three separate vectors which form a 3x3 matrix. The names of these vectors are 'up' , 'left' and 'forward'. I'm using the openGL coordinate system here, so foward (or the positive side of the z axis) actually comes in from the screen, rather than going into it as we would normally think:

up (y)

|
|
|
|_______ left (x)
/
/
/ forward (z)

When the camera is at the default or natural orientation of the coordinate system (xyz axes) these three vectors would have the following x,y,z cooridinates:

left ( 1 , 0 , 0 )
up ( 0 , 1 , 0)
forward ( 0 , 0 , 1 )

Ok, so this is the default orientation of the camera. When we want to do the following movements in space we would do the following rotations about the following axes:

yaw ( turn camera left and right ) : rotate the left and forward vectors about the up vector by desired amount

pitch ( move camera up and down- like looking up and down in a FPS ) : rotate the up and forward vectors about the left vector by desired amount

roll ( like the circular camera movements in the original elite or how an aircraft turns ) : rotate the up and left vectors about the forward vector by the desired amount.

So this is how you would modify the 3 vectors that describe the orientation of the camera when you want to do each of the following movements. So the question is how do we rotate vectors about another arbritarty vector ? Doing rotations about the xyz axes is straight forward enough, and the matrices to do so are not too complicated, but rotation about any vector is slightly more difficult. Luckily there is a genralised matrix that can do this for you. This is taken directly from my own maths notes:

Where 'A' is the angle of rotation and 'V' is the vector to rotate about and Vx Vy Vz are the xyz components of this vector, the rows and columns of the 3x3 matrix which rotates by A around vector V is found by:

row1 col1: (1-cosA)(Vx)(Vx) + cosA
row1 col2: (1-cosA)(Vx)(Vy) - (Vz)(sinA)
row1 col3: (1-cosA)(Vx)(Vz) - (Vy)(sinA)
row2 col1: (1-cosA)(Vx)(Vy) + (Vz)(sinA)
row2 col2: (1-cosA)(Vy)(Vy) + cosA
row2 col3: (1-cosA)(Vy)(Vz) - (Vx)(sinA)
row3 col1: (1-cosA)(Vx)(Vz) - (Vy)(sinA)
row3 col2: (1-cosA)(Vy)(Vz) + (Vx)(sinA)
row3 col3: (1-cosA)(Vz)(Vz) + cosA

Phew.. Thats a lot of maths! [wink] But if you constuct the matrix as such, you can use it to transform any vector about the vector 'V' at the desired angle 'A'. Just mutiply the vector you want to be transformed by this matrix and it will be done.

So how do you incorporate this to make up a transform matrix which will orientate the view in OpenGL ? You may or may not be familar with 4x4 matrices and how openGL uses them to transform objects, but basically they are the same as 3x3 matrices except they also incorporate information about translation as well as rotation. The rows and columns of this matrix are made up of the following components:

leftVector_x , upVector_x , forwardVector_x , translationVector_x
leftVector_y , upVector_y , forwardVector_y , translationVector_y
leftVector_z , upVector_z , forwardVector_z , translationVector_z
0 , 0 , 0 , 1

As you can see your view vectors slot nicely into this matrix. But how do you incorporate translations as well as rotations ? Normally you would want to translate first (think about this in your head) and then rotate all the objects about the position the camera is in. Well first construct a matrix which just describes a translation as follows:

1 , 0 , 0 , translationVector_x
0 , 1 , 0 , translationVector_y
0 , 0 , 1 , translationVector_z
0 , 0 , 0 , 1

Then multiply it by the matrix which describes your camera orientation which is as follows:

leftVector_x , upVector_x , forwardVector_x , 0
leftVector_y , upVector_y , forwardVector_y , 0
leftVector_z , upVector_z , forwardVector_z , 0
0 , 0 , 0 , 1

This should cause the translation to be done first and then rotation. Once you have done this, you now have your final matrix which can be used to do transforms in OpenGL. Use glLoadMatrix() to load your matrix and set it as the current transform matrix.

Thats about it. There are some issues with this however you should be aware of, which are directly to do with floating point precision. Continious multiplications and additions will result in a build up of error, so you will need to take the following precautions:

(1) You should constantly be normalising your camera vectors to ensure they stay of unit lenght. Just figure out each vectors lenght and divide by it to normalise. View vectors must stay at unit lenght, its just the way the math works.

(2) All three of the view vectors should always be perpendicular to each other- in other words the dot product of any two of the view vectors should always be zero. You can pick a vector to make the others perpendicular against, and just solve a linear equation such that Vector 1 dot Vector 2 equals zero. If this is not done your transforms could get gradually skewed and distorted.

Well thats all I have to say about this. Hopefully this will help, but you should look elsewhere on the internet because my explantion alone will probably not be enough for you to completely understand what is happening.

Best of luck anyhows with your space game, send me some screenshots when its done [wink]

Regards,
Darragh

##### Share on other sites
oh wow :D this was more than I could of hoped for :) I'll get to it immediately, I hope it goes smoothly. thanks for your detailed reply :) if anyone else has any ideas, methods, source code, or anything that might help, dont hesitate :D

##### Share on other sites
Quote:
 Original post by MythProgrammeroh wow :D this was more than I could of hoped for :) I'll get to it immediately, I hope it goes smoothly. thanks for your detailed reply :) if anyone else has any ideas, methods, source code, or anything that might help, dont hesitate :D

No problem, your welcome. Coincidently I am also revising for a math exam at the moment (which comprises of a lot of this stuff) so it kinda doubles up as revision for me too! [smile] Ya hopefully it will work, like I said its untested but it sounds right- in theory at least... Communism is also supposed to work in theory too though, so don't take it as being 100% correct! [lol]

Ah sure give it a go anyhow, it may well do the trick.

Good luck!

##### Share on other sites
lol if this works, i dunno :P lol, i'll probably praise ur name and have it appear in the game somewhere :D lol thanks man, glad i didnt hesitate to ask for help when i needed it :)

##### Share on other sites
Quote:
 Original post by MythProgrammerlol, i dont want it in OpenGL functions. I want to write the constructors and functions of the vectors, matricies, and quaternions to do 6DOF. I like to be able to see it happening so I want everything in my own classes (compiled source files). I have a vector class written, I believe thats the basic start of a rotation system. If anyone has information on how to accomplish 6DOF using vectors, matrices, and quaternions (studying quaternions but dont comprehend yet). I dont care if you decide to show source code in a different language such as c, c++ because its easy enough to rewrite.
I imagine Darragh's post probably gave you all the necessary info, but I'd just like to clarify that the source code I directed you to does not implement 6DOF motion via OpenGL functions, but rather through vectors and matrices, exactly as you requested. The only gl function calls are (I believe) glMultMatrix() and the like, which are simply for getting your matrices to OpenGL. So for what it's worth, the linked code is in fact a working example of 6DOF motion using custom math classes, so if you find yourself needing another reference I encourage you to check it out.

[Edit: typo]

[Edited by - jyk on May 8, 2006 10:55:37 PM]

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 10
• ### Forum Statistics

• Total Topics
633653
• Total Posts
3013153
×