## News:

LWJGL 3.3.2 released

## Unrestrained Motion Through 3D Space

Started by TimmerCA, April 11, 2012, 04:08:45

#### TimmerCA

Hi,

I'm trying to whip up a quick LWJGL game that allows the player to effectively fly through space.  There is no gravity, and for the purposes of the project at this point, there are no collisions, and there is no roll ability for the vehicle at this time (that is, there is no rotation about the "Z" axis for now).

So, I've got the following declarations:

In my game loop, I'm doing:

And that part seems to be working fairly well.  But, the next step is to calculate my new position based on my current yaw and pitch, and that's where my trigonometry is failing me badly.  Currently, I have:

But I know that's wrong.  What's the magic formula to calculate my new location based on the current location and my yaw and pitch?

#### matheus23

Got that from a very old GLUT c++ project. I think this should give you a good idea Also, rotation about the z axis is included. Only problem: Z position is not calculated from the mouse angle... That might be a problem for you ...
My github account and currently active project: https://github.com/matheus23/UniverseEngine

#### TimmerCA

So, I've made some progress here but I'm still not done.

I figured out that I need three Vector3f variables:

During each game loop, I adjust my location based on my velocity:

If the player is holding down the left mouse button, I adjust the rotation:

Then during the drawing portion of my game loop, I use the rotation variable to translate the display into the proper orientation and the location variable to translate into the proper location:

All of this works absolutely beautifully.

But here's what I'm still confused about:

If the player is holding down the right mouse button, I want to accelerate the vehicle in the direction that the player is currently facing.  How do I adjust the velocity variable based on the current rotation?

#### CodeBunny

Basic trig functions.

This is kind of a weird control system... why doesn't the ship accelerate in the direction it's facing? You could give the player a way to turn the ship. That way, it'd be like Asteroids controls (which are fairly decent in showing space momentum).

#### TimmerCA

Quote from: CodeBunny on April 12, 2012, 12:27:15
Basic trig functions.

I know that.  Which ones?  The closest I can come is:

The y and z accelerations work well, but the x one is off.  If I comment out the x acceleration line, then the system works as I expect it to on the y and z axis.

Quote from: CodeBunny on April 12, 2012, 12:27:15
This is kind of a weird control system... why doesn't the ship accelerate in the direction it's facing? You could give the player a way to turn the ship. That way, it'd be like Asteroids controls (which are fairly decent in showing space momentum).

We're saying the same thing.  Then you press the mouse button, the ship will accelerate in the direction it's facing.  When I said "the direction the player is facing", I meant "the direction the ship is facing".

#### CodeBunny

What is the vector in the direction of rotation [0, 0, 0]?

#### randfur

When dealing with rotation you often have to make a decision about what direction your angles go in (left/right, up/down, etc.).
Basically as CodeBunny was asking, decide in what orientation (0,0,0), (90,0,0), (0,90,0) and (0,0,90) put you in and then decide in what order the axis rotations occur.
These decisions because the basis of your trig equations.

Personally I prefer to use quaternions for rotation because once you get all the complex math down it becomes much simpler conceptually compared with hand crafted trig expressions.

#### TimmerCA

Quote from: randfur on April 15, 2012, 09:49:26Personally I prefer to use quaternions for rotation because once you get all the complex math down it becomes much simpler conceptually compared with hand crafted trig expressions.

I'm willing to learn something new if it is the better way to do it.  I'll Google around for quaternions and see what I can learn.

#### randfur

I'm happy to share what I know, feel free to post more questions on quaternions if you need answers.
You should also check out this algorithm shown under "Pseudo-code for rotating using a quaternion".
http://en.wikipedia.org/w/index.php?title=Quaternions_and_spatial_rotation&oldid=362009945#Performance_comparisons_with_other_rotation_methods