From 67a5741ed238e635c24259df8afdc22ea8849e05 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 7 Sep 2025 15:07:39 +0300 Subject: [PATCH] update entities physics & fix jumping mobs --- res/scripts/components/mob.lua | 20 ++++++++++++++------ res/scripts/components/pathfinding.lua | 2 +- src/physics/PhysicsSolver.cpp | 11 ++++++----- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/res/scripts/components/mob.lua b/res/scripts/components/mob.lua index 3cbf285d..8de81ba6 100644 --- a/res/scripts/components/mob.lua +++ b/res/scripts/components/mob.lua @@ -50,13 +50,24 @@ function lower(speed, delta, vel) vec3.add(vel, {0, -speed * delta * props.movement_speed, 0}, vel)) end -function move_horizontal(speed, dir, vel) +local function move_horizontal(speed, dir, vel) vel = vel or body:get_vel() if vec3.length(dir) > 0.0 then vec3.normalize(dir, dir) - vel[1] = dir[1] * speed - vel[3] = dir[3] * speed + local magnitude = vec3.length({vel[1], 0, vel[3]}) + + if magnitude <= 1e-4 or (magnitude < speed or vec3.dot( + {vel[1] / magnitude, 0.0, vel[3] / magnitude}, dir) < 0.9) + then + vel[1] = vel[1] + dir[1] * speed * 0.8 + vel[3] = vel[3] + dir[3] * speed * 0.8 + end + magnitude = vec3.length({vel[1], 0, vel[3]}) + if vec3.dot({vel[1] / magnitude, 0.0, vel[3] / magnitude}, dir) > 0.5 then + vel[1] = vel[1] / magnitude * speed + vel[3] = vel[3] / magnitude * speed + end end body:set_vel(vel) end @@ -77,7 +88,6 @@ end local prev_angle = 0.0 local headIndex = rig:index("head") - -- todo: move somewhere local watchtimer = math.random(0, 1000) local function update_head() @@ -103,8 +113,6 @@ end local function follow_waypoints(pathfinding, delta) local pos = tsf:get_pos() - pathfinding.set_target(vec3.add(pos, - {math.random(-15, 15), math.random(-2, 2), math.random(-15, 15)})) local waypoint = pathfinding.next_waypoint() if not waypoint then return diff --git a/res/scripts/components/pathfinding.lua b/res/scripts/components/pathfinding.lua index 797bde9d..2a6e51aa 100644 --- a/res/scripts/components/pathfinding.lua +++ b/res/scripts/components/pathfinding.lua @@ -50,8 +50,8 @@ end function on_update() if not started then + frameid = frameid + 1 if body:is_grounded() then - frameid = frameid + 1 if target and (frameid % refresh_internal == 1 or not route) then pathfinding.make_route_async(agent, tsf:get_pos(), target) started = true diff --git a/src/physics/PhysicsSolver.cpp b/src/physics/PhysicsSolver.cpp index 0d1cb4a0..71df61d2 100644 --- a/src/physics/PhysicsSolver.cpp +++ b/src/physics/PhysicsSolver.cpp @@ -45,11 +45,6 @@ void PhysicsSolver::step( colisionCalc(chunks, hitbox, vel, pos, half, (prevGrounded && gravityScale > 0.0f) ? 0.5f : 0.0f); } - vel.x /= 1.0f + dt * linearDamping; - vel.z /= 1.0f + dt * linearDamping; - if (hitbox.verticalDamping) { - vel.y /= 1.0f + dt * linearDamping; - } pos += vel * dt + gravity * gravityScale * dt * dt * 0.5f; if (hitbox.grounded && pos.y < py) { @@ -89,6 +84,12 @@ void PhysicsSolver::step( hitbox.grounded = true; } } + vel.x /= 1.0f + delta * linearDamping; + vel.z /= 1.0f + delta * linearDamping; + if (hitbox.verticalDamping) { + vel.y /= 1.0f + delta * linearDamping; + } + AABB aabb; aabb.a = hitbox.position - hitbox.halfsize; aabb.b = hitbox.position + hitbox.halfsize;