Abdision
Abdision

Reputation: 31

SDL OpenGL smooth camera movement

I am trying to implement a camera motion system into the program I am currently working on. I cant figure out how to move the camera in non-discrete units so when I move the camera it "jumps" to the next position. how can I move the camera smoothly without jumps.

CameraControlls Function:

void CameraControlls()
{  

while (SDL_PollEvent(&e))
{
    if (e.type == SDL_QUIT)
    {
        exit(0);
    }

    switch (e.type)
    {
    case SDL_KEYDOWN:
        switch (e.key.keysym.sym)
        {
        case SDLK_w:
            cam.MoveForward(0.3f);
            break;
        case SDLK_s:
            cam.MoveBackward(0.3f);
            break;
        case SDLK_d:
            cam.MoveRight(0.2f);
            break;
        case SDLK_a:
            cam.MoveLeft(0.2f);
            break;
        case SDLK_ESCAPE:
            exit(0);
            break;
        case SDLK_RIGHT:
            cam.Yaw(-0.01f);
            break;
        case SDLK_LEFT:
            cam.Yaw(0.01f);
            break;
        case SDLK_DOWN:
            cam.Pitch(0.01f);
            break;
        case SDLK_UP:
            cam.Pitch(-0.01f);
            break;
        }
      }
   }
}

Camera Structure:

struct Camera
 {
 public:
Camera(glm::vec3& pos, float fov, float aspect, float zNear, float zFar)
{
    this->pos = pos;
    this->forward = glm::vec3(0.0f, 0.0f, 1.0f);
    this->up = glm::vec3(0.0f, 1.0f, 0.0f);
    this->projection = glm::perspective(fov, aspect, zNear, zFar);
}
inline glm::mat4 GetViewProjection() const
{
    return projection * glm::lookAt(pos, pos + forward, up);
}
void MoveBackward(float amt)
{
    pos -= forward*amt;
}
void MoveForward(float amt)
{
    pos += forward * amt;
}
void MoveRight(float amt)
{
    pos -= glm::cross(up, forward) * amt;
}
void MoveLeft(float amt)
{
    pos += glm::cross(up, forward) * amt;
}
void Pitch(float angle)
{
    glm::vec3 right = glm::normalize(glm::cross(up, forward));
    forward = glm::vec3(glm::normalize(glm::rotate(angle, right) * glm::vec4(forward, 0.0)));
    up = glm::normalize(glm::cross(forward, right));
}
void Yaw(float angle)
{
    static const glm::vec3 UP(0.0f, 1.0f, 0.0f);
    glm::mat4 rotation = glm::rotate(angle, UP);
    forward = glm::vec3(glm::normalize(rotation * glm::vec4(forward, 0.0)));
    up = glm::vec3(glm::normalize(rotation * glm::vec4(up, 0.0)));
}
   private:
glm::mat4 projection;
glm::vec3 pos;
glm::vec3 forward;
glm::vec3 up;
};

Upvotes: 1

Views: 2634

Answers (1)

julito30
julito30

Reputation: 49

I guess the key event of sdl is not firing every frame so you shouldn't change the camera position directly from there because camera wont update every frame.

I would create a boolean variable MOVE_FORWARD that will represent if the forward key is pushed or not

  • In the key event you update the MOVE_FORWARD variable.

    while (SDL_PollEvent(&e))
    {
    ..........
        case SDL_KEYDOWN:
            switch (e.key.keysym.sym)
            {
            case SDLK_w:
                MOVE_FORWARD=true;
                break;
    
  • In every frame you update the position of the camera

    if (MOVE_FORWARD){pos+=0.3}
    

This way the camera updates its position every frame and not only when the key event fires


You could also avoid creating key state variables using SDL_GetKeyboardState(NULL) instead of SDL_PollEvent(&e):

Uint8* keystate = SDL_GetKeyState(NULL);    
if(keystate[SDL_SCANCODE_W])
{
  pos+=0.3;
}

Upvotes: 2

Related Questions