101 lines
2.5 KiB
C++
101 lines
2.5 KiB
C++
#ifndef CAMERA_H
|
|
#define CAMERA_H
|
|
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
const float CAMERA_HEIGHT = 1.7f;
|
|
const float GRAVITY = 18.6f; // Should be in m/s^2 but it doesn't really work out with 9.8 so we double it
|
|
|
|
class Camera
|
|
{
|
|
public:
|
|
glm::vec3 position;
|
|
glm::vec3 target;
|
|
glm::vec3 direction;
|
|
glm::vec3 up;
|
|
|
|
// Movement
|
|
glm::vec3 horizontal_velocity;
|
|
glm::vec3 forward_velocity;
|
|
glm::vec3 vertical_velocity;
|
|
|
|
float speed;
|
|
|
|
glm::vec3 worldUp;
|
|
|
|
Camera(float speed, glm::vec3 pos)
|
|
{
|
|
this->speed = speed;
|
|
this->vertical_velocity = glm::vec3(0.0f);
|
|
|
|
this->horizontal_velocity = glm::vec3(0.0f);
|
|
this->forward_velocity = glm::vec3(0.0f);
|
|
|
|
this->position = pos;
|
|
this->target = glm::vec3(0.0f, 0.0f, -1.0f);
|
|
|
|
this->direction = glm::normalize(this->position);
|
|
this->worldUp = glm::vec3(0.0f, 1.0f, 0.0f);
|
|
|
|
glm::vec3 right = glm::normalize(glm::cross(up, this->direction));
|
|
this->up = glm::cross(this->direction, right);
|
|
}
|
|
|
|
Camera(glm::vec3 pos, glm::vec3 target, float speed)
|
|
{
|
|
this->speed = speed;
|
|
this->vertical_velocity = glm::vec3(0.0f);
|
|
|
|
this->horizontal_velocity = glm::vec3(0.0f);
|
|
this->forward_velocity = glm::vec3(0.0f);
|
|
|
|
this->position = pos;
|
|
this->target = target;
|
|
|
|
this->direction = glm::normalize(this->position - this->target);
|
|
this->worldUp = glm::vec3(0.0f, 1.0f, 0.0f);
|
|
|
|
glm::vec3 right = glm::normalize(glm::cross(up, this->direction));
|
|
this->up = glm::cross(this->direction, right);
|
|
}
|
|
|
|
glm::mat4 getView()
|
|
{
|
|
glm::mat4 v = glm::lookAt(this->position,
|
|
this->target + this->position,
|
|
this->worldUp);
|
|
|
|
v = glm::translate(v, glm::vec3(0.0f, -CAMERA_HEIGHT, 0.0f));
|
|
|
|
return v;
|
|
}
|
|
|
|
void move(float time) // time passed in seconds since last move
|
|
{
|
|
bool in_air = this->position.y > 0;
|
|
|
|
if (this->position.y > 0)
|
|
this->vertical_velocity.y -= (GRAVITY * time); // no way! physics reference?
|
|
|
|
glm::vec3 velocity = this->forward_velocity + this->horizontal_velocity + this->vertical_velocity;
|
|
|
|
glm::vec3 movement = (velocity * time);
|
|
this->position += movement;
|
|
|
|
// Don't want to clip into the ground right
|
|
if (this->position.y <= 0 && in_air)
|
|
{
|
|
this->position.y = 0.0f;
|
|
this->vertical_velocity.y = 0.0f;
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|