Fixed incorrect FixedUpdate() execution and added Time.FixedDeltaTime

This commit is contained in:
Kah Wei 2022-11-01 00:37:09 +08:00
parent 4e97392098
commit c08afcb804
6 changed files with 42 additions and 6 deletions

View File

@ -327,6 +327,11 @@ namespace SHADE
void SHPhysicsSystem::PhysicsFixedUpdate::Execute(double dt) noexcept void SHPhysicsSystem::PhysicsFixedUpdate::Execute(double dt) noexcept
{ {
auto* physicsSystem = reinterpret_cast<SHPhysicsSystem*>(GetSystem()); auto* physicsSystem = reinterpret_cast<SHPhysicsSystem*>(GetSystem());
auto scriptSys = SHSystemManager::GetSystem<SHScriptEngine>();
if (!scriptSys)
{
SHLOG_WARNING("[SHPhysicsSystem] Unable to invoke FixedUpdate() on scripts due to missing SHScriptEngine!");
}
fixedTimeStep = 1.0 / physicsSystem->fixedDT; fixedTimeStep = 1.0 / physicsSystem->fixedDT;
accumulatedTime += dt; accumulatedTime += dt;
@ -334,6 +339,10 @@ namespace SHADE
int count = 0; int count = 0;
while (accumulatedTime > fixedTimeStep) while (accumulatedTime > fixedTimeStep)
{ {
if (scriptSys)
{
scriptSys->ExecuteFixedUpdates();
}
physicsSystem->world->update(static_cast<rp3d::decimal>(fixedTimeStep)); physicsSystem->world->update(static_cast<rp3d::decimal>(fixedTimeStep));
accumulatedTime -= fixedTimeStep; accumulatedTime -= fixedTimeStep;
@ -359,7 +368,6 @@ namespace SHADE
auto scriptSys = SHSystemManager::GetSystem<SHScriptEngine>(); auto scriptSys = SHSystemManager::GetSystem<SHScriptEngine>();
if (scriptSys) if (scriptSys)
{ {
scriptSys->ExecuteFixedUpdates();
scriptSys->ExecuteCollisionFunctions(); scriptSys->ExecuteCollisionFunctions();
} }
else else

View File

@ -50,4 +50,16 @@ namespace SHADE
SHLOG_WARNING("[SHPhysicsSystemInterface] Failed to get trigger events. Empty vector returned instead."); SHLOG_WARNING("[SHPhysicsSystemInterface] Failed to get trigger events. Empty vector returned instead.");
return emptyVec; return emptyVec;
} }
double SHPhysicsSystemInterface::GetFixedDT() noexcept
{
auto phySystem = SHSystemManager::GetSystem<SHPhysicsSystem>();
if (phySystem)
{
return phySystem->GetFixedDT();
}
SHLOG_WARNING("[SHPhysicsSystemInterface] Failed to get fixed delta time. 0.0 returned instead.");
return 0.0;
}
} }

View File

@ -41,5 +41,6 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] static const std::vector<SHCollisionEvent>& GetCollisionInfo() noexcept; [[nodiscard]] static const std::vector<SHCollisionEvent>& GetCollisionInfo() noexcept;
[[nodiscard]] static const std::vector<SHCollisionEvent>& GetTriggerInfo() noexcept; [[nodiscard]] static const std::vector<SHCollisionEvent>& GetTriggerInfo() noexcept;
[[nodiscard]] static double GetFixedDT() noexcept;
}; };
} }

View File

@ -14,6 +14,9 @@ of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/ *//*************************************************************************************/
// Precompiled Headers // Precompiled Headers
#include "SHpch.h" #include "SHpch.h"
// External Dependencies
#include "FRC/SHFramerateController.h"
#include "Physics/SHPhysicsSystemInterface.h"
// Primary Header // Primary Header
#include "Time.hxx" #include "Time.hxx"
@ -26,4 +29,8 @@ namespace SHADE
{ {
return SHFrameRateController::GetRawDeltaTime(); return SHFrameRateController::GetRawDeltaTime();
} }
double Time::FixedDeltaTime::get()
{
return SHPhysicsSystemInterface::GetFixedDT();
}
} }

View File

@ -14,8 +14,6 @@ of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/ *//*************************************************************************************/
#pragma once #pragma once
#include "FRC/SHFramerateController.h"
namespace SHADE namespace SHADE
{ {
/// <summary> /// <summary>
@ -29,13 +27,18 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/// <summary> /// <summary>
/// Time taken to process the previous frame. /// Time taken to process the previous frame.
/// Note, is affected by TimeScale. Use UnscaledDeltaTime if you wish to retrieve
/// real world time. This is also affected by MaxDeltaTime clamping that
/// UnscaledDeltaTime is subject to.
/// </summary> /// </summary>
static property double DeltaTime static property double DeltaTime
{ {
double get(); double get();
} }
/// <summary>
/// Time taken for Physics simulations. You should use this for operations
/// within Script.FixedUpdate()
/// </summary>
static property double FixedDeltaTime
{
double get();
}
}; };
} }

View File

@ -41,6 +41,11 @@ public class PhysicsTest : Script
Debug.Log($"{Transform.LocalPosition.y}"); Debug.Log($"{Transform.LocalPosition.y}");
} }
protected override void fixedUpdate()
{
Debug.Log("Fixed Update");
}
protected override void onCollisionEnter(CollisionInfo info) protected override void onCollisionEnter(CollisionInfo info)
{ {
Debug.Log($"Collision Enter: {info.GameObject.Name}"); Debug.Log($"Collision Enter: {info.GameObject.Name}");