diff --git a/src/game.zig b/src/game.zig index d017df3f2a..0c5b976925 100644 --- a/src/game.zig +++ b/src/game.zig @@ -663,7 +663,18 @@ pub fn update(deltaTime: f64) void { // MARK: update() stepAmount = physics.calculateWallCollision(.client, &motion, &Player.super.pos, &Player.super.vel, &Player.onGround, Player.friction, Player.outerBoundingBox, Player.steppingHeight()[2], steppingHeightLimit, Player.crouching); } physics.calculateEyeMovement(.client, deltaTime, Player.super.pos, Player.super.vel, &Player.eye, stepAmount); - physics.update(.client, deltaTime, motion); + var didCollide: bool = false; + const wasOnGround = Player.onGround; + const prevPos = Player.super.pos; + const prevVel = Player.super.vel; + if (!Player.isGhost.load(.monotonic)) { + const bouncinessMultiplier: f64 = if (Player.isFlying.load(.monotonic)) 0.0 else if (Player.crouching) 0.5 else 1.0; + didCollide = physics.calculateVerticalCollision(.client, deltaTime, &Player.super.pos, &Player.super.vel, &Player.jumpCoyote, &Player.onGround, Player.outerBoundingBox, motion, bouncinessMultiplier); + if (didCollide) { + physics.calculateVerticalCollisionEyeMovement(&Player.eye, Player.onGround, wasOnGround, prevPos, Player.super.pos, prevVel, Player.super.vel, motion); + } + } + physics.update(.client, deltaTime, prevVel, Player.super.vel, didCollide, motion, wasOnGround); } const time = main.timestamp(); diff --git a/src/physics.zig b/src/physics.zig index 47b55c3b23..07a39244d6 100644 --- a/src/physics.zig +++ b/src/physics.zig @@ -553,49 +553,59 @@ pub fn calculateWallCollision(comptime side: main.sync.Side, motion: *Vec3d, pos return stepAmount; } -pub fn update(comptime side: main.sync.Side, deltaTime: f64, motion: Vec3d) void { - if (!Player.isGhost.load(.monotonic)) { - const hitBox = Player.outerBoundingBox; - - const wasOnGround = Player.onGround; - Player.onGround = false; - Player.super.pos[2] += motion[2]; - if (collision.collides(side, .z, -motion[2], Player.super.pos, hitBox)) |box| { - if (motion[2] < 0) { - if (!wasOnGround) { - Player.eye.vel[2] = Player.super.vel[2]; - Player.eye.pos[2] -= (box.max[2] - hitBox.min[2] - Player.super.pos[2]); - } - Player.onGround = true; - Player.super.pos[2] = box.max[2] - hitBox.min[2]; - Player.eye.coyote = 0; - } else { - Player.super.pos[2] = box.min[2] - hitBox.max[2]; - } - var bounciness = if (Player.isFlying.load(.monotonic)) 0 else collision.calculateSurfaceProperties(side, Player.super.pos, Player.outerBoundingBox, 0.0).bounciness; - if (Player.crouching) { - bounciness *= 0.5; - } - var velocityChange: f64 = undefined; +pub fn calculateVerticalCollision(comptime side: main.sync.Side, deltaTime: f64, pos: *Vec3d, vel: *Vec3d, jumpCoyote: ?*f64, onGround: *bool, hitBox: collision.Box, motion: Vec3d, bouncinessMultiplier: f64) bool { + onGround.* = false; + pos[2] += motion[2]; + + if (collision.collides(side, .z, -motion[2], pos.*, hitBox)) |box| { + if (motion[2] < 0) { + onGround.* = true; + pos[2] = box.max[2] - hitBox.min[2]; + } else { + pos[2] = box.min[2] - hitBox.max[2]; + } + const bounciness = if (bouncinessMultiplier == 0) 0 else collision.calculateSurfaceProperties(side, pos.*, hitBox, 0.0).bounciness*bouncinessMultiplier; - if (bounciness != 0.0 and Player.super.vel[2] < -3.0) { - velocityChange = Player.super.vel[2]*@as(f64, @floatCast(1 - bounciness)); - Player.super.vel[2] = -Player.super.vel[2]*bounciness; - Player.jumpCoyote = Player.jumpCoyoteTimeConstant + deltaTime; - Player.eye.vel[2] *= 2; - } else { - velocityChange = Player.super.vel[2]; - Player.super.vel[2] = 0; + if (bounciness != 0.0 and vel[2] < -3.0) { + vel[2] = -vel[2]*bounciness; + if (jumpCoyote) |coyote| { + coyote.* = Player.jumpCoyoteTimeConstant + deltaTime; } + } else { + vel[2] = 0; + } + + // Always unstuck upwards for now + while (collision.collides(side, .z, 0, pos.*, hitBox)) |_| { + pos[2] += 1; + } + return true; + } else { + return false; + } +} + +pub fn calculateVerticalCollisionEyeMovement(eye: *Player.EyeData, onGround: bool, wasOnGround: bool, prevPos: Vec3d, pos: Vec3d, prevVel: Vec3d, vel: Vec3d, motion: Vec3d) void { + if (onGround) { + if (!wasOnGround) { + eye.vel[2] = prevVel[2]; + eye.pos[2] -= pos[2] - prevPos[2] - motion[2]; + } + eye.coyote = 0.0; + } + if (vel[2] != 0.0) { + eye.vel[2] *= 2.0; + } +} + +pub fn update(comptime side: main.sync.Side, deltaTime: f64, prevVel: Vec3d, vel: Vec3d, didCollide: bool, motion: Vec3d, wasOnGround: bool) void { + if (!Player.isGhost.load(.monotonic)) { + if (didCollide) { + const velocityChange = @abs(@abs(prevVel[2]) - @abs(vel[2])); const damage: f32 = @floatCast(@round(@max((velocityChange*velocityChange)/(2*baseGravity) - 7, 0))/2); if (damage > 0.01) { main.sync.addHealth(-damage, .fall, .client, Player.id); } - - // Always unstuck upwards for now - while (collision.collides(side, .z, 0, Player.super.pos, hitBox)) |_| { - Player.super.pos[2] += 1; - } } else if (wasOnGround and motion[2] < 0) { // If the player drops off a ledge, they might just be walking over a small gap, so lock the y position of the eyes that long. // This calculates how long the player has to fall until we know they're not walking over a small gap. @@ -606,7 +616,7 @@ pub fn update(comptime side: main.sync.Side, deltaTime: f64, motion: Vec3d) void } else if (Player.eye.coyote > 0) { Player.eye.pos[2] -= motion[2]; } - collision.touchBlocks(side, &Player.super, hitBox, deltaTime); + collision.touchBlocks(side, &Player.super, Player.outerBoundingBox, deltaTime); } else { Player.super.pos += motion; }