Chapter 9 - Forward Vector for 3D

Hi Caroline,

I’m trying to implement space rocket movement like pitch, yaw, roll and accelerate with key bindings.

I’ve modified the forward vector for 3D space like;
normalize([sin(rotation.y), -tan(rotation.x), cos(rotation.y)])
but it is still problematic. How can i achieve that problem?

My project’s github link is solarsystem-metal. You can download it for testing. After you run macOS version, you can change the focus camera with pressing 1 and 4 keys and rocket movement with w,a,s,d,q,e and space.

Here is my little rocket :slight_smile:

My other question is about the Right Vector;
return [forwardVector.z, forwardVector.y, -forwardVector.x]
I dont understand the usage of this, could you explain with more detail?

Best Regards,

I love your space model, especially the ambient lighting on planet earth!

Your question is quite difficult to answer, because the movement is in three dimensions.

The book code is in two dimensions, as the player only moves on x and z.

To explain the right vector…

The player has a forward vector and a right vector, which is at right angles to the forward vector.

The movement code is:

player.position +=
        (direction.z * player.forwardVector
          + direction.x * player.rightVector)
        * translationSpeed

When you press w, the player moves ahead in his currently facing direction. In the first example on the left, the direction is [0, 0, 1] (that’s from the w key), meaning go forward in the z direction.

The result of multiplying direction by forward and right is (1 * (0, 0, 1) + 0 * (1, 0, 0)) - that’s (0, 0, 1), so the player goes straight forward in world coordinates.

In the second example, the result is (1 * (0.707, 0, 0.707) + (0 * (0.707, 0.0, -0.707), resulting in (0.707, 0, 0.707), and the player goes along his forward vector.

When you press a, the direction is (-1, 0, 0), so the forward vector will be multiplied by zero, so only the negative of the right vector is used.

So the combination of keys gives the direction, and then when you multiply the z direction by the player’s forward vector and the x direction by the right vector, you get the final vector along which the player should travel.

1 Like

Hi again,

Thank you very much about right vector explanation :hugs:

About three dimension forward vector, is there any document that you could suggest?
For now, i can continue with two dimensions because i spent a bit more time about figuring it, in future i would like to go back for fixing it.


It’s a matter of doing the math. There’s a great Math book recently set free. This chapter is on rotations:

Quaternions might be a good fit here.

1 Like

@thaliloglu - I played around with how to do 3d movement using quaternions. I forked and created a branch from your GitHub. This is your scene with an extremely large rocket in it :smiley:

The movement is extremely naive. Spacebar only moves rocket along forward vector while pressed, and I guess you’d need to apply some law of motion. There’s no decay in acceleration and no thrust dynamics, etc.

But it does show how to rotate in 3D using quaternions.

It only works on macOS - I didn’t even look at what it does to iOS.

1 Like

That is great! Thank you very much for your effort and time. It helped me a lot. I really appreciated.

1 Like