2024-01-23 18:39:25 +03:00
|
|
|
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
|
|
|
|
{
|
2024-01-25 18:35:24 +03:00
|
|
|
private readonly List<IRigidBody2D> rigidBodies = new(32);
|
|
|
|
private readonly List<ICollider2D> colliders = new(64);
|
2024-01-23 18:39:25 +03:00
|
|
|
|
|
|
|
private int _iterationCount = 1;
|
2024-01-25 18:35:24 +03:00
|
|
|
private ICollisionDetector collisionDetector = new CollisionDetector();
|
2024-01-23 18:39:25 +03:00
|
|
|
|
|
|
|
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<ICollider2D>())
|
|
|
|
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++)
|
|
|
|
{
|
2024-01-25 18:35:24 +03:00
|
|
|
// Can Parallel
|
|
|
|
for (int i = 0; i < rigidBodies.Count; i++)
|
|
|
|
StepRigidBody(rigidBodies[i], intervalDeltaTime);
|
2024-01-23 18:39:25 +03:00
|
|
|
|
2024-01-25 18:35:24 +03:00
|
|
|
// Can Parallel
|
2024-01-23 18:39:25 +03:00
|
|
|
foreach (var collider in colliders)
|
|
|
|
collider.Recalculate();
|
2024-01-25 18:35:24 +03:00
|
|
|
|
|
|
|
// Can Parallel
|
|
|
|
for (int x = 0; x < colliders.Count; x++)
|
|
|
|
{
|
|
|
|
ICollider2D? colliderX = colliders[x];
|
2024-01-25 21:54:23 +03:00
|
|
|
for (int y = x + 1; y < colliders.Count; y++)
|
2024-01-25 18:35:24 +03:00
|
|
|
{
|
|
|
|
ICollider2D? colliderY = colliders[y];
|
|
|
|
|
2024-01-25 21:54:23 +03:00
|
|
|
if (colliderX.RigidBody2D == colliderY.RigidBody2D)
|
2024-01-25 18:35:24 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (collisionDetector.TryDetect(colliderX, colliderY, out CollisionDetectionInformation? information))
|
|
|
|
{
|
2024-01-25 21:54:23 +03:00
|
|
|
Vector2D displacementVector = .5f * information.Normal * information.Penetration;
|
|
|
|
information.Left.Transform.Position -= displacementVector;
|
|
|
|
information.Right.Transform.Position += displacementVector;
|
|
|
|
information.Left.Recalculate();
|
|
|
|
information.Right.Recalculate();
|
2024-01-25 18:35:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-23 18:39:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime)
|
|
|
|
{
|
|
|
|
rigidBody.Transform.Position += rigidBody.Velocity * intervalDeltaTime;
|
|
|
|
rigidBody.Transform.Rotation += rigidBody.AngularVelocity * intervalDeltaTime;
|
|
|
|
}
|
|
|
|
|
2024-01-25 18:35:24 +03:00
|
|
|
private static void CacheRotations(List<IRigidBody2D> rigidBodies, List<float> rotations)
|
|
|
|
{
|
|
|
|
rotations.Clear();
|
|
|
|
for (int i = 0; i < rigidBodies.Count; i++)
|
|
|
|
rotations.Add(rigidBodies[i].Transform.Rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void CachePositions(List<IRigidBody2D> rigidBodies, List<Vector2D> positions)
|
|
|
|
{
|
|
|
|
positions.Clear();
|
|
|
|
for (int i = 0; i < rigidBodies.Count; i++)
|
|
|
|
positions.Add(rigidBodies[i].Transform.Position);
|
|
|
|
}
|
|
|
|
|
2024-01-23 18:39:25 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|