Search code examples
javaopengllwjglgame-physics

OpenGL Upwards Movement


I am creating a game using LWJGL OpenGL. To move the player (a spaceship), I move X and Z based off of the player's Y-axis rotation:

float dx = distMoved * sin(toRadians(getRotY())));
float dz = distMoved * cos(toRadians(getRotY())));

This all works fine, however, for upward movement, I use this code, which kinda works:

float dy = distMoved * sin(toRadians(getRotX())));

The problem is, it seems that my player is moving the same amount forwards (X and Z) regardless of my up and down rotation, which means my player can be facing 90 degrees upwards, and still move X and Z.

I have tried subtracting the dy from 'distMoved' before calculating the X and Z movement, yet that does not provide correct movement.

Is anybody know what I should do?


Solution

  • dx and dz both have to be multiplied by cos(toRadians(getRotX()))).