feat: Test Collision Detection & Move

This commit is contained in:
Syntriax 2024-01-25 18:35:24 +03:00
parent f5be49609b
commit 0af1b11396
1 changed files with 40 additions and 6 deletions

View File

@ -4,17 +4,16 @@ using System.Collections.Generic;
using Syntriax.Engine.Core; using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Physics2D.Abstract; using Syntriax.Engine.Physics2D.Abstract;
using Syntriax.Engine.Physics2D.Primitives;
namespace Syntriax.Engine.Physics2D; namespace Syntriax.Engine.Physics2D;
public class PhysicsEngine2D : IPhysicsEngine2D public class PhysicsEngine2D : IPhysicsEngine2D
{ {
private List<IRigidBody2D> rigidBodies = new List<IRigidBody2D>(32); private readonly List<IRigidBody2D> rigidBodies = new(32);
private List<ICollider2D> colliders = new List<ICollider2D>(64); private readonly List<ICollider2D> colliders = new(64);
private int _iterationCount = 1; private int _iterationCount = 1;
private ICollisionDetector collisionDetector = new CollisionDetector();
public int IterationCount { get => _iterationCount; set => _iterationCount = value < 1 ? 1 : value; } public int IterationCount { get => _iterationCount; set => _iterationCount = value < 1 ? 1 : value; }
@ -43,11 +42,32 @@ public class PhysicsEngine2D : IPhysicsEngine2D
for (int iterationIndex = 0; iterationIndex < IterationCount; iterationIndex++) for (int iterationIndex = 0; iterationIndex < IterationCount; iterationIndex++)
{ {
foreach (var rigidBody in rigidBodies) // Can Parallel
StepRigidBody(rigidBody, intervalDeltaTime); for (int i = 0; i < rigidBodies.Count; i++)
StepRigidBody(rigidBodies[i], intervalDeltaTime);
// Can Parallel
foreach (var collider in colliders) foreach (var collider in colliders)
collider.Recalculate(); collider.Recalculate();
// Can Parallel
for (int x = 0; x < colliders.Count; x++)
{
ICollider2D? colliderX = colliders[x];
for (int y = 0; y < colliders.Count; y++)
{
ICollider2D? colliderY = colliders[y];
if (colliderX.RigidBody2D == colliderY.RigidBody2D && colliderY.RigidBody2D is null)
continue;
if (collisionDetector.TryDetect(colliderX, colliderY, out CollisionDetectionInformation? information))
{
information.Left.Transform.Position -= .5f * information.Normal * information.Penetration;
information.Right.Transform.Position += .5f * information.Normal * information.Penetration;
}
}
}
} }
} }
@ -57,6 +77,20 @@ public class PhysicsEngine2D : IPhysicsEngine2D
rigidBody.Transform.Rotation += rigidBody.AngularVelocity * intervalDeltaTime; rigidBody.Transform.Rotation += rigidBody.AngularVelocity * intervalDeltaTime;
} }
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);
}
private void OnBehaviourAdded(IBehaviourController controller, IBehaviour behaviour) private void OnBehaviourAdded(IBehaviourController controller, IBehaviour behaviour)
{ {
if (behaviour is not ICollider2D collider2D) if (behaviour is not ICollider2D collider2D)