Skip to content

Commit

Permalink
View bobbing cannot escape eyeBox anymore
Browse files Browse the repository at this point in the history
View bobbing will scale down as it gets closer to eyeBox edges,
and will hard clamp if needed.
  • Loading branch information
AlexDavies8 committed Sep 26, 2024
1 parent ffd36e7 commit cc29de7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/camera.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ pub const ViewBobbing = struct { // MARK: ViewBobber
const xBob = @sin(time);
const a = 0.5 * -@sin(2 * time);
const zBob = ((a - a * a * a / 3) * 2 + 0.25 - 0.25 * @cos(2 * time)) * 0.8;
const bobVec = vec.rotateZ(Vec3d{xBob * magnitude * 0.05, 0, zBob * magnitude * 0.05}, -rotation[2]);
var bobVec = vec.rotateZ(Vec3d{xBob * magnitude * 1.05, 0, zBob * magnitude * 1.05}, -rotation[2]);
const eyeMin = game.Player.eyePos - game.Player.desiredEyePos + game.Player.eyeBox.min;
const eyeMax = game.Player.eyePos - game.Player.desiredEyePos + game.Player.eyeBox.max;
const eyeBoxSize = game.Player.eyeBox.max - game.Player.eyeBox.min;
const scaling = @as(Vec3d, @splat(1)) - @as(Vec3d, @splat(2)) * @abs(game.Player.eyePos) / eyeBoxSize;
bobVec = @max(eyeMin, @min(bobVec * scaling, eyeMax));
return bobVec;
}

Expand Down
5 changes: 2 additions & 3 deletions src/game.zig
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ pub const Player = struct { // MARK: Player
.min = -outerBoundingBoxExtent,
.max = outerBoundingBoxExtent,
};
const eyeBox: collision.Box = .{
pub const eyeBox: collision.Box = .{
.min = -Vec3d{outerBoundingBoxExtent[0]*0.2, outerBoundingBoxExtent[1]*0.2, 0.6},
.max = Vec3d{outerBoundingBoxExtent[0]*0.2, outerBoundingBoxExtent[1]*0.2, 0.9 - 0.05},
};
Expand Down Expand Up @@ -722,8 +722,6 @@ pub fn update(deltaTime: f64) void { // MARK: update()
main.Window.scrollOffset = 0;
}

camera.ViewBobbing.update(deltaTime);

// This our model for movement on a single frame:
// dv/dt = a - λ·v
// dx/dt = v
Expand Down Expand Up @@ -925,6 +923,7 @@ pub fn update(deltaTime: f64) void { // MARK: update()
Player.eyePos = @max(Player.eyeBox.min, @min(Player.eyePos, Player.eyeBox.max));
Player.eyeCoyote -= deltaTime;

camera.ViewBobbing.update(deltaTime);
camera.position = Player.getEyePosBlocking();

const biome = world.?.playerBiome.load(.monotonic);
Expand Down

0 comments on commit cc29de7

Please sign in to comment.