Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
move movement logic to tick and interpolate between ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-makes-code committed Nov 18, 2023
1 parent 88b432c commit 3b54b82
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 44 deletions.
3 changes: 2 additions & 1 deletion Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

using Voxel.Client;
using Voxel.Client.Rendering.World;
using Voxel.Common.Util;
using Voxel.Common.World.Generation;

GenerationUtils.LoadNativeLibraries();
ChunkMeshBuilder.Init(4);

using var game = new VoxelClient();

game.Run(20, "Voxel Game");
game.Run((int)Constants.TicksPerSecond, "Voxel Game");
4 changes: 4 additions & 0 deletions Client/Rendering/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
namespace Voxel.Client.Rendering;

public class Camera {
public dvec3 oldPosition = new(0, 10, 0);
public dvec3 position = new(0, 10, 0);
public vec2 oldRotationVec = vec2.Zero;
public vec2 rotationVec = vec2.Zero;

public quat rotationY => quat.Identity.Rotated(rotationVec.y, new(0, 1, 0));
public quat oldRotationY => quat.Identity.Rotated(oldRotationVec.y, new(0, 1, 0));
public quat rotation => rotationY.Rotated(rotationVec.x, new(1,0,0));
public quat oldRotation => oldRotationY.Rotated(oldRotationVec.x, new(1,0,0));

/// <summary>
/// FOV in degrees from the top to the bottom of the camera.
Expand Down
9 changes: 6 additions & 3 deletions Client/Rendering/CameraStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using RenderSurface.Rendering;
using Veldrid;
using Voxel.Client.Rendering.Utils;
using Voxel.Common.Util;

namespace Voxel.Client.Rendering;

Expand Down Expand Up @@ -36,11 +37,13 @@ public CameraStateManager(RenderSystem system) {
));
}

public void SetToCamera(Camera c) {
currentCameraPosition = c.position;
public void SetToCamera(Camera c, double timeSinceLastTick) {
double deltaTicks = timeSinceLastTick * Constants.TicksPerSecond;
currentCameraPosition = dvec3.Lerp(c.oldPosition, c.position, deltaTicks);

var data = new CameraData();
data.VPMatrix = c.rotation.ToMat4 * mat4.Perspective(-c.fovy, c.aspect, c.nearClip, c.farClip).Transposed;
var cRotation = quat.Lerp(c.oldRotation, c.rotation, (float)deltaTicks);
data.VPMatrix = cRotation.ToMat4 * mat4.Perspective(-c.fovy, c.aspect, c.nearClip, c.farClip).Transposed;
CameraBuffer.value = data;
}

Expand Down
42 changes: 2 additions & 40 deletions Client/Rendering/GameRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Voxel.Client.Keybinding;
using Voxel.Client.Rendering.World;
using Voxel.Common.Collision;
using Voxel.Common.World;

namespace Voxel.Client.Rendering;

Expand All @@ -27,46 +28,7 @@ public GameRenderer(VoxelClient client) : base(client) {
}

public override void Render(double delta) {

dvec3 inputDir = dvec3.Zero;

if (Keybinds.StrafeLeft.isPressed)
inputDir.x -= 1;
if (Keybinds.StrafeRight.isPressed)
inputDir.x += 1;
if (Keybinds.Forward.isPressed)
inputDir.z -= 1;
if (Keybinds.Backward.isPressed)
inputDir.z += 1;
if (Keybinds.Crouch.isPressed)
inputDir.y -= 1;
if (Keybinds.Jump.isPressed)
inputDir.y += 1;

if (Keybinds.Refresh.isPressed)
WorldRenderer.ChunkRenderer.Reload();

inputDir = inputDir.NormalizedSafe;

if (Keybinds.LookLeft.isPressed)
MainCamera.rotationVec.y += (float)delta;
if (Keybinds.LookRight.isPressed)
MainCamera.rotationVec.y -= (float)delta;
if (Keybinds.LookUp.isPressed)
MainCamera.rotationVec.x += (float)delta;
if (Keybinds.LookDown.isPressed)
MainCamera.rotationVec.x -= (float)delta;
if (MainCamera.rotationVec.x < -MathF.PI/2)
MainCamera.rotationVec.x = -MathF.PI/2;
if (MainCamera.rotationVec.x > MathF.PI/2)
MainCamera.rotationVec.x = MathF.PI/2;

inputDir = MainCamera.rotationY * (vec3)inputDir;
inputDir /= 4;

MainCamera.MoveAndSlide(VoxelClient.Instance.world!, inputDir);

CameraStateManager.SetToCamera(MainCamera);
CameraStateManager.SetToCamera(MainCamera, Client.timeSinceLastTick);

WorldRenderer.Render(delta);
}
Expand Down
50 changes: 50 additions & 0 deletions Client/VoxelClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using GlmSharp;
using RenderSurface;
using Voxel.Client.Keybinding;
using Voxel.Client.Rendering;
using Voxel.Client.Rendering.World;
using Voxel.Client.World;
using Voxel.Common.World;

Expand All @@ -14,6 +16,8 @@ public class VoxelClient : Game {

public ClientWorld? world { get; private set; }

public double timeSinceLastTick = 0;

public VoxelClient() {

Check warning on line 21 in Client/VoxelClient.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'GameRenderer' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
Instance = this;
}
Expand All @@ -30,13 +34,59 @@ public override void Init() {
}

public override void OnFrame(double delta) {
timeSinceLastTick += delta;
GameRenderer.Render(delta);

ImGuiNET.ImGui.ShowMetricsWindow();
}

public override void OnTick() {
Keybinds.Poll();

timeSinceLastTick = 0;

dvec3 inputDir = dvec3.Zero;

if (Keybinds.StrafeLeft.isPressed)
inputDir.x -= 1;
if (Keybinds.StrafeRight.isPressed)
inputDir.x += 1;
if (Keybinds.Forward.isPressed)
inputDir.z -= 1;
if (Keybinds.Backward.isPressed)
inputDir.z += 1;
if (Keybinds.Crouch.isPressed)
inputDir.y -= 1;
if (Keybinds.Jump.isPressed)
inputDir.y += 1;

if (Keybinds.Refresh.isPressed)
GameRenderer.WorldRenderer.ChunkRenderer.Reload();

inputDir = inputDir.NormalizedSafe;

var camera = GameRenderer.MainCamera;

camera.oldPosition = camera.position;
camera.oldRotationVec = camera.rotationVec;

if (Keybinds.LookLeft.isPressed)
camera.rotationVec.y += 0.125f * (float)Keybinds.LookLeft.strength;
if (Keybinds.LookRight.isPressed)
camera.rotationVec.y -= 0.125f * (float)Keybinds.LookRight.strength;
if (Keybinds.LookUp.isPressed)
camera.rotationVec.x += 0.125f * (float)Keybinds.LookUp.strength;
if (Keybinds.LookDown.isPressed)
camera.rotationVec.x -= 0.25f * (float)Keybinds.LookDown.strength;
if (camera.rotationVec.x < -MathF.PI/2)
camera.rotationVec.x = -MathF.PI/2;
if (camera.rotationVec.x > MathF.PI/2)
camera.rotationVec.x = MathF.PI/2;

inputDir = camera.rotationY * (vec3)inputDir;
inputDir /= 4;

camera.MoveAndSlide(world!, inputDir);
}

public override void OnWindowResize() {
Expand Down
6 changes: 6 additions & 0 deletions Common/Util/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Voxel.Common.Util;

public static class Constants {
public const double TicksPerSecond = 20d;
public const double SecondsPerTick = 1d / TicksPerSecond;
}

0 comments on commit 3b54b82

Please sign in to comment.