From 3bb56a50d1979a181114c1811f7318ee462cd725 Mon Sep 17 00:00:00 2001 From: Harrison Date: Tue, 7 Jul 2020 14:16:31 -0500 Subject: [PATCH] Changed how interpolation between updates happen. --- src/SlatedGameToolkit.Framework/GameEngine.cs | 2 +- .../Graphics/Camera.cs | 7 +++++- .../Graphics/Render/IMoveable.cs | 22 ------------------- .../Graphics/Render/IPositionInterpolable.cs | 13 +++++++++++ .../Graphics/Render/MeshBatchRenderer.cs | 14 +++--------- .../StateSystem/States/IState.cs | 6 ++--- 6 files changed, 26 insertions(+), 38 deletions(-) delete mode 100644 src/SlatedGameToolkit.Framework/Graphics/Render/IMoveable.cs create mode 100644 src/SlatedGameToolkit.Framework/Graphics/Render/IPositionInterpolable.cs diff --git a/src/SlatedGameToolkit.Framework/GameEngine.cs b/src/SlatedGameToolkit.Framework/GameEngine.cs index fc938c3..0004270 100644 --- a/src/SlatedGameToolkit.Framework/GameEngine.cs +++ b/src/SlatedGameToolkit.Framework/GameEngine.cs @@ -174,7 +174,7 @@ namespace SlatedGameToolkit.Framework { timePassedFromLastRender += difference; if (timePassedFromLastRender > frameDeltaTime) { //Draw calls. - manager.Render(updateDeltaTime.TotalSeconds <= 0 ? updateDeltaTime.TotalSeconds : (timePassedFromLastUpdate / updateDeltaTime)); + manager.Render(updateDeltaTime.TotalSeconds <= 0 ? 1 : Math.Max(timePassedFromLastUpdate / updateDeltaTime, 0)); timePassedFromLastRender = TimeSpan.Zero; } else { Thread.Yield(); diff --git a/src/SlatedGameToolkit.Framework/Graphics/Camera.cs b/src/SlatedGameToolkit.Framework/Graphics/Camera.cs index a08da2a..01687f3 100644 --- a/src/SlatedGameToolkit.Framework/Graphics/Camera.cs +++ b/src/SlatedGameToolkit.Framework/Graphics/Camera.cs @@ -6,7 +6,7 @@ using SlatedGameToolkit.Framework.Utilities; namespace SlatedGameToolkit.Framework.Graphics { - public class Camera : IMoveable { + public class Camera : IPositionInterpolable { private bool viewUpdated = true, projectionUpdated = true, orthographic = false; private Vector3 position, lookAt; @@ -179,5 +179,10 @@ namespace SlatedGameToolkit.Framework.Graphics this.CameraFront = -Vector3.UnitZ; Logger.Log(string.Format("Camera initial dimensions: {0}x{1}, ratio of view: {2}", width, height, width / height), LogLevel.DEBUG); } + + public void InterpolatePosition(float delta) + { + this.Position += delta * (MoveTo - Position); + } } } \ No newline at end of file diff --git a/src/SlatedGameToolkit.Framework/Graphics/Render/IMoveable.cs b/src/SlatedGameToolkit.Framework/Graphics/Render/IMoveable.cs deleted file mode 100644 index 6e926f8..0000000 --- a/src/SlatedGameToolkit.Framework/Graphics/Render/IMoveable.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Numerics; - -namespace SlatedGameToolkit.Framework.Graphics.Render -{ - public interface IMoveable - { - /// - /// A vector describing the position this object should move to. - /// This is then used to smooth the movement to the intermediate renders. - /// - /// - /// A vector describing the new position of the object by the end of the update. - Vector3 MoveTo { get; set; } - - - /// - /// The current position of the entity. - /// - /// A vector describing the current objects position. - Vector3 Position { get; set; } - } -} \ No newline at end of file diff --git a/src/SlatedGameToolkit.Framework/Graphics/Render/IPositionInterpolable.cs b/src/SlatedGameToolkit.Framework/Graphics/Render/IPositionInterpolable.cs new file mode 100644 index 0000000..1a249d2 --- /dev/null +++ b/src/SlatedGameToolkit.Framework/Graphics/Render/IPositionInterpolable.cs @@ -0,0 +1,13 @@ +using System.Numerics; + +namespace SlatedGameToolkit.Framework.Graphics.Render +{ + public interface IPositionInterpolable + { + /// + /// Is called between each update to allow rendered meshes to be interpolated between positions and time steps. + /// + /// A normalized value between [0, 1] representing the current position between the updates. + void InterpolatePosition(float delta); + } +} \ No newline at end of file diff --git a/src/SlatedGameToolkit.Framework/Graphics/Render/MeshBatchRenderer.cs b/src/SlatedGameToolkit.Framework/Graphics/Render/MeshBatchRenderer.cs index b1e3ecc..355ae3b 100644 --- a/src/SlatedGameToolkit.Framework/Graphics/Render/MeshBatchRenderer.cs +++ b/src/SlatedGameToolkit.Framework/Graphics/Render/MeshBatchRenderer.cs @@ -136,13 +136,9 @@ namespace SlatedGameToolkit.Framework.Graphics.Render /// /// Draws the given mesh. public virtual void Draw(IMesh mesh) { - IMoveable moveable = mesh as IMoveable; + IPositionInterpolable moveable = mesh as IPositionInterpolable; if (moveable != null) { - if (GameEngine.UpdatesPerSecond <= 0) { - moveable.Position = moveable.MoveTo; - } else { - moveable.Position = moveable.Position + (moveable.MoveTo - moveable.Position) * batchDelta; - } + moveable.InterpolatePosition(batchDelta); } if (mesh.Texture?.Handle != this.texture?.Handle) { Flush(); @@ -202,11 +198,7 @@ namespace SlatedGameToolkit.Framework.Graphics.Render GLContext.BindTexture(TextureTarget.Texture2D, texture.Handle); } renderProgram.Use(); - if (GameEngine.UpdatesPerSecond <= 0) { - camera.Position = camera.MoveTo; - } else { - camera.Position = camera.Position + (camera.MoveTo - camera.Position) * batchDelta; - } + camera.InterpolatePosition(batchDelta); vertexBuffers.BufferVertices(data, true); vertexBuffers.BufferIndices(indices, true); diff --git a/src/SlatedGameToolkit.Framework/StateSystem/States/IState.cs b/src/SlatedGameToolkit.Framework/StateSystem/States/IState.cs index 90d24c7..9aae8a0 100644 --- a/src/SlatedGameToolkit.Framework/StateSystem/States/IState.cs +++ b/src/SlatedGameToolkit.Framework/StateSystem/States/IState.cs @@ -27,16 +27,16 @@ namespace SlatedGameToolkit.Framework.StateSystem.States /// /// Called in intervals dependent on game loop chosen for every update cycle. - /// Only called on if this state is the active state, indicated on the last status method called on this implementation (acitvate, and deactivate). + /// Only called on if this state is the active state, indicated on the last status method called on this implementation (activate, and deactivate). /// /// The change in time between the updates in seconds. If fixed, this amounts to the portion of time between the set update rate. If unlocked, represents the actual amount of time passed since the last update. void Update(double timeStep); /// /// Called in intervals dependent on game loop chose for every render cycle. - /// Only called on if this state is the active state, indicated on the last status method called on this implementation (acitvate, and deactivate). + /// Only called on if this state is the active state, indicated on the last status method called on this implementation (activate, and deactivate). /// - /// If in time-step mode, normalized value representing the time until the next update. Otherwise, the time passed since the last update. + /// Defined as a normalized value between [0, 1] representing the time at which this render was performed between updates. Will always be 1 if update cycle is unbounded. void Render(double delta); ///