1

So I'm making this FPS game where I want to move forward, backward, left and right with keyboard input and look around with the camera like in a real FPS-game like Battlefield. The camera movement in combination with the keyboard input works great, but now my camera can fly around. And I just want to be able to stay on the "ground" and move forward, backward, left and right while looking up or down like in a game like battlefield without flying around (like no-clip does). Now If I look down or up and press forward on my keyboard I can "fly", but I don't want this to happen.

A friend of my suggested to use something like this to go forward.

Position += glm::vec3(glm::cos(Yaw), glm::sin(Yaw), 0) * velocity

instead of:

Position += Front * velocity;

But I don't fully understand how this would work?

This is the current keyboard input code:

void Camera::ProcessKeyboard(Camera_Movement direction, float deltaTime)
{
float velocity = MovementSpeed * deltaTime;
if (direction == FORWARD)
    Position += Front * velocity;
if (direction == BACKWARD)
    Position -= Front * velocity;
if (direction == LEFT)
    Position -= Right * velocity;
if (direction == RIGHT)
    Position += Right * velocity;
}

Tips or help would be appreciated! Yes this code comes from LearnOpenGL.com What we are doing here is trying to simulate movement by moving all objects in the scene in the reverse direction, giving the illusion that we are moving.

Vengenecx
  • 105
  • 8
  • 1
    When I once made a walk navigation, I distinguished body position/direction from head direction. For the camera position, I used the head angles as "offset" to the body position/direction. For the walking, I considered (modified) the body position/direction only. Hence, I could even look to the side while walking - not quite easy to manage with keyboard/mouse but reflecting the reality. (Though, if I do this in reality I'm prepared to stumble - better look where you walk.) ;-) – Scheff's Cat Apr 01 '22 at 13:46
  • 1
    check all the links in the end of this answer [Understanding 4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) – Spektre Apr 02 '22 at 05:29

2 Answers2

1

To make the camera stay on the ground, simply don't move it up and down.

Yeah, the part where the camera moves up and down? You're the one who wrote that code. Delete it.

You didn't say where this Front vector comes from. It comes from a place that is taking pitch and yaw into account. Delete the pitch part.

Your friend's suggestion glm::vec3(glm::cos(Yaw), glm::sin(Yaw), 0) is a way to calculate Front using only yaw.

As your game gets more advanced you may also implement jumping and gravity, so the player can go up and down to a limited extent.

user253751
  • 57,427
  • 7
  • 48
  • 90
0

I want to thank everyone for helping me out! I understand better how everything works now. I want to move in the x-z plane and y must be zero (yes I chose the Y-axis as "up" or "the sky"), because I don't want the camera to move up or down. So when I go forward I only want to change the x and z parameter of the glm vector!

void Camera::ProcessKeyboard(Camera_Movement direction, float deltaTime)
{
    float velocity = MovementSpeed * deltaTime;
    if (direction == FORWARD) {
        // glm::vec3(X,Y,Z)!!! we only want to change the X-Z position
        Position += glm::vec3(glm::cos(glm::radians(Yaw)), 0, glm::sin(glm::radians(Yaw))) * velocity; //Y is not affected, Y is looking up
    }
    if (direction == BACKWARD) {
        // glm::vec3(X,Y,Z)!!! we only want to change the X-Z position
        Position -= glm::vec3(glm::cos(glm::radians(Yaw)), 0, glm::sin(glm::radians(Yaw))) * velocity; //Y is not affected, Y is looking up
    }
    if (direction == LEFT) {
        Position -= Right * velocity;
    }
    if (direction == RIGHT) {
        Position += Right * velocity;
    }

}

Now everything works great!

Vengenecx
  • 105
  • 8