Changed how interpolation between updates happen.

This commit is contained in:
Harrison Deng 2020-07-07 14:16:31 -05:00
parent 512b5f4b13
commit 3bb56a50d1
6 changed files with 26 additions and 38 deletions

View File

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

View File

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

View File

@ -1,22 +0,0 @@
using System.Numerics;
namespace SlatedGameToolkit.Framework.Graphics.Render
{
public interface IMoveable
{
/// <summary>
/// A vector describing the position this object should move to.
/// This is then used to smooth the movement to the intermediate renders.
///
/// </summary>
/// <value>A vector describing the new position of the object by the end of the update.</value>
Vector3 MoveTo { get; set; }
/// <summary>
/// The current position of the entity.
/// </summary>
/// <value>A vector describing the current objects position.</value>
Vector3 Position { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System.Numerics;
namespace SlatedGameToolkit.Framework.Graphics.Render
{
public interface IPositionInterpolable
{
/// <summary>
/// Is called between each update to allow rendered meshes to be interpolated between positions and time steps.
/// </summary>
/// <param name="delta">A normalized value between [0, 1] representing the current position between the updates.</param>
void InterpolatePosition(float delta);
}
}

View File

@ -136,13 +136,9 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
/// </summary>
/// <param name="mesh">Draws the given mesh.</param>
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);

View File

@ -27,16 +27,16 @@ namespace SlatedGameToolkit.Framework.StateSystem.States
/// <summary>
/// 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).
/// </summary>
/// <param name="timeStep">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.</param>
void Update(double timeStep);
/// <summary>
/// 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).
/// </summary>
/// <param name="delta">If in time-step mode, normalized value representing the time until the next update. Otherwise, the time passed since the last update.</param>
/// <param name="delta">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.</param>
void Render(double delta);
/// <summary>