using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Syntriax.Engine.Core; using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Physics2D.Abstract; using Syntriax.Engine.Physics2D.Primitives; namespace Syntriax.Engine.Physics2D; public class PhysicsEngine2D : IPhysicsEngine2D { private List rigidBodies = new List(32); private List colliders = new List(64); private int _iterationCount = 1; 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); } private List lines = new List(32); public void Step(float deltaTime) { float intervalDeltaTime = deltaTime / IterationCount; for (int iterationIndex = 0; iterationIndex < IterationCount; iterationIndex++) { foreach (var rigidBody in rigidBodies) StepRigidBody(rigidBody, intervalDeltaTime); foreach (var collider in colliders) collider.Recalculate(); for (int i = colliders.Count - 1; i >= 0; i--) CheckCollisions(colliders[i], colliders, (collider1, collider2, collisionVertex) => ResolveCollision(collider1, collider2, collisionVertex, intervalDeltaTime) ); } } private void ResolveCollision(ICollider2D c1, ICollider2D c2, Vector2D vertex, float intervalDeltaTime) { if (c1.RigidBody2D is not IRigidBody2D) return; Line vertexTrajectory = new Line(vertex - c1.RigidBody2D.Velocity * intervalDeltaTime, vertex); if (vertexTrajectory.LengthSquared <= 0.001f) return; c2.Vertices.ToLines(lines); Vector2D normal = Vector2D.Up; float t = 1f; bool hasIntersectedWithLines = false; foreach (var line in lines) { if (!vertexTrajectory.Intersects(line, out Vector2D? intersectionPoint)) continue; if (intersectionPoint is not Vector2D ip) continue; float tTemp = vertexTrajectory.GetT(ip); if (tTemp > t) // React to the earliest collision value continue; t = tTemp; Vector2D lineDirection = line.Direction; normal = new(lineDirection.Y, lineDirection.X); hasIntersectedWithLines = true; } if (!hasIntersectedWithLines) return; float rewindInterval = intervalDeltaTime - t * intervalDeltaTime; StepRigidBody(c1.RigidBody2D, -rewindInterval); c1.RigidBody2D.Velocity = Vector2D.Reflect(c1.RigidBody2D.Velocity, normal); StepRigidBody(c1.RigidBody2D, rewindInterval); } private void CheckCollisions(ICollider2D collider2D, List collider2Ds, Action OnCollisionDetectedAction) { for (int i = 0; i < collider2Ds.Count; i++) { ICollider2D collider2DItem = collider2Ds[i]; if (collider2DItem == collider2D) continue; for (int y = 0; y < collider2D.Vertices.Count; y++) { Vector2D vertex = collider2D.Vertices[y]; if (collider2DItem.CheckCollision(vertex)) { OnCollisionDetectedAction?.Invoke(collider2D, collider2DItem, vertex); OnCollisionDetectedAction?.Invoke(collider2DItem, collider2D, vertex); collider2D.Recalculate(); collider2DItem.Recalculate(); y--; } } } } private void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime) { Vector2D nextPosition = rigidBody.Transform.Position; nextPosition += rigidBody.Velocity * intervalDeltaTime; rigidBody.Transform.Position = nextPosition; } 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); } }