Skip to content

Commit

Permalink
Untie camera movement from framerate
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsPepperpot committed Sep 20, 2024
1 parent 43d3a5d commit 91b4ea6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
20 changes: 10 additions & 10 deletions IndustrialPark/MainForm/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -719,29 +719,29 @@ public void KeyboardController()
return;

if (PressedKeys.Contains(Keys.A) & PressedKeys.Contains(Keys.ControlKey))
renderer.Camera.AddYaw(-0.05f);
renderer.Camera.AddYaw(-0.05f * renderer.TransformScaleFactor);
else if (PressedKeys.Contains(Keys.A))
renderer.Camera.AddPositionSideways(0.25f);
renderer.Camera.AddPositionSideways(0.25f * renderer.TransformScaleFactor);

if (PressedKeys.Contains(Keys.D) & PressedKeys.Contains(Keys.ControlKey))
renderer.Camera.AddYaw(0.05f);
renderer.Camera.AddYaw(0.05f * renderer.TransformScaleFactor);
else if (PressedKeys.Contains(Keys.D))
renderer.Camera.AddPositionSideways(-0.25f);
renderer.Camera.AddPositionSideways(-0.25f * renderer.TransformScaleFactor);

if (PressedKeys.Contains(Keys.W) & PressedKeys.Contains(Keys.ControlKey))
renderer.Camera.AddPitch(-0.05f);
renderer.Camera.AddPitch(-0.05f * renderer.TransformScaleFactor);
else if (PressedKeys.Contains(Keys.W) & PressedKeys.Contains(Keys.ShiftKey))
renderer.Camera.AddPositionUp(0.25f);
renderer.Camera.AddPositionUp(0.25f * renderer.TransformScaleFactor);
else if (PressedKeys.Contains(Keys.W))
renderer.Camera.AddPositionForward(0.25f);
renderer.Camera.AddPositionForward(0.25f * renderer.TransformScaleFactor);

// Added extra conditions to stop camera moving when saving all archive editors
if (PressedKeys.Contains(Keys.S) & PressedKeys.Contains(Keys.ControlKey) & !PressedKeys.Contains(Keys.ShiftKey))
renderer.Camera.AddPitch(0.05f);
renderer.Camera.AddPitch(0.05f * renderer.TransformScaleFactor);
else if (PressedKeys.Contains(Keys.S) & PressedKeys.Contains(Keys.ShiftKey) & !PressedKeys.Contains(Keys.ControlKey))
renderer.Camera.AddPositionUp(-0.25f);
renderer.Camera.AddPositionUp(-0.25f * renderer.TransformScaleFactor);
else if (PressedKeys.Contains(Keys.S) & !PressedKeys.Contains(Keys.ShiftKey) & !PressedKeys.Contains(Keys.ControlKey))
renderer.Camera.AddPositionForward(-0.25f);
renderer.Camera.AddPositionForward(-0.25f * renderer.TransformScaleFactor);

if (PressedKeys.Contains(Keys.R))
renderer.Camera.Reset();
Expand Down
18 changes: 18 additions & 0 deletions IndustrialPark/SharpDX/SharpRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using SharpDX.Windows;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using static IndustrialPark.Models.BSP_IO_ReadOBJ;
Expand Down Expand Up @@ -487,14 +488,31 @@ public void StopFly()

public bool allowRender = true;

private Stopwatch stopwatch = new Stopwatch();
private const float TARGET_FRAME_TIME = 1.0f / 60.0f;

public float TransformScaleFactor { get; private set; } = 1.0f;

private void MainLoop(System.Drawing.Size controlSize)
{
if (!stopwatch.IsRunning)
{
stopwatch.Start(); // Start the stopwatch for the first frame
}

//Resizing
if (device.MustResize)
{
device.Resize();
Camera.AspectRatio = (float)controlSize.Width / controlSize.Height;
}

// Calculate the time elapsed since the last frame
float elapsedSeconds = Convert.ToSingle(stopwatch.Elapsed.TotalSeconds);
stopwatch.Restart(); // Restart the stopwatch for the next frame

// Calculate the scaling factor
TransformScaleFactor = elapsedSeconds / TARGET_FRAME_TIME;

Program.MainForm.KeyboardController();

Expand Down

0 comments on commit 91b4ea6

Please sign in to comment.