feat: Static & Mass Consideration For Rigidbodies

This commit is contained in:
Syntriax 2024-01-27 15:07:40 +03:00
parent bd43d39367
commit 9556be6f17
1 changed files with 25 additions and 3 deletions

View File

@ -62,9 +62,31 @@ public class PhysicsEngine2D : IPhysicsEngine2D
if (collisionDetector.TryDetect(colliderX, colliderY, out CollisionDetectionInformation information))
{
Vector2D displacementVector = .5f * information.Normal * information.Penetration;
information.Left.Transform.Position -= displacementVector;
information.Right.Transform.Position += displacementVector;
Vector2D displacementVector = information.Normal * information.Penetration;
bool isStaticLeft = information.Left.RigidBody2D?.IsStatic ?? true;
bool isStaticRight = information.Right.RigidBody2D?.IsStatic ?? true;
if (isStaticLeft && isStaticRight)
continue;
else if (isStaticLeft)
information.Right.Transform.Position += displacementVector;
else if (isStaticRight)
information.Left.Transform.Position -= displacementVector;
else
{
float leftMass = information.Left.RigidBody2D?.Mass ?? float.Epsilon;
float rightMass = information.Right.RigidBody2D?.Mass ?? float.Epsilon;
float massSum = leftMass + rightMass;
float leftMomentumPercentage = leftMass / massSum;
float rightMomentumPercentage = rightMass / massSum;
information.Right.Transform.Position += leftMomentumPercentage * displacementVector;
information.Left.Transform.Position -= rightMomentumPercentage * displacementVector;
}
information.Left.Recalculate();
information.Right.Recalculate();
}