From 9211e835db5b91ec534ab2c07f690bd2a6124990 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 5 Jun 2024 18:32:43 +0300 Subject: [PATCH] fix: input.add_callback and blocks placing when shift pressed --- src/logic/scripting/lua/libinput.cpp | 5 +++-- src/physics/PhysicsSolver.cpp | 12 ++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/logic/scripting/lua/libinput.cpp b/src/logic/scripting/lua/libinput.cpp index d870509c..309cc26b 100644 --- a/src/logic/scripting/lua/libinput.cpp +++ b/src/logic/scripting/lua/libinput.cpp @@ -31,16 +31,17 @@ static int l_mousecode(lua_State* L) { return 1; } -static int l_add_callback(lua_State* L) { +static int l_add_callback(lua_State*) { auto bindname = state->requireString(1); const auto& bind = Events::bindings.find(bindname); if (bind == Events::bindings.end()) { throw std::runtime_error("unknown binding "+util::quote(bindname)); } state->pushvalue(2); + runnable actual_callback = state->createRunnable(); runnable callback = [=]() { if (!scripting::engine->getGUI()->isFocusCaught()) { - state->createRunnable(); + actual_callback(); } }; if (hud) { diff --git a/src/physics/PhysicsSolver.cpp b/src/physics/PhysicsSolver.cpp index f3c5963a..4370be30 100644 --- a/src/physics/PhysicsSolver.cpp +++ b/src/physics/PhysicsSolver.cpp @@ -33,6 +33,7 @@ void PhysicsSolver::step( hitbox->grounded = false; for (uint i = 0; i < substeps; i++) { float px = pos.x; + float py = pos.y; float pz = pos.z; vel += gravity * dt * gravityScale; @@ -44,6 +45,9 @@ void PhysicsSolver::step( vel.z *= glm::max(0.0f, 1.0f - dt * linear_damping); pos += vel * dt + gravity * gravityScale * dt * dt * 0.5f; + if (hitbox->grounded) { + pos.y = py; + } if (shifting && hitbox->grounded){ float y = (pos.y-half.y-E); @@ -220,6 +224,7 @@ bool PhysicsSolver::isBlockInside(int x, int y, int z, Hitbox* hitbox) { } bool PhysicsSolver::isBlockInside(int x, int y, int z, Block* def, blockstate state, Hitbox* hitbox) { + const float E = 0.001f; // inaccuracy const glm::vec3& pos = hitbox->position; const glm::vec3& half = hitbox->halfsize; const auto& boxes = def->rotatable @@ -228,10 +233,9 @@ bool PhysicsSolver::isBlockInside(int x, int y, int z, Block* def, blockstate st for (const auto& block_hitbox : boxes) { glm::vec3 min = block_hitbox.min(); glm::vec3 max = block_hitbox.max(); - // 0.00001 - inaccuracy - if (min.x < pos.x+half.x-x-0.00001f && max.x > pos.x-half.x-x+0.00001f && - min.z < pos.z+half.z-z-0.00001f && max.z > pos.z-half.z-z+0.00001f && - min.y < pos.y+half.y-y-0.00001f && max.y > pos.y-half.y-y+0.00001f) + if (min.x < pos.x+half.x-x-E && max.x > pos.x-half.x-x+E && + min.z < pos.z+half.z-z-E && max.z > pos.z-half.z-z+E && + min.y < pos.y+half.y-y-E && max.y > pos.y-half.y-y+E) return true; } return false;