chore: Removed Old Physics Checks & Resolvers
This commit is contained in:
parent
4ae9578123
commit
64205353e5
|
@ -48,80 +48,8 @@ public class PhysicsEngine2D : IPhysicsEngine2D
|
|||
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<ICollider2D> collider2Ds, Action<ICollider2D, ICollider2D, Vector2D> 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--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue