added a player class
This commit is contained in:
parent
de39d10776
commit
5920376262
126
src/Camera.h
126
src/Camera.h
@ -4,74 +4,32 @@
|
|||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
#include "World.h"
|
#include "Player.h"
|
||||||
|
|
||||||
const float CAMERA_HEIGHT = 1.8f;
|
|
||||||
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
|
class Camera
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
glm::vec3 position;
|
Player* player;
|
||||||
glm::vec3 target;
|
glm::vec3 target;
|
||||||
glm::vec3 direction;
|
glm::vec3 direction;
|
||||||
glm::vec3 up;
|
glm::vec3 up;
|
||||||
|
|
||||||
// Movement
|
|
||||||
glm::vec3 horizontal_velocity;
|
|
||||||
glm::vec3 forward_velocity;
|
|
||||||
glm::vec3 vertical_velocity;
|
|
||||||
|
|
||||||
bool grounded;
|
|
||||||
|
|
||||||
float speed;
|
|
||||||
|
|
||||||
glm::vec3 worldUp;
|
glm::vec3 worldUp;
|
||||||
|
|
||||||
Camera(float speed, glm::vec3 pos)
|
Camera(Player* player)
|
||||||
{
|
{
|
||||||
this->speed = speed;
|
this->player = player;
|
||||||
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->target = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||||
|
|
||||||
this->direction = glm::normalize(this->position);
|
this->direction = glm::normalize(this->player->position);
|
||||||
this->worldUp = glm::vec3(0.0f, 1.0f, 0.0f);
|
this->worldUp = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||||
|
|
||||||
glm::vec3 right = glm::normalize(glm::cross(up, this->direction));
|
glm::vec3 right = glm::normalize(glm::cross(up, this->direction));
|
||||||
this->up = glm::cross(this->direction, right);
|
this->up = glm::cross(this->direction, right);
|
||||||
|
|
||||||
this->grounded = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
this->grounded = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::mat4 getView()
|
glm::mat4 getView()
|
||||||
{
|
{
|
||||||
glm::vec3 real_camera_pos = this->position;
|
glm::vec3 real_camera_pos = this->player->getEyePosition();
|
||||||
real_camera_pos.y += CAMERA_HEIGHT;
|
|
||||||
|
|
||||||
glm::mat4 v = glm::lookAt(real_camera_pos,
|
glm::mat4 v = glm::lookAt(real_camera_pos,
|
||||||
real_camera_pos + this->target,
|
real_camera_pos + this->target,
|
||||||
@ -80,78 +38,8 @@ class Camera
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
void move(World* world, float time) // time passed in seconds since last move
|
|
||||||
{
|
|
||||||
// bool in_air = this->position.y > 1;
|
|
||||||
|
|
||||||
// if (this->position.y > 1)
|
|
||||||
// this->vertical_velocity.y -= (GRAVITY * time); // no way! physics reference?
|
|
||||||
|
|
||||||
glm::vec3 velocity = this->forward_velocity + this->horizontal_velocity + this->vertical_velocity;
|
|
||||||
|
|
||||||
glm::vec3 position_delta = (velocity * time);
|
|
||||||
|
|
||||||
// glm::vec3 new_position = this->position + position_delta;
|
|
||||||
|
|
||||||
// AABB Algorithm
|
|
||||||
glm::vec3 aabb_dims = glm::vec3(0.8f, 1.8f, 0.8f);
|
|
||||||
glm::vec3 aabb_half_dims = aabb_dims / 2.0f;
|
|
||||||
|
|
||||||
glm::vec3 aabb_center_offset = glm::vec3(0.0f, aabb_dims.y / 2.0f, 0.0f);
|
|
||||||
|
|
||||||
bool y_axis_collided = false;
|
|
||||||
|
|
||||||
for (int axis = 0; axis < 3; axis++) {
|
|
||||||
glm::vec3 target_position = this->position;
|
|
||||||
target_position[axis] += position_delta[axis];
|
|
||||||
|
|
||||||
glm::vec3 aabb_center = target_position + aabb_center_offset;
|
|
||||||
glm::vec3 adjusted_center = aabb_center + glm::vec3(0.5f, 0.5f, 0.5f);
|
|
||||||
glm::vec3 min = glm::floor(adjusted_center - aabb_half_dims);
|
|
||||||
glm::vec3 max = glm::ceil(adjusted_center + aabb_half_dims);
|
|
||||||
|
|
||||||
bool collided = false;
|
|
||||||
for (int x = min.x; x < max.x && !collided; x++) {
|
|
||||||
for (int y = min.y; y < max.y && !collided; y++) {
|
|
||||||
for (int z = min.z; z < max.z && !collided; z++) {
|
|
||||||
glm::ivec3 calculated_position = glm::ivec3(x, y, z);
|
|
||||||
if (world->get_voxel(calculated_position) != VoxelKind::Air) {
|
|
||||||
collided = true;
|
|
||||||
// std::cout << "Collided!!" << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!collided) {
|
|
||||||
this->position[axis] = target_position[axis];
|
|
||||||
} else if (axis == 1) {
|
|
||||||
y_axis_collided = true;
|
|
||||||
grounded = true;
|
|
||||||
this->vertical_velocity.y = 0.0f;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!y_axis_collided) {
|
|
||||||
this->vertical_velocity.y -= GRAVITY * time;
|
|
||||||
grounded = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// this->position += movement;
|
|
||||||
|
|
||||||
// Don't want to clip into the ground right
|
|
||||||
// if (this->position.y <= 1 && in_air)
|
|
||||||
// {
|
|
||||||
// this->position.y = 0.0f;
|
|
||||||
// this->vertical_velocity.y = 0.0f;
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
glm::vec3 getEyePosition() const {
|
glm::vec3 getEyePosition() const {
|
||||||
return position + glm::vec3(0.0f, CAMERA_HEIGHT, 0.0f);
|
return player->getEyePosition();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
40
src/main.cpp
40
src/main.cpp
@ -5,6 +5,7 @@
|
|||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
#include "Shader.h"
|
#include "Shader.h"
|
||||||
|
#include "Player.h"
|
||||||
#include "Camera.h"
|
#include "Camera.h"
|
||||||
#include "Object3D.h"
|
#include "Object3D.h"
|
||||||
#include "World.h"
|
#include "World.h"
|
||||||
@ -37,7 +38,8 @@ const int VIEW_DISTANCE = 4; // In Chunks
|
|||||||
bool W_pressed, S_pressed, A_pressed, D_pressed = false;
|
bool W_pressed, S_pressed, A_pressed, D_pressed = false;
|
||||||
|
|
||||||
glm::mat4 projection = glm::mat4(1.0f);
|
glm::mat4 projection = glm::mat4(1.0f);
|
||||||
Camera camera = Camera(CAMERA_SPEED, glm::vec3(0.0f, 1.85f, 0.0f));
|
Player player = Player(CAMERA_SPEED, glm::vec3(0.0f, 1.85f, 0.0f));
|
||||||
|
Camera camera = Camera(&player);
|
||||||
|
|
||||||
// Targeted block for highlighting
|
// Targeted block for highlighting
|
||||||
std::optional<glm::ivec3> targeted_block = std::nullopt;
|
std::optional<glm::ivec3> targeted_block = std::nullopt;
|
||||||
@ -143,25 +145,25 @@ void processInput(GLFWwindow *window)
|
|||||||
// Forward / Backward
|
// Forward / Backward
|
||||||
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||||
{
|
{
|
||||||
camera.forward_velocity = camera.speed * glm::vec3(camera.target.x, 0.0f, camera.target.z);
|
player.forward_velocity = player.speed * glm::vec3(camera.target.x, 0.0f, camera.target.z);
|
||||||
W_pressed = true;
|
W_pressed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||||
{
|
{
|
||||||
camera.forward_velocity = -camera.speed * glm::vec3(camera.target.x, 0.0f, camera.target.z);
|
player.forward_velocity = -player.speed * glm::vec3(camera.target.x, 0.0f, camera.target.z);
|
||||||
S_pressed = true;
|
S_pressed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_RELEASE && S_pressed)
|
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_RELEASE && S_pressed)
|
||||||
{
|
{
|
||||||
camera.forward_velocity = glm::vec3(0.0f);
|
player.forward_velocity = glm::vec3(0.0f);
|
||||||
S_pressed = false;
|
S_pressed = false;
|
||||||
}
|
}
|
||||||
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_RELEASE && W_pressed)
|
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_RELEASE && W_pressed)
|
||||||
{
|
{
|
||||||
camera.forward_velocity = glm::vec3(0.0f);
|
player.forward_velocity = glm::vec3(0.0f);
|
||||||
W_pressed = false;
|
W_pressed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,44 +171,44 @@ void processInput(GLFWwindow *window)
|
|||||||
// Horizontal
|
// Horizontal
|
||||||
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||||
{
|
{
|
||||||
camera.horizontal_velocity = -glm::normalize(glm::cross(glm::vec3(camera.target.x, 0.0f, camera.target.z), camera.worldUp)) * camera.speed;
|
player.horizontal_velocity = -glm::normalize(glm::cross(glm::vec3(camera.target.x, 0.0f, camera.target.z), camera.worldUp)) * player.speed;
|
||||||
A_pressed = true;
|
A_pressed = true;
|
||||||
}
|
}
|
||||||
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||||
{
|
{
|
||||||
camera.horizontal_velocity = glm::normalize(glm::cross(glm::vec3(camera.target.x, 0.0f, camera.target.z), camera.worldUp)) * camera.speed;
|
player.horizontal_velocity = glm::normalize(glm::cross(glm::vec3(camera.target.x, 0.0f, camera.target.z), camera.worldUp)) * player.speed;
|
||||||
D_pressed = true;
|
D_pressed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_RELEASE && A_pressed)
|
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_RELEASE && A_pressed)
|
||||||
{
|
{
|
||||||
camera.horizontal_velocity = glm::vec3(0.0f);
|
player.horizontal_velocity = glm::vec3(0.0f);
|
||||||
A_pressed = false;
|
A_pressed = false;
|
||||||
}
|
}
|
||||||
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_RELEASE && D_pressed)
|
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_RELEASE && D_pressed)
|
||||||
{
|
{
|
||||||
camera.horizontal_velocity = glm::vec3(0.0f);
|
player.horizontal_velocity = glm::vec3(0.0f);
|
||||||
D_pressed = false;
|
D_pressed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Jump
|
// Jump
|
||||||
if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
||||||
// camera.velocity -= glm::normalize(glm::cross(camera.target, glm::normalize(glm::cross(camera.target, camera.worldUp)))) * camera.speed * 0.5f;
|
// player.velocity -= glm::normalize(glm::cross(camera.target, glm::normalize(glm::cross(camera.target, camera.worldUp)))) * player.speed * 0.5f;
|
||||||
{
|
{
|
||||||
if (current_time - last_jump > JUMP_COOLDOWN && camera.grounded)
|
if (current_time - last_jump > JUMP_COOLDOWN && player.grounded)
|
||||||
{
|
{
|
||||||
camera.vertical_velocity.y = JUMP_POWER;
|
player.vertical_velocity.y = JUMP_POWER;
|
||||||
last_jump = current_time;
|
last_jump = current_time;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sprint
|
// Sprint
|
||||||
if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
|
if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
|
||||||
camera.speed = CAMERA_SPEED * 7.5;
|
player.speed = CAMERA_SPEED * 7.5;
|
||||||
if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_RELEASE)
|
if(glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_RELEASE)
|
||||||
camera.speed = CAMERA_SPEED * 20;
|
player.speed = CAMERA_SPEED * 20;
|
||||||
|
|
||||||
static bool left_mouse_pressed = false;
|
static bool left_mouse_pressed = false;
|
||||||
static bool right_mouse_pressed = false;
|
static bool right_mouse_pressed = false;
|
||||||
@ -421,11 +423,11 @@ int main() {
|
|||||||
|
|
||||||
shader.use();
|
shader.use();
|
||||||
|
|
||||||
// Generate chunks around the camera
|
// Generate chunks around the player
|
||||||
glm::ivec3 camera_chunk = glm::ivec3(
|
glm::ivec3 camera_chunk = glm::ivec3(
|
||||||
std::floor(camera.position.x / CHUNK_SIZE),
|
std::floor(player.position.x / CHUNK_SIZE),
|
||||||
std::floor(camera.position.y / CHUNK_SIZE),
|
std::floor(player.position.y / CHUNK_SIZE),
|
||||||
std::floor(camera.position.z / CHUNK_SIZE)
|
std::floor(player.position.z / CHUNK_SIZE)
|
||||||
);
|
);
|
||||||
|
|
||||||
// ---- PASS 1: Draw solid grey cubes ----
|
// ---- PASS 1: Draw solid grey cubes ----
|
||||||
@ -517,7 +519,7 @@ int main() {
|
|||||||
float time_since_last_move = end_time - move_time;
|
float time_since_last_move = end_time - move_time;
|
||||||
if (time_since_last_move >= 0.017)
|
if (time_since_last_move >= 0.017)
|
||||||
{
|
{
|
||||||
camera.move(&world, time_since_last_move);
|
player.move(&world, time_since_last_move);
|
||||||
move_time = end_time;
|
move_time = end_time;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user