refactor: renamed primitives
This commit is contained in:
parent
62b43025b9
commit
9ecf0b900f
@ -3,17 +3,17 @@ using Syntriax.Engine.Physics2D.Primitives;
|
||||
namespace Syntriax.Engine.Physics2D.Abstract;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a <see cref="ICollider2D"/> with a custom <see cref="Shape"/>.
|
||||
/// Represents a <see cref="ICollider2D"/> with a custom <see cref="Shape2D"/>.
|
||||
/// </summary>
|
||||
public interface IShapeCollider2D : ICollider2D
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the local <see cref="Shape"/> of the <see cref="ICollider2D"/>.
|
||||
/// Gets or sets the local <see cref="Shape2D"/> of the <see cref="ICollider2D"/>.
|
||||
/// </summary>
|
||||
Shape ShapeLocal { get; set; }
|
||||
Shape2D ShapeLocal { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the world space representation of the <see cref="Shape"/>.
|
||||
/// Gets the world space representation of the <see cref="Shape2D"/>.
|
||||
/// </summary>
|
||||
Shape ShapeWorld { get; }
|
||||
Shape2D ShapeWorld { get; }
|
||||
}
|
||||
|
@ -5,13 +5,13 @@ namespace Syntriax.Engine.Physics2D;
|
||||
|
||||
public class Collider2DShapeBehaviour : Collider2DBehaviourBase, IShapeCollider2D
|
||||
{
|
||||
public Shape ShapeWorld { get => _shapeWorld; protected set => _shapeWorld = value; }
|
||||
public Shape ShapeLocal { get; set; } = Shape.Box;
|
||||
public Shape2D ShapeWorld { get => _shapeWorld; protected set => _shapeWorld = value; }
|
||||
public Shape2D ShapeLocal { get; set; } = Shape2D.Box;
|
||||
|
||||
private Shape _shapeWorld = Shape.Box.CreateCopy();
|
||||
private Shape2D _shapeWorld = Shape2D.Box.CreateCopy();
|
||||
|
||||
public override void CalculateCollider() => Transform.TransformShape(ShapeLocal, ref _shapeWorld);
|
||||
|
||||
public Collider2DShapeBehaviour() { }
|
||||
public Collider2DShapeBehaviour(Shape shape) => ShapeLocal = shape;
|
||||
public Collider2DShapeBehaviour(Shape2D shape) => ShapeLocal = shape;
|
||||
}
|
||||
|
@ -49,8 +49,8 @@ public class CollisionDetector2D : ICollisionDetector2D
|
||||
{
|
||||
Vector2D projectionVector = vertices[indexProjection].FromTo(vertices[(indexProjection + 1) % count]).Perpendicular().Normalized;
|
||||
|
||||
Projection leftProjection = left.ShapeWorld.ToProjection(projectionVector);
|
||||
Projection rightProjection = right.ShapeWorld.ToProjection(projectionVector);
|
||||
Projection1D leftProjection = left.ShapeWorld.ToProjection(projectionVector);
|
||||
Projection1D rightProjection = right.ShapeWorld.ToProjection(projectionVector);
|
||||
|
||||
if (!leftProjection.Overlaps(rightProjection, out float depth))
|
||||
return false;
|
||||
@ -73,8 +73,8 @@ public class CollisionDetector2D : ICollisionDetector2D
|
||||
{
|
||||
Vector2D projectionVector = vertices[indexProjection].FromTo(vertices[(indexProjection + 1) % count]).Perpendicular().Normalized;
|
||||
|
||||
Projection shapeProjection = shapeCollider.ShapeWorld.ToProjection(projectionVector);
|
||||
Projection circleProjection = circleCollider.CircleWorld.ToProjection(projectionVector);
|
||||
Projection1D shapeProjection = shapeCollider.ShapeWorld.ToProjection(projectionVector);
|
||||
Projection1D circleProjection = circleCollider.CircleWorld.ToProjection(projectionVector);
|
||||
|
||||
if (!shapeProjection.Overlaps(circleProjection, out float depth))
|
||||
return false;
|
||||
@ -86,8 +86,8 @@ public class CollisionDetector2D : ICollisionDetector2D
|
||||
{
|
||||
Vector2D shapeToCircleProjectionVector = shapeCollider.Transform.Position.FromTo(circleCollider.CircleWorld.Center).Normalized;
|
||||
|
||||
Projection shapeProjection = shapeCollider.ShapeWorld.ToProjection(shapeToCircleProjectionVector);
|
||||
Projection circleProjection = circleCollider.CircleWorld.ToProjection(shapeToCircleProjectionVector);
|
||||
Projection1D shapeProjection = shapeCollider.ShapeWorld.ToProjection(shapeToCircleProjectionVector);
|
||||
Projection1D circleProjection = circleCollider.CircleWorld.ToProjection(shapeToCircleProjectionVector);
|
||||
|
||||
if (!shapeProjection.Overlaps(circleProjection, out float depth))
|
||||
return false;
|
||||
@ -105,8 +105,8 @@ public class CollisionDetector2D : ICollisionDetector2D
|
||||
|
||||
Vector2D leftToRightCenterProjectionVector = left.CircleWorld.Center.FromTo(right.CircleWorld.Center).Normalized;
|
||||
|
||||
Projection leftProjection = left.CircleWorld.ToProjection(leftToRightCenterProjectionVector);
|
||||
Projection rightProjection = right.CircleWorld.ToProjection(leftToRightCenterProjectionVector);
|
||||
Projection1D leftProjection = left.CircleWorld.ToProjection(leftToRightCenterProjectionVector);
|
||||
Projection1D rightProjection = right.CircleWorld.ToProjection(leftToRightCenterProjectionVector);
|
||||
|
||||
bool collision = leftProjection.Overlaps(rightProjection, out float depth);
|
||||
|
||||
|
@ -5,8 +5,8 @@ namespace Syntriax.Engine.Physics2D;
|
||||
|
||||
public static partial class Physics2D
|
||||
{
|
||||
public static bool Overlaps(this Shape shape, Vector2D point) => Overlaps(shape, point, out float _);
|
||||
public static bool Overlaps(this Shape shape, Vector2D point, out float depth)
|
||||
public static bool Overlaps(this Shape2D shape, Vector2D point) => Overlaps(shape, point, out float _);
|
||||
public static bool Overlaps(this Shape2D shape, Vector2D point, out float depth)
|
||||
{
|
||||
depth = float.MaxValue;
|
||||
System.Collections.Generic.IReadOnlyList<Vector2D> vertices = shape.Vertices;
|
||||
@ -16,7 +16,7 @@ public static partial class Physics2D
|
||||
{
|
||||
Vector2D projectionVector = vertices[indexProjection].FromTo(vertices[(indexProjection + 1) % count]).Perpendicular().Normalized;
|
||||
|
||||
Projection shapeProjection = shape.ToProjection(projectionVector);
|
||||
Projection1D shapeProjection = shape.ToProjection(projectionVector);
|
||||
float projectedPoint = point.Dot(projectionVector);
|
||||
|
||||
if (shapeProjection.Max < projectedPoint || shapeProjection.Min > projectedPoint)
|
||||
@ -90,5 +90,5 @@ public static partial class Physics2D
|
||||
return originalTriangleArea.ApproximatelyEquals(pointTriangleAreasSum, float.Epsilon * 3f);
|
||||
}
|
||||
|
||||
public static bool LaysOn(this Vector2D point, Line line) => Line.Intersects(line, point);
|
||||
public static bool LaysOn(this Vector2D point, Line2D line) => Line2D.Intersects(line, point);
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ public readonly struct Circle(Vector2D center, float radius)
|
||||
/// <summary>
|
||||
/// Projects the <see cref="Circle"/> onto the specified <see cref="Vector2D"/>.
|
||||
/// </summary>
|
||||
public static Projection Project(Circle circle, Vector2D projectionVector)
|
||||
public static Projection1D Project(Circle circle, Vector2D projectionVector)
|
||||
{
|
||||
float projectedCenter = circle.Center.Dot(projectionVector);
|
||||
return new(projectedCenter - circle.Radius, projectedCenter + circle.Radius);
|
||||
@ -101,7 +101,7 @@ public static class CircleExtensions
|
||||
/// <summary>
|
||||
/// Projects the <see cref="Circle"/> onto the specified <see cref="Vector2D"/>.
|
||||
/// </summary>
|
||||
public static Projection ToProjection(this Circle circle, Vector2D projectionVector) => Circle.Project(circle, projectionVector);
|
||||
public static Projection1D ToProjection(this Circle circle, Vector2D projectionVector) => Circle.Project(circle, projectionVector);
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the <see cref="Circle"/> by the specified <see cref="ITransform"/>.
|
||||
|
@ -11,64 +11,64 @@ namespace Syntriax.Engine.Physics2D.Primitives;
|
||||
/// <remarks>
|
||||
/// Initializes a new instance of the Line struct with the specified endpoints.
|
||||
/// </remarks>
|
||||
/// <param name="from">The starting point of the <see cref="Line"/> segment.</param>
|
||||
/// <param name="to">The ending point of the <see cref="Line"/> segment.</param>
|
||||
/// <param name="from">The starting point of the <see cref="Line2D"/> segment.</param>
|
||||
/// <param name="to">The ending point of the <see cref="Line2D"/> segment.</param>
|
||||
[System.Diagnostics.DebuggerDisplay("From: {From.ToString(),nq}, To: {To.ToString(),nq}, Direction: {Direction.ToString(),nq}, Length: {Length}")]
|
||||
public readonly struct Line(Vector2D from, Vector2D to)
|
||||
public readonly struct Line2D(Vector2D from, Vector2D to)
|
||||
{
|
||||
/// <summary>
|
||||
/// The starting point of the <see cref="Line"/> segment.
|
||||
/// The starting point of the <see cref="Line2D"/> segment.
|
||||
/// </summary>
|
||||
public readonly Vector2D From = from;
|
||||
|
||||
/// <summary>
|
||||
/// The ending point of the <see cref="Line"/> segment.
|
||||
/// The ending point of the <see cref="Line2D"/> segment.
|
||||
/// </summary>
|
||||
public readonly Vector2D To = to;
|
||||
|
||||
/// <summary>
|
||||
/// The reversed <see cref="Line"/> segment.
|
||||
/// The reversed <see cref="Line2D"/> segment.
|
||||
/// </summary>
|
||||
public readonly Line Reversed => new(To, From);
|
||||
public readonly Line2D Reversed => new(To, From);
|
||||
|
||||
/// <summary>
|
||||
/// The normalized direction <see cref="Vector2D"/> of the <see cref="Line"/> segment.
|
||||
/// The normalized direction <see cref="Vector2D"/> of the <see cref="Line2D"/> segment.
|
||||
/// </summary>
|
||||
public readonly Vector2D Direction => From.FromTo(To).Normalize();
|
||||
|
||||
/// <summary>
|
||||
/// The length of the <see cref="Line"/> segment.
|
||||
/// The length of the <see cref="Line2D"/> segment.
|
||||
/// </summary>
|
||||
public readonly float Length => From.FromTo(To).Length();
|
||||
|
||||
/// <summary>
|
||||
/// The squared length of the <see cref="Line"/> segment.
|
||||
/// The squared length of the <see cref="Line2D"/> segment.
|
||||
/// </summary>
|
||||
public readonly float LengthSquared => From.FromTo(To).LengthSquared();
|
||||
|
||||
/// <summary>
|
||||
/// The equation of the <see cref="Line"/> defined by this <see cref="Line"/> segment.
|
||||
/// The equation of the <see cref="Line2D"/> defined by this <see cref="Line2D"/> segment.
|
||||
/// </summary>
|
||||
public static LineEquation GetLineEquation(Line line)
|
||||
public static Line2DEquation GetLineEquation(Line2D line)
|
||||
{
|
||||
Vector2D slopeVector = line.From.FromTo(line.To);
|
||||
float slope = slopeVector.Y / slopeVector.X;
|
||||
|
||||
float yOffset = line.From.Y - (slope * line.From.X);
|
||||
|
||||
return new LineEquation(slope, yOffset);
|
||||
return new Line2DEquation(slope, yOffset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the specified <see cref="Vector2D"/> lies on the <see cref="Line"/>.
|
||||
/// Determines whether the specified <see cref="Vector2D"/> lies on the <see cref="Line2D"/>.
|
||||
/// </summary>
|
||||
public static bool Intersects(Line line, Vector2D point)
|
||||
=> LineEquation.Resolve(GetLineEquation(line), point.X).ApproximatelyEquals(point.Y);
|
||||
public static bool Intersects(Line2D line, Vector2D point)
|
||||
=> Line2DEquation.Resolve(GetLineEquation(line), point.X).ApproximatelyEquals(point.Y);
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the parameter 't' representing the point's position on the <see cref="Line"/> segment.
|
||||
/// Calculates the parameter 't' representing the point's position on the <see cref="Line2D"/> segment.
|
||||
/// </summary>
|
||||
public static float GetT(Line line, Vector2D point)
|
||||
public static float GetT(Line2D line, Vector2D point)
|
||||
{
|
||||
float fromX = MathF.Abs(line.From.X);
|
||||
float toX = MathF.Abs(line.To.X);
|
||||
@ -91,9 +91,9 @@ public readonly struct Line(Vector2D from, Vector2D to)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the <see cref="Line"/> segment intersects with another <see cref="Line"/> segment.
|
||||
/// Checks if the <see cref="Line2D"/> segment intersects with another <see cref="Line2D"/> segment.
|
||||
/// </summary>
|
||||
public static bool Intersects(Line left, Line right)
|
||||
public static bool Intersects(Line2D left, Line2D right)
|
||||
{
|
||||
int o1 = Vector2D.Orientation(left.From, left.To, right.From);
|
||||
int o2 = Vector2D.Orientation(left.From, left.To, right.To);
|
||||
@ -112,9 +112,9 @@ public readonly struct Line(Vector2D from, Vector2D to)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the point lies within the <see cref="Line"/> segment.
|
||||
/// Checks if the point lies within the <see cref="Line2D"/> segment.
|
||||
/// </summary>
|
||||
public static bool OnSegment(Line line, Vector2D point)
|
||||
public static bool OnSegment(Line2D line, Vector2D point)
|
||||
{
|
||||
if (point.X <= MathF.Max(line.From.X, line.To.X) && point.X >= MathF.Min(line.From.X, line.To.X) &&
|
||||
point.Y <= MathF.Max(line.From.Y, line.To.Y) && point.Y >= MathF.Min(line.From.Y, line.To.Y))
|
||||
@ -124,9 +124,9 @@ public readonly struct Line(Vector2D from, Vector2D to)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether two <see cref="Line"/> segments intersect.
|
||||
/// Determines whether two <see cref="Line2D"/> segments intersect.
|
||||
/// </summary>
|
||||
public static bool Intersects(Line left, Line right, [NotNullWhen(returnValue: true)] out Vector2D? point)
|
||||
public static bool Intersects(Line2D left, Line2D right, [NotNullWhen(returnValue: true)] out Vector2D? point)
|
||||
{
|
||||
point = null;
|
||||
|
||||
@ -139,15 +139,15 @@ public readonly struct Line(Vector2D from, Vector2D to)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds the point of intersection between two <see cref="Line"/> segments.
|
||||
/// Finds the point of intersection between two <see cref="Line2D"/> segments.
|
||||
/// </summary>
|
||||
public static Vector2D IntersectionPoint(Line left, Line right)
|
||||
public static Vector2D IntersectionPoint(Line2D left, Line2D right)
|
||||
=> Vector2D.Lerp(left.From, left.To, IntersectionParameterT(left, right));
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the parameter 't' representing the intersection point's position on the <see cref="Line"/> segment.
|
||||
/// Calculates the parameter 't' representing the intersection point's position on the <see cref="Line2D"/> segment.
|
||||
/// </summary>
|
||||
public static float IntersectionParameterT(Line left, Line right)
|
||||
public static float IntersectionParameterT(Line2D left, Line2D right)
|
||||
{
|
||||
float numerator = (left.From.X - right.From.X) * (right.From.Y - right.To.Y) - (left.From.Y - right.From.Y) * (right.From.X - right.To.X);
|
||||
float denominator = (left.From.X - left.To.X) * (right.From.Y - right.To.Y) - (left.From.Y - left.To.Y) * (right.From.X - right.To.X);
|
||||
@ -160,21 +160,18 @@ public readonly struct Line(Vector2D from, Vector2D to)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Linearly interpolates between the two endpoints of the <see cref="Line"/> segment using parameter 't'.
|
||||
/// Linearly interpolates between the two endpoints of the <see cref="Line2D"/> segment using parameter 't'.
|
||||
/// </summary>
|
||||
public static Vector2D Lerp(Line line, float t)
|
||||
=> new(
|
||||
line.From.X + (line.To.X - line.From.X) * t,
|
||||
line.From.Y + (line.To.Y - line.From.Y) * t
|
||||
);
|
||||
public static Vector2D Lerp(Line2D line, float t)
|
||||
=> Vector2D.Lerp(line.From, line.To, t);
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the closest point on the <see cref="Line"/> segment to the specified point.
|
||||
/// Calculates the closest point on the <see cref="Line2D"/> segment to the specified point.
|
||||
/// </summary>
|
||||
public static Vector2D ClosestPointTo(Line line, Vector2D point)
|
||||
public static Vector2D ClosestPointTo(Line2D line, Vector2D point)
|
||||
{
|
||||
Vector2D edgeVector = new(line.To.X - line.From.X, line.To.Y - line.From.Y);
|
||||
Vector2D pointVector = new(point.X - line.From.X, point.Y - line.From.Y);
|
||||
Vector2D edgeVector = line.From.FromTo(line.To);
|
||||
Vector2D pointVector = point - line.From;
|
||||
|
||||
float t = (pointVector.X * edgeVector.X + pointVector.Y * edgeVector.Y) / (edgeVector.X * edgeVector.X + edgeVector.Y * edgeVector.Y);
|
||||
|
||||
@ -187,49 +184,49 @@ public readonly struct Line(Vector2D from, Vector2D to)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if two <see cref="Line"/> segments are approximately equal.
|
||||
/// Checks if two <see cref="Line2D"/> segments are approximately equal.
|
||||
/// </summary>
|
||||
public static bool ApproximatelyEquals(Line left, Line right)
|
||||
public static bool ApproximatelyEquals(Line2D left, Line2D right)
|
||||
=> left.From.ApproximatelyEquals(right.From) && left.To.ApproximatelyEquals(right.To);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides extension methods for the Line struct.
|
||||
/// </summary>
|
||||
public static class LineExtensions
|
||||
public static class Line2DExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Linearly interpolates between the two endpoints of the <see cref="Line"/> segment using parameter 't'.
|
||||
/// Linearly interpolates between the two endpoints of the <see cref="Line2D"/> segment using parameter 't'.
|
||||
/// </summary>
|
||||
public static Vector2D Lerp(this Line line, float t) => Line.Lerp(line, t);
|
||||
public static Vector2D Lerp(this Line2D line, float t) => Line2D.Lerp(line, t);
|
||||
|
||||
/// <summary>
|
||||
/// The equation of the <see cref="Line"/> defined by this <see cref="Line"/> segment.
|
||||
/// The equation of the <see cref="Line2D"/> defined by this <see cref="Line2D"/> segment.
|
||||
/// </summary>
|
||||
public static LineEquation ToLineEquation(this Line line) => Line.GetLineEquation(line);
|
||||
public static Line2DEquation ToLineEquation(this Line2D line) => Line2D.GetLineEquation(line);
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the specified <see cref="Vector2D"/> lies on the <see cref="Line"/>.
|
||||
/// Determines whether the specified <see cref="Vector2D"/> lies on the <see cref="Line2D"/>.
|
||||
/// </summary>
|
||||
public static bool Intersects(this Line line, Vector2D point) => Line.Intersects(line, point);
|
||||
public static bool Intersects(this Line2D line, Vector2D point) => Line2D.Intersects(line, point);
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the parameter 't' representing the point's position on the <see cref="Line"/> segment.
|
||||
/// Calculates the parameter 't' representing the point's position on the <see cref="Line2D"/> segment.
|
||||
/// </summary>
|
||||
public static float GetT(this Line line, Vector2D point) => Line.GetT(line, point);
|
||||
public static float GetT(this Line2D line, Vector2D point) => Line2D.GetT(line, point);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the <see cref="Line"/> segment intersects with another <see cref="Line"/> segment.
|
||||
/// Checks if the <see cref="Line2D"/> segment intersects with another <see cref="Line2D"/> segment.
|
||||
/// </summary>
|
||||
public static bool Intersects(this Line left, Line right) => Line.Intersects(left, right);
|
||||
public static bool Intersects(this Line2D left, Line2D right) => Line2D.Intersects(left, right);
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether two <see cref="Line"/> segments intersect.
|
||||
/// Determines whether two <see cref="Line2D"/> segments intersect.
|
||||
/// </summary>
|
||||
public static bool Intersects(this Line left, Line right, [NotNullWhen(returnValue: true)] out Vector2D? point) => Line.Intersects(left, right, out point);
|
||||
public static bool Intersects(this Line2D left, Line2D right, [NotNullWhen(returnValue: true)] out Vector2D? point) => Line2D.Intersects(left, right, out point);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if two <see cref="Line"/>s are approximately equal.
|
||||
/// Checks if two <see cref="Line2D"/>s are approximately equal.
|
||||
/// </summary>
|
||||
public static bool ApproximatelyEquals(this Line left, Line right) => Line.ApproximatelyEquals(left, right);
|
||||
public static bool ApproximatelyEquals(this Line2D left, Line2D right) => Line2D.ApproximatelyEquals(left, right);
|
||||
}
|
@ -8,10 +8,10 @@ namespace Syntriax.Engine.Physics2D.Primitives;
|
||||
/// <param name="slope">The slope of the line.</param>
|
||||
/// <param name="offsetY">The y-intercept of the line.</param>
|
||||
/// <remarks>
|
||||
/// Initializes a new instance of the <see cref="LineEquation"/> struct with the specified slope and y-intercept.
|
||||
/// Initializes a new instance of the <see cref="Line2DEquation"/> struct with the specified slope and y-intercept.
|
||||
/// </remarks>
|
||||
[System.Diagnostics.DebuggerDisplay("y = {Slope}x + {OffsetY}")]
|
||||
public readonly struct LineEquation(float slope, float offsetY)
|
||||
public readonly struct Line2DEquation(float slope, float offsetY)
|
||||
{
|
||||
/// <summary>
|
||||
/// The slope of the line equation.
|
||||
@ -29,7 +29,7 @@ public readonly struct LineEquation(float slope, float offsetY)
|
||||
/// <param name="lineEquation">The line equation to resolve.</param>
|
||||
/// <param name="x">The x-coordinate for which to resolve the y-coordinate.</param>
|
||||
/// <returns>The y-coordinate resolved using the line equation.</returns>
|
||||
public static float Resolve(LineEquation lineEquation, float x) => lineEquation.Slope * x + lineEquation.OffsetY; // y = mx + b
|
||||
public static float Resolve(Line2DEquation lineEquation, float x) => lineEquation.Slope * x + lineEquation.OffsetY; // y = mx + b
|
||||
|
||||
/// <summary>
|
||||
/// Checks if two line equations are approximately equal.
|
||||
@ -37,14 +37,14 @@ public readonly struct LineEquation(float slope, float offsetY)
|
||||
/// <param name="left">The first line equation to compare.</param>
|
||||
/// <param name="right">The second line equation to compare.</param>
|
||||
/// <returns>True if the line equations are approximately equal; otherwise, false.</returns>
|
||||
public static bool ApproximatelyEquals(LineEquation left, LineEquation right)
|
||||
public static bool ApproximatelyEquals(Line2DEquation left, Line2DEquation right)
|
||||
=> left.Slope.ApproximatelyEquals(right.Slope) && left.OffsetY.ApproximatelyEquals(right.OffsetY);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides extension methods for the LineEquation struct.
|
||||
/// </summary>
|
||||
public static class LineEquationExtensions
|
||||
public static class Line2DEquationExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Resolves the y-coordinate for a given x-coordinate using the line equation.
|
||||
@ -52,7 +52,7 @@ public static class LineEquationExtensions
|
||||
/// <param name="lineEquation">The line equation to resolve.</param>
|
||||
/// <param name="x">The x-coordinate for which to resolve the y-coordinate.</param>
|
||||
/// <returns>The y-coordinate resolved using the line equation.</returns>
|
||||
public static float Resolve(this LineEquation lineEquation, float x) => LineEquation.Resolve(lineEquation, x);
|
||||
public static float Resolve(this Line2DEquation lineEquation, float x) => Line2DEquation.Resolve(lineEquation, x);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if two line equations are approximately equal.
|
||||
@ -60,5 +60,5 @@ public static class LineEquationExtensions
|
||||
/// <param name="left">The first line equation to compare.</param>
|
||||
/// <param name="right">The second line equation to compare.</param>
|
||||
/// <returns>True if the line equations are approximately equal; otherwise, false.</returns>
|
||||
public static bool ApproximatelyEquals(this LineEquation left, LineEquation right) => LineEquation.ApproximatelyEquals(left, right);
|
||||
public static bool ApproximatelyEquals(this Line2DEquation left, Line2DEquation right) => Line2DEquation.ApproximatelyEquals(left, right);
|
||||
}
|
@ -6,10 +6,10 @@ namespace Syntriax.Engine.Physics2D.Primitives;
|
||||
/// <param name="min">The minimum value of the projection.</param>
|
||||
/// <param name="max">The maximum value of the projection.</param>
|
||||
/// <remarks>
|
||||
/// Initializes a new instance of the <see cref="Projection"/> struct with the specified minimum and maximum values.
|
||||
/// Initializes a new instance of the <see cref="Projection1D"/> struct with the specified minimum and maximum values.
|
||||
/// </remarks>
|
||||
[System.Diagnostics.DebuggerDisplay("Min: {Min}, Max: {Max}")]
|
||||
public readonly struct Projection(float min, float max)
|
||||
public readonly struct Projection1D(float min, float max)
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the minimum value of the projection.
|
||||
@ -27,7 +27,7 @@ public readonly struct Projection(float min, float max)
|
||||
/// <param name="left">The first projection to check.</param>
|
||||
/// <param name="right">The second projection to check.</param>
|
||||
/// <returns><see cref="true"/> if the projections overlap; otherwise, <see cref="false"/>.</returns>
|
||||
public static bool Overlaps(Projection left, Projection right) => Overlaps(left, right, out float _);
|
||||
public static bool Overlaps(Projection1D left, Projection1D right) => Overlaps(left, right, out float _);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if two projections overlap and calculates the depth of the overlap.
|
||||
@ -36,7 +36,7 @@ public readonly struct Projection(float min, float max)
|
||||
/// <param name="right">The second projection to check.</param>
|
||||
/// <param name="depth">The depth of the overlap, if any.</param>
|
||||
/// <returns><see cref="true"/> if the projections overlap; otherwise, <see cref="false"/>.</returns>
|
||||
public static bool Overlaps(Projection left, Projection right, out float depth)
|
||||
public static bool Overlaps(Projection1D left, Projection1D right, out float depth)
|
||||
{
|
||||
// TODO Try to improve this
|
||||
bool rightMinInLeft = right.Min > left.Min && right.Min < left.Max;
|
||||
@ -73,9 +73,9 @@ public readonly struct Projection(float min, float max)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides extension methods for the <see cref="Projection"/> struct.
|
||||
/// Provides extension methods for the <see cref="Projection1D"/> struct.
|
||||
/// </summary>
|
||||
public static class ProjectionExtensions
|
||||
public static class Projection1DExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Checks if two projections overlap.
|
||||
@ -83,7 +83,7 @@ public static class ProjectionExtensions
|
||||
/// <param name="left">The first projection to check.</param>
|
||||
/// <param name="right">The second projection to check.</param>
|
||||
/// <returns><see cref="true"/> if the projections overlap; otherwise, <see cref="false"/>.</returns>
|
||||
public static bool Overlaps(this Projection left, Projection right) => Projection.Overlaps(left, right);
|
||||
public static bool Overlaps(this Projection1D left, Projection1D right) => Projection1D.Overlaps(left, right);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if two projections overlap and calculates the depth of the overlap.
|
||||
@ -92,5 +92,5 @@ public static class ProjectionExtensions
|
||||
/// <param name="right">The second projection to check.</param>
|
||||
/// <param name="depth">The depth of the overlap, if any.</param>
|
||||
/// <returns><see cref="true"/> if the projections overlap; otherwise, <see cref="false"/>.</returns>
|
||||
public static bool Overlaps(this Projection left, Projection right, out float depth) => Projection.Overlaps(left, right, out depth);
|
||||
public static bool Overlaps(this Projection1D left, Projection1D right, out float depth) => Projection1D.Overlaps(left, right, out depth);
|
||||
}
|
@ -11,15 +11,15 @@ namespace Syntriax.Engine.Physics2D.Primitives;
|
||||
/// </summary>
|
||||
/// <param name="vertices">The vertices of the shape.</param>
|
||||
/// <remarks>
|
||||
/// Initializes a new instance of the <see cref="Shape"/> struct with the specified vertices.
|
||||
/// Initializes a new instance of the <see cref="Shape2D"/> struct with the specified vertices.
|
||||
/// </remarks>
|
||||
[System.Diagnostics.DebuggerDisplay("Vertices Count: {Vertices.Count}")]
|
||||
public readonly struct Shape(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
public readonly struct Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
{
|
||||
public static readonly Shape Triangle = CreateNgon(3, Vector2D.Up);
|
||||
public static readonly Shape Box = CreateNgon(4, Vector2D.One);
|
||||
public static readonly Shape Pentagon = CreateNgon(5, Vector2D.Up);
|
||||
public static readonly Shape Hexagon = CreateNgon(6, Vector2D.Right);
|
||||
public static readonly Shape2D Triangle = CreateNgon(3, Vector2D.Up);
|
||||
public static readonly Shape2D Box = CreateNgon(4, Vector2D.One);
|
||||
public static readonly Shape2D Pentagon = CreateNgon(5, Vector2D.Up);
|
||||
public static readonly Shape2D Hexagon = CreateNgon(6, Vector2D.Right);
|
||||
|
||||
private readonly List<Vector2D> _verticesList = vertices;
|
||||
|
||||
@ -40,14 +40,14 @@ public readonly struct Shape(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
/// </summary>
|
||||
/// <param name="shape">The shape to copy.</param>
|
||||
/// <returns>A copy of the input shape.</returns>
|
||||
public static Shape CreateCopy(Shape shape) => new(new List<Vector2D>(shape.Vertices));
|
||||
public static Shape2D CreateCopy(Shape2D shape) => new(new List<Vector2D>(shape.Vertices));
|
||||
|
||||
/// <summary>
|
||||
/// Creates a regular polygon (ngon) with the specified number of vertices.
|
||||
/// </summary>
|
||||
/// <param name="vertexCount">The number of vertices in the polygon.</param>
|
||||
/// <returns>A regular polygon with the specified number of vertices.</returns>
|
||||
public static Shape CreateNgon(int vertexCount) => CreateNgon(vertexCount, Vector2D.Up);
|
||||
public static Shape2D CreateNgon(int vertexCount) => CreateNgon(vertexCount, Vector2D.Up);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a regular polygon (ngon) with the specified number of vertices and a rotation position.
|
||||
@ -55,7 +55,7 @@ public readonly struct Shape(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
/// <param name="vertexCount">The number of vertices in the polygon.</param>
|
||||
/// <param name="positionToRotate">The position to use for rotation.</param>
|
||||
/// <returns>A regular polygon with the specified number of vertices and rotation position.</returns>
|
||||
public static Shape CreateNgon(int vertexCount, Vector2D positionToRotate)
|
||||
public static Shape2D CreateNgon(int vertexCount, Vector2D positionToRotate)
|
||||
{
|
||||
if (vertexCount < 3)
|
||||
throw new System.ArgumentException($"{nameof(vertexCount)} must have a value of more than 2.");
|
||||
@ -75,7 +75,7 @@ public readonly struct Shape(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
/// </summary>
|
||||
/// <param name="shape">The shape to enclose.</param>
|
||||
/// <returns>The super triangle that encloses the given shape.</returns>
|
||||
public static Triangle GetSuperTriangle(Shape shape)
|
||||
public static Triangle GetSuperTriangle(Shape2D shape)
|
||||
{
|
||||
float minX = float.MaxValue, minY = float.MaxValue;
|
||||
float maxX = float.MinValue, maxY = float.MinValue;
|
||||
@ -106,7 +106,7 @@ public readonly struct Shape(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
/// </summary>
|
||||
/// <param name="shape">The shape to get lines from.</param>
|
||||
/// <param name="lines">The list to populate with lines.</param>
|
||||
public static void GetLines(Shape shape, IList<Line> lines)
|
||||
public static void GetLines(Shape2D shape, IList<Line2D> lines)
|
||||
{
|
||||
lines.Clear();
|
||||
for (int i = 0; i < shape.Vertices.Count - 1; i++)
|
||||
@ -119,9 +119,9 @@ public readonly struct Shape(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
/// </summary>
|
||||
/// <param name="shape">The shape to get lines from.</param>
|
||||
/// <returns>A list of lines that form the edges of the shape.</returns>
|
||||
public static List<Line> GetLines(Shape shape)
|
||||
public static List<Line2D> GetLines(Shape2D shape)
|
||||
{
|
||||
List<Line> lines = new(shape.Vertices.Count - 1);
|
||||
List<Line2D> lines = new(shape.Vertices.Count - 1);
|
||||
GetLines(shape, lines);
|
||||
return lines;
|
||||
}
|
||||
@ -132,7 +132,7 @@ public readonly struct Shape(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
/// <param name="shape">The shape to project.</param>
|
||||
/// <param name="projectionVector">The vector to project onto.</param>
|
||||
/// <param name="list">The list to populate with projected values.</param>
|
||||
public static void Project(Shape shape, Vector2D projectionVector, IList<float> list)
|
||||
public static void Project(Shape2D shape, Vector2D projectionVector, IList<float> list)
|
||||
{
|
||||
list.Clear();
|
||||
|
||||
@ -147,7 +147,7 @@ public readonly struct Shape(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
/// <param name="shape">The shape to project.</param>
|
||||
/// <param name="projectionVector">The vector to project onto.</param>
|
||||
/// <returns>The projection of the shape onto the vector.</returns>
|
||||
public static Projection Project(Shape shape, Vector2D projectionVector)
|
||||
public static Projection1D Project(Shape2D shape, Vector2D projectionVector)
|
||||
{
|
||||
float min = float.MaxValue;
|
||||
float max = float.MinValue;
|
||||
@ -168,7 +168,7 @@ public readonly struct Shape(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
/// <param name="shape">The shape to transform.</param>
|
||||
/// <param name="transform">The transform to apply.</param>
|
||||
/// <returns>The transformed shape.</returns>
|
||||
public static Shape TransformShape(Shape shape, ITransform transform)
|
||||
public static Shape2D TransformShape(Shape2D shape, ITransform transform)
|
||||
{
|
||||
List<Vector2D> vertices = new(shape.Vertices.Count);
|
||||
|
||||
@ -176,7 +176,7 @@ public readonly struct Shape(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
for (int i = 0; i < count; i++)
|
||||
vertices.Add(transform.TransformVector2D(shape[i]));
|
||||
|
||||
return new Shape(vertices);
|
||||
return new Shape2D(vertices);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -185,7 +185,7 @@ public readonly struct Shape(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
/// <param name="from">The shape to transform.</param>
|
||||
/// <param name="transform">The transform to apply.</param>
|
||||
/// <param name="to">The transformed shape.</param>
|
||||
public static void TransformShape(Shape from, ITransform transform, ref Shape to)
|
||||
public static void TransformShape(Shape2D from, ITransform transform, ref Shape2D to)
|
||||
{
|
||||
to._verticesList.Clear();
|
||||
|
||||
@ -200,7 +200,7 @@ public readonly struct Shape(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
/// <param name="left">The first shape to compare.</param>
|
||||
/// <param name="right">The second shape to compare.</param>
|
||||
/// <returns><c>true</c> if the shapes are approximately equal; otherwise, <c>false</c>.</returns>
|
||||
public static bool ApproximatelyEquals(Shape left, Shape right)
|
||||
public static bool ApproximatelyEquals(Shape2D left, Shape2D right)
|
||||
{
|
||||
if (left.Vertices.Count != right.Vertices.Count)
|
||||
return false;
|
||||
@ -220,37 +220,37 @@ public readonly struct Shape(List<Vector2D> vertices) : IEnumerable<Vector2D>
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides extension methods for the <see cref="Shape"/> struct.
|
||||
/// Provides extension methods for the <see cref="Shape2D"/> struct.
|
||||
/// </summary>
|
||||
public static class ShapeExtensions
|
||||
public static class Shape2DExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a copy of the shape.
|
||||
/// </summary>
|
||||
/// <param name="shape">The shape to copy.</param>
|
||||
/// <returns>A copy of the input shape.</returns>
|
||||
public static Shape CreateCopy(this Shape shape) => Shape.CreateCopy(shape);
|
||||
public static Shape2D CreateCopy(this Shape2D shape) => Shape2D.CreateCopy(shape);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the super triangle that encloses the shape.
|
||||
/// </summary>
|
||||
/// <param name="shape">The shape to enclose.</param>
|
||||
/// <returns>The super triangle that encloses the shape.</returns>
|
||||
public static Triangle ToSuperTriangle(this Shape shape) => Shape.GetSuperTriangle(shape);
|
||||
public static Triangle ToSuperTriangle(this Shape2D shape) => Shape2D.GetSuperTriangle(shape);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the lines that form the edges of the shape.
|
||||
/// </summary>
|
||||
/// <param name="shape">The shape to get lines from.</param>
|
||||
/// <param name="lines">The list to populate with lines.</param>
|
||||
public static void ToLines(this Shape shape, IList<Line> lines) => Shape.GetLines(shape, lines);
|
||||
public static void ToLines(this Shape2D shape, IList<Line2D> lines) => Shape2D.GetLines(shape, lines);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of lines that form the edges of the shape.
|
||||
/// </summary>
|
||||
/// <param name="shape">The shape to get lines from.</param>
|
||||
/// <returns>A list of lines that form the edges of the shape.</returns>
|
||||
public static List<Line> ToLines(this Shape shape) => Shape.GetLines(shape);
|
||||
public static List<Line2D> ToLines(this Shape2D shape) => Shape2D.GetLines(shape);
|
||||
|
||||
/// <summary>
|
||||
/// Projects the shape onto a vector.
|
||||
@ -258,7 +258,7 @@ public static class ShapeExtensions
|
||||
/// <param name="shape">The shape to project.</param>
|
||||
/// <param name="projectionVector">The vector to project onto.</param>
|
||||
/// <param name="list">The list to populate with projected values.</param>
|
||||
public static void ToProjection(this Shape shape, Vector2D projectionVector, IList<float> list) => Shape.Project(shape, projectionVector, list);
|
||||
public static void ToProjection(this Shape2D shape, Vector2D projectionVector, IList<float> list) => Shape2D.Project(shape, projectionVector, list);
|
||||
|
||||
/// <summary>
|
||||
/// Projects the shape onto a vector.
|
||||
@ -266,7 +266,7 @@ public static class ShapeExtensions
|
||||
/// <param name="shape">The shape to project.</param>
|
||||
/// <param name="projectionVector">The vector to project onto.</param>
|
||||
/// <returns>The projection of the shape onto the vector.</returns>
|
||||
public static Projection ToProjection(this Shape shape, Vector2D projectionVector) => Shape.Project(shape, projectionVector);
|
||||
public static Projection1D ToProjection(this Shape2D shape, Vector2D projectionVector) => Shape2D.Project(shape, projectionVector);
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the shape using the specified transform.
|
||||
@ -274,7 +274,7 @@ public static class ShapeExtensions
|
||||
/// <param name="transform">The transform to apply.</param>
|
||||
/// <param name="shape">The shape to transform.</param>
|
||||
/// <returns>The transformed shape.</returns>
|
||||
public static Shape TransformShape(this ITransform transform, Shape shape) => Shape.TransformShape(shape, transform);
|
||||
public static Shape2D TransformShape(this ITransform transform, Shape2D shape) => Shape2D.TransformShape(shape, transform);
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the shape using the specified transform.
|
||||
@ -282,7 +282,7 @@ public static class ShapeExtensions
|
||||
/// <param name="transform">The transform to apply.</param>
|
||||
/// <param name="from">The shape to transform.</param>
|
||||
/// <param name="to">The transformed shape.</param>
|
||||
public static void TransformShape(this ITransform transform, Shape from, ref Shape to) => Shape.TransformShape(from, transform, ref to);
|
||||
public static void TransformShape(this ITransform transform, Shape2D from, ref Shape2D to) => Shape2D.TransformShape(from, transform, ref to);
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether two shapes are approximately equal.
|
||||
@ -290,5 +290,5 @@ public static class ShapeExtensions
|
||||
/// <param name="left">The first shape to compare.</param>
|
||||
/// <param name="right">The second shape to compare.</param>
|
||||
/// <returns><c>true</c> if the shapes are approximately equal; otherwise, <c>false</c>.</returns>
|
||||
public static bool ApproximatelyEquals(this Shape left, Shape right) => Shape.ApproximatelyEquals(left, right);
|
||||
public static bool ApproximatelyEquals(this Shape2D left, Shape2D right) => Shape2D.ApproximatelyEquals(left, right);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user