using Syntriax.Engine.Core; using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Physics2D.Abstract; namespace Syntriax.Engine.Physics2D; public class PhysicsEngine2DCollector : HierarchyObjectBase, IPhysicsEngine2D { public event IPhysicsEngine2D.OnPhysicsIterationDelegate? OnPhysicsIteration = null; public event IPhysicsEngine2D.OnPhysicsStepDelegate? OnPhysicsStep = null; private int _iterationPerStep = 1; protected readonly ICollisionDetector2D collisionDetector = null!; protected readonly ICollisionResolver2D collisionResolver = null!; protected BehaviourCollector rigidBodyCollector = new(); protected BehaviourCollector colliderCollector = new(); protected BehaviourCollector physicsUpdateCollector = new(); public int IterationPerStep { get => _iterationPerStep; set => _iterationPerStep = value < 1 ? 1 : value; } public void Step(float deltaTime) { float intervalDeltaTime = deltaTime / IterationPerStep; for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++) { // Can Parallel foreach (var rigidBody in rigidBodyCollector) StepRigidBody(rigidBody, intervalDeltaTime); // Can Parallel foreach (var collider in colliderCollector) collider.Recalculate(); // Can Parallel for (int x = 0; x < colliderCollector.Behaviours.Count; x++) { ICollider2D? colliderX = colliderCollector.Behaviours[x]; if (!colliderX.IsActive) return; for (int y = x + 1; y < colliderCollector.Behaviours.Count; y++) { ICollider2D? colliderY = colliderCollector.Behaviours[y]; if (!colliderY.IsActive) return; if (colliderX.RigidBody2D == colliderY.RigidBody2D) continue; bool bothCollidersAreTriggers = colliderX.IsTrigger && colliderX.IsTrigger == colliderY.IsTrigger; if (bothCollidersAreTriggers) continue; bool bothCollidersAreStatic = colliderX.RigidBody2D?.IsStatic ?? true && colliderX.RigidBody2D?.IsStatic == colliderY.RigidBody2D?.IsStatic; if (bothCollidersAreStatic) continue; if (collisionDetector.TryDetect(colliderX, colliderY, out CollisionDetectionInformation information)) { if (colliderX.IsTrigger) { colliderX.Trigger(colliderY); continue; } else if (colliderY.IsTrigger) { colliderY.Trigger(colliderY); continue; } colliderX.Detect(information); colliderY.Detect(information); collisionResolver?.Resolve(information); } } } OnPhysicsIteration?.Invoke(this, intervalDeltaTime); } foreach (IPhysicsUpdate physicsUpdate in physicsUpdateCollector) physicsUpdate.PhysicsUpdate(deltaTime); OnPhysicsStep?.Invoke(this, deltaTime); } private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime) { if (rigidBody.IsStatic || !rigidBody.IsActive) return; rigidBody.Transform.Position += rigidBody.Velocity * intervalDeltaTime; rigidBody.Transform.Rotation += rigidBody.AngularVelocity * intervalDeltaTime; } protected override void OnEnteringHierarchy(IGameManager gameManager) { physicsUpdateCollector.Assign(gameManager); colliderCollector.Assign(gameManager); rigidBodyCollector.Assign(gameManager); } protected override void OnExitingHierarchy(IGameManager gameManager) { physicsUpdateCollector.Unassign(); colliderCollector.Unassign(); rigidBodyCollector.Unassign(); } public PhysicsEngine2DCollector() { collisionDetector = new CollisionDetector2D(); collisionResolver = new CollisionResolver2D(); } public PhysicsEngine2DCollector(ICollisionDetector2D collisionDetector, ICollisionResolver2D collisionResolver) { this.collisionDetector = collisionDetector; this.collisionResolver = collisionResolver; } }