refactor: added pre, regular & post physics update interfaces

This commit is contained in:
Syntriax 2025-05-24 13:29:21 +03:00
parent b1970d93f9
commit 877a004a13
4 changed files with 46 additions and 3 deletions

View File

@ -0,0 +1,15 @@
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D;
/// <summary>
/// Represents a <see cref="IBehaviour"/> that listens to the phase after the physics simulation phase.
/// </summary>
public interface IPostPhysicsUpdate : IBehaviour
{
/// <summary>
/// Execute logic that should occur after the physics simulation has been updated.
/// </summary>
/// <param name="delta">The time elapsed since the last physics update, typically in seconds.</param>
void PostPhysicsUpdate(float delta);
}

View File

@ -0,0 +1,15 @@
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D;
/// <summary>
/// Represents a <see cref="IBehaviour"/> that listens to the phase before the physics simulation phase.
/// </summary>
public interface IPrePhysicsUpdate : IBehaviour
{
/// <summary>
/// Execute logic that should occur before the physics simulation is updated.
/// </summary>
/// <param name="delta">The time elapsed since the last physics update, typically in seconds.</param>
void PrePhysicsUpdate(float delta);
}

View File

@ -14,9 +14,12 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
protected readonly ICollisionDetector2D collisionDetector = null!;
protected readonly ICollisionResolver2D collisionResolver = null!;
private static System.Comparison<IBehaviour> SortByPriority() => (x, y) => y.Priority.CompareTo(x.Priority);
protected ActiveBehaviourCollectorSorted<IPrePhysicsUpdate> physicsPreUpdateCollector = new() { SortBy = SortByPriority() };
protected ActiveBehaviourCollectorSorted<IPhysicsUpdate> physicsUpdateCollector = new() { SortBy = SortByPriority() };
protected ActiveBehaviourCollectorSorted<IPostPhysicsUpdate> physicsPostUpdateCollector = new() { SortBy = SortByPriority() };
protected BehaviourCollector<IRigidBody2D> rigidBodyCollector = new();
protected BehaviourCollector<ICollider2D> colliderCollector = new();
protected BehaviourCollector<IPhysicsUpdate> physicsUpdateCollector = new();
public int IterationPerStep { get => _iterationPerStep; set => _iterationPerStep = value < 1 ? 1 : value; }
public float IterationPeriod { get => _iterationPeriod; set => _iterationPeriod = value.Max(0.0001f); }
@ -25,6 +28,12 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
{
float intervalDeltaTime = deltaTime / IterationPerStep;
foreach (IPrePhysicsUpdate physicsPreUpdate in physicsPreUpdateCollector)
physicsPreUpdate.PrePhysicsUpdate(deltaTime);
foreach (IPhysicsUpdate physicsUpdate in physicsUpdateCollector)
physicsUpdate.PhysicsUpdate(deltaTime);
for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++)
{
// Can Parallel
@ -92,8 +101,8 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
OnPhysicsIteration?.InvokeSafe(this, intervalDeltaTime);
}
foreach (IPhysicsUpdate physicsUpdate in physicsUpdateCollector)
physicsUpdate.PhysicsUpdate(deltaTime);
foreach (IPostPhysicsUpdate physicsPostUpdate in physicsPostUpdateCollector)
physicsPostUpdate.PostPhysicsUpdate(deltaTime);
OnPhysicsStep?.InvokeSafe(this, deltaTime);
}
@ -109,7 +118,9 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
protected override void OnEnteringUniverse(IUniverse universe)
{
physicsPreUpdateCollector.Assign(universe);
physicsUpdateCollector.Assign(universe);
physicsPostUpdateCollector.Assign(universe);
colliderCollector.Assign(universe);
rigidBodyCollector.Assign(universe);
@ -118,7 +129,9 @@ public class PhysicsEngine2D : UniverseObject, IPhysicsEngine2D
protected override void OnExitingUniverse(IUniverse universe)
{
physicsPreUpdateCollector.Unassign();
physicsUpdateCollector.Unassign();
physicsPostUpdateCollector.Unassign();
colliderCollector.Unassign();
rigidBodyCollector.Unassign();