feat: Collision Detector Circles
This commit is contained in:
parent
816f09fffe
commit
f7467a62ee
|
@ -0,0 +1,58 @@
|
|||
using Syntriax.Engine.Core;
|
||||
using Syntriax.Engine.Physics2D.Abstract;
|
||||
|
||||
namespace Syntriax.Engine.Physics2D;
|
||||
|
||||
public class CollisionDetector : ICollisionDetector
|
||||
{
|
||||
public bool TryDetect<T1, T2>(T1 left, T2 right, out CollisionDetectionInformation? collisionInformation)
|
||||
where T1 : ICollider2D
|
||||
where T2 : ICollider2D
|
||||
{
|
||||
collisionInformation = default;
|
||||
if (left is IShapeCollider2D shapeColliderLeft)
|
||||
{
|
||||
if (right is IShapeCollider2D shapeColliderRight)
|
||||
return DetectShapeShape(shapeColliderLeft, shapeColliderRight, out collisionInformation);
|
||||
else if (right is ICircleCollider2D circleColliderRight)
|
||||
return DetectShapeCircle(shapeColliderLeft, circleColliderRight, out collisionInformation);
|
||||
}
|
||||
else if (left is ICircleCollider2D circleColliderLeft)
|
||||
{
|
||||
if (right is IShapeCollider2D shapeColliderRight)
|
||||
return DetectShapeCircle(shapeColliderRight, circleColliderLeft, out collisionInformation);
|
||||
else if (right is ICircleCollider2D circleColliderRight)
|
||||
return DetectCircleCircle(circleColliderLeft, circleColliderRight, out collisionInformation);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool DetectShapeShape(IShapeCollider2D left, IShapeCollider2D right, out CollisionDetectionInformation? collisionInformation)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
private static bool DetectShapeCircle(IShapeCollider2D shapeCollider, ICircleCollider2D circleCollider, out CollisionDetectionInformation? collisionInformation)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
private static bool DetectCircleCircle(ICircleCollider2D left, ICircleCollider2D right, out CollisionDetectionInformation? collisionInformation)
|
||||
{
|
||||
collisionInformation = default;
|
||||
|
||||
Vector2D leftToRightCenter = left.CircleWorld.Center.FromTo(right.CircleWorld.Center);
|
||||
float distanceCircleCenterSquared = leftToRightCenter.MagnitudeSquared;
|
||||
float radiusSumSquared = left.CircleWorld.RadiusSquared + right.CircleWorld.RadiusSquared;
|
||||
|
||||
float circleSurfaceDistanceSquared = distanceCircleCenterSquared - radiusSumSquared;
|
||||
|
||||
bool collision = circleSurfaceDistanceSquared <= 0f;
|
||||
|
||||
if (collision)
|
||||
collisionInformation = new(left, right, leftToRightCenter.Normalized, Math.Sqrt(circleSurfaceDistanceSquared));
|
||||
|
||||
return collision;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
using Syntriax.Engine.Physics2D.Abstract;
|
||||
|
||||
namespace Syntriax.Engine.Physics2D;
|
||||
|
||||
public interface ICollisionDetector
|
||||
{
|
||||
bool TryDetect<T1, T2>(T1 left, T2 right, out CollisionDetectionInformation? collisionInformation) where T1 : ICollider2D where T2 : ICollider2D;
|
||||
}
|
Loading…
Reference in New Issue