Files
mrgameeng/src/MrGameEng.Simulation/Collisions/Collider.cs
T

64 lines
2.2 KiB
C#

using Friflo.Engine.ECS;
using Microsoft.Xna.Framework;
namespace MrGameEng.Collisions;
/// <summary>Shape of a <see cref="Collider"/>.</summary>
public enum ColliderShape : byte
{
/// <summary>Circle of <see cref="Collider.Radius"/>.</summary>
Circle,
/// <summary>Axis-aligned box of <see cref="Collider.HalfExtents"/>. Does not rotate with the entity.</summary>
Box,
}
/// <summary>
/// Collision shape component. Create via <see cref="Circle"/> or <see cref="Box"/> —
/// the struct default has no size and collides with nothing.
/// Positions come from <c>Transform2D</c>; <see cref="Offset"/> shifts the shape
/// relative to it (entity scale and rotation are not applied to collider shapes).
/// </summary>
public struct Collider : IComponent
{
/// <summary>Shape kind.</summary>
public ColliderShape Shape;
/// <summary>Circle radius in world units (<see cref="ColliderShape.Circle"/> only).</summary>
public float Radius;
/// <summary>Half extents of the box (<see cref="ColliderShape.Box"/> only).</summary>
public Vector2 HalfExtents;
/// <summary>Shape center offset from the entity's transform position.</summary>
public Vector2 Offset;
/// <summary>Bit mask of layers this collider belongs to.</summary>
public uint Layer;
/// <summary>Bit mask of layers this collider collides with. A pair is reported only when the masks agree both ways.</summary>
public uint CollidesWith;
/// <summary>Creates a circle collider on layer 1 colliding with everything.</summary>
public static Collider Circle(float radius, Vector2 offset = default) =>
new()
{
Shape = ColliderShape.Circle,
Radius = radius,
Offset = offset,
Layer = 1,
CollidesWith = uint.MaxValue,
};
/// <summary>Creates a box collider on layer 1 colliding with everything.</summary>
public static Collider Box(float width, float height, Vector2 offset = default) =>
new()
{
Shape = ColliderShape.Box,
HalfExtents = new Vector2(width / 2f, height / 2f),
Offset = offset,
Layer = 1,
CollidesWith = uint.MaxValue,
};
}