feat: Improved Test Collision Resolving

This commit is contained in:
Syntriax 2024-01-25 21:54:23 +03:00
parent 3c39e6709d
commit 266443504f
1 changed files with 7 additions and 5 deletions

View File

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Syntriax.Engine.Core; using Syntriax.Engine.Core;
@ -54,17 +53,20 @@ public class PhysicsEngine2D : IPhysicsEngine2D
for (int x = 0; x < colliders.Count; x++) for (int x = 0; x < colliders.Count; x++)
{ {
ICollider2D? colliderX = colliders[x]; ICollider2D? colliderX = colliders[x];
for (int y = 0; y < colliders.Count; y++) for (int y = x + 1; y < colliders.Count; y++)
{ {
ICollider2D? colliderY = colliders[y]; ICollider2D? colliderY = colliders[y];
if (colliderX.RigidBody2D == colliderY.RigidBody2D && colliderY.RigidBody2D is null) if (colliderX.RigidBody2D == colliderY.RigidBody2D)
continue; continue;
if (collisionDetector.TryDetect(colliderX, colliderY, out CollisionDetectionInformation? information)) if (collisionDetector.TryDetect(colliderX, colliderY, out CollisionDetectionInformation? information))
{ {
information.Left.Transform.Position -= .5f * information.Normal * information.Penetration; Vector2D displacementVector = .5f * information.Normal * information.Penetration;
information.Right.Transform.Position += .5f * information.Normal * information.Penetration; information.Left.Transform.Position -= displacementVector;
information.Right.Transform.Position += displacementVector;
information.Left.Recalculate();
information.Right.Recalculate();
} }
} }
} }