diff --git a/src/content/loading/EntityLoader.cpp b/src/content/loading/EntityLoader.cpp index 29a72cb73..12e5313a7 100644 --- a/src/content/loading/EntityLoader.cpp +++ b/src/content/loading/EntityLoader.cpp @@ -88,4 +88,5 @@ template<> void ContentUnitLoader::loadUnit( root.at("skeleton-name").get(def.skeletonName); root.at("blocking").get(def.blocking); + root.at("step-height").get(def.stepHeight); } diff --git a/src/objects/EntityDef.hpp b/src/objects/EntityDef.hpp index 4d10a2d02..3752f3063 100644 --- a/src/objects/EntityDef.hpp +++ b/src/objects/EntityDef.hpp @@ -42,6 +42,9 @@ struct EntityDef { /// @brief Does entity prevent blocks setup bool blocking = true; + /// @brief Max obstacle height that does not require jumping + float stepHeight = 0.5f; + /// @brief save-** flags struct { bool enabled = true; diff --git a/src/objects/Rigidbody.cpp b/src/objects/Rigidbody.cpp index 9b1f78d27..9e2edba4a 100644 --- a/src/objects/Rigidbody.cpp +++ b/src/objects/Rigidbody.cpp @@ -80,4 +80,5 @@ void Rigidbody::initialize( create_sensor_callback(entities), create_sensor_callback(entities)}; } + hitbox.stepHeight = def.stepHeight; } diff --git a/src/physics/Hitbox.hpp b/src/physics/Hitbox.hpp index 2b25bd6fe..909a48451 100644 --- a/src/physics/Hitbox.hpp +++ b/src/physics/Hitbox.hpp @@ -59,6 +59,7 @@ struct Hitbox { bool grounded = false; float gravityScale = 1.0f; bool crouching = false; + float stepHeight = 0.5f; Hitbox(BodyType type, glm::vec3 position, glm::vec3 halfsize); diff --git a/src/physics/PhysicsSolver.cpp b/src/physics/PhysicsSolver.cpp index 9244112e2..e20bafacd 100644 --- a/src/physics/PhysicsSolver.cpp +++ b/src/physics/PhysicsSolver.cpp @@ -42,7 +42,7 @@ void PhysicsSolver::step( vel += gravity * dt * gravityScale; if (hitbox.type == BodyType::DYNAMIC) { colisionCalc(chunks, hitbox, vel, pos, half, - (prevGrounded && gravityScale > 0.0f) ? 0.5f : 0.0f); + (prevGrounded && gravityScale > 0.0f) ? hitbox.stepHeight : 0.0f); } pos += vel * dt + gravity * gravityScale * dt * dt * 0.5f; @@ -65,6 +65,7 @@ void PhysicsSolver::step( } if (!hitbox.grounded) { pos.z = pz; + vel.z = 0.0f; } hitbox.grounded = false; for (int ix = 0; ix <= (half.x-E)*2/s; ix++) { @@ -79,6 +80,7 @@ void PhysicsSolver::step( } if (!hitbox.grounded) { pos.x = px; + vel.x = 0.0f; } hitbox.grounded = true; } @@ -152,17 +154,16 @@ static bool calc_collision_neg( return false; } glm::vec3 offset(0.0f, stepHeight, 0.0f); - for (int iy = 0; iy <= ((half-offset*0.5f)[ny]-E)*2/s; iy++) { + for (int iy = 0; iy <= std::max(1, ((half-offset*0.5f)[ny]-E)*2/s); iy++) { glm::vec3 coord; coord[ny] = ((pos+offset)[ny]-half[ny]+E) + iy * s; for (int iz = 0; iz <= (half[nz]-E)*2/s; iz++){ coord[nz] = (pos[nz]-half[nz]+E) + iz * s; coord[nx] = (pos[nx]-half[nx]-E); - if (const auto aabb = chunks.isObstacleAt(coord.x, coord.y, coord.z)) { vel[nx] = 0.0f; - float newx = std::floor(coord[nx]) + aabb->max()[nx] + half[nx] + E; - if (std::abs(newx-pos[nx]) <= MAX_FIX) { + float newx = std::floor(coord[nx]) + half[nx] + aabb->max()[nx] + E; + if (newx - pos[nx] <= E) { pos[nx] = newx; } return true; @@ -185,7 +186,7 @@ static void calc_collision_pos( return; } glm::vec3 offset(0.0f, stepHeight, 0.0f); - for (int iy = 0; iy <= ((half-offset*0.5f)[ny]-E)*2/s; iy++) { + for (int iy = 0; iy <= std::max(1, ((half-offset*0.5f)[ny]-E)*2/s); iy++) { glm::vec3 coord; coord[ny] = ((pos+offset)[ny]-half[ny]+E) + iy * s; for (int iz = 0; iz <= (half[nz]-E)*2/s; iz++) { @@ -194,7 +195,7 @@ static void calc_collision_pos( if (const auto aabb = chunks.isObstacleAt(coord.x, coord.y, coord.z)) { vel[nx] = 0.0f; float newx = std::floor(coord[nx]) - half[nx] + aabb->min()[nx] - E; - if (std::abs(newx-pos[nx]) <= MAX_FIX) { + if (newx - pos[nx] <= E) { pos[nx] = newx; } return; @@ -211,7 +212,7 @@ void PhysicsSolver::colisionCalc( const glm::vec3 half, float stepHeight ) { - // step size (smaller - more accurate, but slower) + // step size (smaller - more accurate, but slower) // TODO: GET RID OF THIS float s = 2.0f/BLOCK_AABB_GRID; stepHeight = calc_step_height(chunks, pos, half, stepHeight, s); @@ -224,7 +225,7 @@ void PhysicsSolver::colisionCalc( calc_collision_neg<2, 1, 0>(chunks, pos, vel, half, stepHeight, s); calc_collision_pos<2, 1, 0>(chunks, pos, vel, half, stepHeight, s); - if (calc_collision_neg<1, 0, 2>(chunks, pos, vel, half, stepHeight, s)) { + if (calc_collision_neg<1, 0, 2>(chunks, pos, vel, half, 0.0f, s)) { hitbox.grounded = true; }