diff options
| author | Mel <einebeere@gmail.com> | 2024-02-05 13:48:13 +0100 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2024-02-05 13:48:13 +0100 |
| commit | 091bf9e418ffdcf36e1735ed78d544f3d1b86785 (patch) | |
| tree | 58b1cd88d9822b14c90da757cc26acb64bf9bad5 /src/Entities | |
| parent | bcb89e0554e43d37daf74b3ef6e466b6630752cd (diff) | |
| download | meowcraft-091bf9e418ffdcf36e1735ed78d544f3d1b86785.tar.zst meowcraft-091bf9e418ffdcf36e1735ed78d544f3d1b86785.zip | |
More precise collision detection (and less NaNs)
Diffstat (limited to 'src/Entities')
| -rw-r--r-- | src/Entities/Player.cpp | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/src/Entities/Player.cpp b/src/Entities/Player.cpp index 01e3588..ae82e83 100644 --- a/src/Entities/Player.cpp +++ b/src/Entities/Player.cpp @@ -1,5 +1,4 @@ #include "Player.hpp" - #include "../Common/Casts.hpp" #include <unordered_set> @@ -17,8 +16,8 @@ void Player::update(const Time& time, GFX::Window& window, GFX::Camera& camera, m_on_ground = blocked_axes.y().negative; for (UInt axis = 0; axis < 3; axis++) { - if (blocked_axes[axis].positive) m_velocity[axis] = std::max(m_velocity[axis], 0.0); - if (blocked_axes[axis].negative) m_velocity[axis] = std::min(m_velocity[axis], 0.0); + if (blocked_axes[axis].positive) m_velocity[axis] = std::min(m_velocity[axis], 0.0); + if (blocked_axes[axis].negative) m_velocity[axis] = std::max(m_velocity[axis], 0.0); } } @@ -78,11 +77,9 @@ Vec3 Player::walking_velocity(GFX::Window& window, const Time& time, Vec3 input_ auto const walking_velocity = walking_direction * base_move_speed * time.delta(); auto vertical_velocity = 0.0; - if (m_on_ground) { - if (window.key(GLFW_KEY_SPACE, GLFW_PRESS)) { - vertical_velocity = initial_jump_velocity; - m_on_ground = false; - } + if (m_on_ground && input_direction.y() > 0.5) { + vertical_velocity = initial_jump_velocity; + m_on_ground = false; } else { // TODO: This integration depends on frame delta. vertical_velocity = m_velocity.y() - gravity * time.delta(); @@ -165,13 +162,13 @@ void Player::actions(GFX::Window& window, World::World& world) { Player::ProcessCollisionsResult Player::process_collisions(World::World& world, Position::World from, Position::World to) { if (from.mostly_equal(to)) return {to, {}}; - auto current_box = bounding_box_for_position(from); + auto const current_box = bounding_box_for_position(from); // All the blocks we could theoretically collide with. // NOTE: It isn't updated as new responses are applied, // as that would be (currently) too expensive. // At very high speeds, this may lead to phasing through blocks. - auto collision_domain = terrain_collision_domain(from, to, world); + auto const collision_domain = terrain_collision_domain(from, to, world); // Sort the responses first by the magnitude of the velocity until the collision, // and then by the distance from the entity, so that we slide along the closest block. @@ -189,12 +186,12 @@ Player::ProcessCollisionsResult Player::process_collisions(World::World& world, }; std::vector<Response> responses; for (UInt stuck = 0; stuck < STUCK_THRESHOLD; stuck++) { - auto v = to - from; + auto const v = to - from; for (auto possible_collision : collision_domain) { - auto response = current_box.collision_response(v, possible_collision); - auto total_velocity = response.v_to_collision + response.v_slide; - if (!total_velocity.mostly_equal(v)) { + auto const response = current_box.collision_response(v, possible_collision); + auto const total_velocity = response.v_to_collision + response.v_slide; + if (!total_velocity.mostly_equal(v) && !total_velocity.is_nan()) { responses.push_back({ response, possible_collision.center().distance_squared(from), @@ -212,10 +209,9 @@ Player::ProcessCollisionsResult Player::process_collisions(World::World& world, // TODO: This applies the entire response, even though ideally we'd apply it in two parts, // since technically the total velocity is a diagonal of the two components. // This should only be a marginal issue, though. - auto response = responses[0].response; + auto const response = responses[0].response; to = from + response.v_to_collision + response.v_slide; - auto check_axis = [&](Real axis_normal, BlockedAxis& axis) -> Bool { if (axis_normal < -0.5) { axis.negative = true; return true; } if (axis_normal > 0.5) { axis.positive = true; return true; } |
