using System.Collections.Generic; using Syntriax.Engine.Core; using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Physics2D.Abstract; namespace Syntriax.Engine.Physics2D; public class PhysicsEngine2D : IPhysicsEngine2D { private readonly List rigidBodies = new(32); private readonly List colliders = new(64); private int _iterationCount = 1; private ICollisionDetector2D collisionDetector = new CollisionDetector2D(); private ICollisionResolver2D collisionResolver = new CollisionResolver2D(); public int IterationCount { get => _iterationCount; set => _iterationCount = value < 1 ? 1 : value; } public void AddRigidBody(IRigidBody2D rigidBody) { if (rigidBodies.Contains(rigidBody)) return; rigidBodies.Add(rigidBody); foreach (var collider2D in rigidBody.BehaviourController.GetBehaviours()) colliders.Add(collider2D); rigidBody.BehaviourController.OnBehaviourAdded += OnBehaviourAdded; rigidBody.BehaviourController.OnBehaviourRemoved += OnBehaviourRemoved; } public void RemoveRigidBody(IRigidBody2D rigidBody) { rigidBodies.Remove(rigidBody); } public void Step(float deltaTime) { float intervalDeltaTime = deltaTime / IterationCount; for (int iterationIndex = 0; iterationIndex < IterationCount; iterationIndex++) { // Can Parallel for (int i = 0; i < rigidBodies.Count; i++) StepRigidBody(rigidBodies[i], intervalDeltaTime); // Can Parallel foreach (var collider in colliders) collider.Recalculate(); // Can Parallel for (int x = 0; x < colliders.Count; x++) { ICollider2D? colliderX = colliders[x]; for (int y = x + 1; y < colliders.Count; y++) { ICollider2D? colliderY = colliders[y]; 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.OnTriggered?.Invoke(colliderX, colliderY); continue; } else if (colliderY.IsTrigger) { colliderY.OnTriggered?.Invoke(colliderY, colliderY); continue; } colliderX.OnCollisionDetected?.Invoke(colliderX, information); colliderY.OnCollisionDetected?.Invoke(colliderY, information); collisionResolver?.Resolve(information); } } } } } private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime) { if (rigidBody.IsStatic) return; rigidBody.Transform.Position += rigidBody.Velocity * intervalDeltaTime; rigidBody.Transform.Rotation += rigidBody.AngularVelocity * intervalDeltaTime; } private void OnBehaviourAdded(IBehaviourController controller, IBehaviour behaviour) { if (behaviour is not ICollider2D collider2D) return; colliders.Add(collider2D); } private void OnBehaviourRemoved(IBehaviourController controller, IBehaviour behaviour) { if (behaviour is not ICollider2D collider2D) return; colliders.Remove(collider2D); } }