Working on better y axis collosion
This commit is contained in:
parent
0f31fe8656
commit
111c8f1f6e
57
src/Camera.h
57
src/Camera.h
@ -6,7 +6,7 @@
|
||||
|
||||
#include "World.h"
|
||||
|
||||
const float CAMERA_HEIGHT = 1.8f;
|
||||
const float CAMERA_HEIGHT = 0.0f;
|
||||
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
|
||||
@ -76,61 +76,68 @@ class Camera
|
||||
|
||||
void move(World* world, float time) // time passed in seconds since last move
|
||||
{
|
||||
bool in_air = this->position.y > 1;
|
||||
// bool in_air = this->position.y > 1;
|
||||
|
||||
if (this->position.y > 1)
|
||||
this->vertical_velocity.y -= (GRAVITY * time); // no way! physics reference?
|
||||
// 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;
|
||||
// glm::vec3 new_position = this->position + position_delta;
|
||||
|
||||
// AABB Algorithm
|
||||
glm::vec3 aabb_dims = glm::vec3(0.8f, 1.8f, 0.2f);
|
||||
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 min = glm::floor(target_position - aabb_half_dims);
|
||||
glm::vec3 max = glm::ceil(target_position + aabb_half_dims);
|
||||
glm::vec3 aabb_center = target_position + aabb_center_offset;
|
||||
glm::vec3 min = glm::floor(aabb_center - aabb_half_dims);
|
||||
glm::vec3 max = glm::ceil(aabb_center + aabb_half_dims);
|
||||
|
||||
bool collided = false;
|
||||
for (int x = min.x; x < max.x; x++) {
|
||||
for (int y = min.y; y < max.y; y++) {
|
||||
for (int z = min.z; z < max.z; z++) {
|
||||
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).has_value()) {
|
||||
collided = true;
|
||||
std::cout << "Collided!!" << std::endl;
|
||||
break;
|
||||
// std::cout << "Collided!!" << std::endl;
|
||||
}
|
||||
}
|
||||
if (collided) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (collided) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!collided) {
|
||||
this->position[axis] = new_position[axis];
|
||||
this->position[axis] = target_position[axis];
|
||||
} else if (axis == 1) {
|
||||
y_axis_collided = true;
|
||||
this->vertical_velocity.y = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
if (!y_axis_collided) {
|
||||
this->vertical_velocity.y -= GRAVITY * time;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 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;
|
||||
}
|
||||
// if (this->position.y <= 1 && in_air)
|
||||
// {
|
||||
// this->position.y = 0.0f;
|
||||
// this->vertical_velocity.y = 0.0f;
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -35,7 +35,7 @@ const int VIEW_DISTANCE = 4; // In Chunks
|
||||
bool W_pressed, S_pressed, A_pressed, D_pressed = false;
|
||||
|
||||
glm::mat4 projection = glm::mat4(1.0f);
|
||||
Camera camera = Camera(CAMERA_SPEED, glm::vec3(0.0f, 0.0f, 0.0f));
|
||||
Camera camera = Camera(CAMERA_SPEED, glm::vec3(0.0f, 1.85f, 0.0f));
|
||||
|
||||
// Targeted block for highlighting
|
||||
std::optional<glm::ivec3> targeted_block = std::nullopt;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user