update entities physics & fix jumping mobs

This commit is contained in:
MihailRis 2025-09-07 15:07:39 +03:00
parent 93e7ae159a
commit 67a5741ed2
3 changed files with 21 additions and 12 deletions

View File

@ -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

View File

@ -50,8 +50,8 @@ end
function on_update()
if not started then
if body:is_grounded() then
frameid = frameid + 1
if body:is_grounded() then
if target and (frameid % refresh_internal == 1 or not route) then
pathfinding.make_route_async(agent, tsf:get_pos(), target)
started = true

View File

@ -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;