So far I've implemented horizontal (X-axis) and vertical (Z-axis) linear movement for the player in a 3D game. Using the code shown below, the player is able to move relative to the camera's Y-axis rotation.
rotationOffset represents the direction:
- 0.f = Forward
- 90.f = Right
- 180.f = Backwards
- -90.f = Left
velocity.x = glm::sin(cameraYRotation + rotationOffset) * speed; velocity.z = glm::cos(cameraYRotation + rotationOffset) * speed;
I use key press & release events to set the velocity using the code above.
However, I'm not sure how I can implement diagonal movement, such as when the player is moving Forward + Right.
If the player is initially moving Forward, but then starts moving to the right as well to move diagonally, how would can I apply that change to the velocity?
Should I create more variables to keep track of the keys being pressed?
Furthermore, how do I deal with key releases (e.g. Player initially moves diagonally Forward + Right, but then releases the Right key, thus only moves Forward)?