Engine DeMonoGame #2

This commit is contained in:
2024-01-23 12:16:58 +03:00
parent 0486b0343a
commit ba284a199a
17 changed files with 135 additions and 124 deletions

View File

@@ -1,10 +1,10 @@
using Microsoft.Xna.Framework;
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D.Primitives;
public record AABB(Vector2 LowerBoundary, Vector2 UpperBoundary)
public record AABB(Vector2D LowerBoundary, Vector2D UpperBoundary)
{
public bool Overlaps(Vector2 point)
public bool Overlaps(Vector2D point)
=> point.X >= LowerBoundary.X && point.X <= UpperBoundary.X &&
point.Y >= LowerBoundary.Y && point.Y <= UpperBoundary.Y;

View File

@@ -1,8 +1,8 @@
using Microsoft.Xna.Framework;
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D.Primitives;
public record Circle(Vector2 Position, float Radius)
public record Circle(Vector2D Position, float Radius)
{
public bool Intersects(Circle other)
{
@@ -12,7 +12,7 @@ public record Circle(Vector2 Position, float Radius)
return distanceSquared < radiusSumSquared;
}
public bool Overlaps(Vector2 point) => (Position - point).LengthSquared() <= Radius * Radius;
public bool Overlaps(Vector2D point) => (Position - point).LengthSquared() <= Radius * Radius;
public bool ApproximatelyEquals(Circle other)
=> Position.ApproximatelyEquals(other.Position) && Radius.ApproximatelyEquals(other.Radius);
}

View File

@@ -2,14 +2,14 @@ using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Xna.Framework;
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D.Primitives;
public record Line(Vector2 From, Vector2 To)
public record Line(Vector2D From, Vector2D To)
{
public Line Reversed => new(To, From);
public Vector2 Direction => Vector2.Normalize(To - From);
public Vector2D Direction => Vector2D.Normalize(To - From);
public float Length => (From - To).Length();
public float LengthSquared => (From - To).LengthSquared();
@@ -17,7 +17,7 @@ public record Line(Vector2 From, Vector2 To)
{
get
{
Vector2 slopeVector = To - From;
Vector2D slopeVector = To - From;
float slope = slopeVector.Y / slopeVector.X;
float yOffset = From.Y - (slope * From.X);
@@ -26,10 +26,10 @@ public record Line(Vector2 From, Vector2 To)
}
}
public bool Intersects(Vector2 point)
public bool Intersects(Vector2D point)
=> Resolve(point.X).ApproximatelyEquals(point);
public float GetT(Vector2 point)
public float GetT(Vector2D point)
{
float fromX = MathF.Abs(From.X);
float toX = MathF.Abs(To.X);
@@ -51,18 +51,18 @@ public record Line(Vector2 From, Vector2 To)
return t;
}
public bool Exist(List<Vector2> vertices)
public bool Exist(List<Vector2D> vertices)
{
for (int i = 0; i < vertices.Count - 1; i++)
{
Vector2 vertexCurrent = vertices[i];
Vector2 vertexNext = vertices[i];
Vector2D vertexCurrent = vertices[i];
Vector2D vertexNext = vertices[i];
if (From == vertexCurrent && To == vertexNext) return true;
if (From == vertexNext && To == vertexCurrent) return true;
}
Vector2 vertexFirst = vertices[0];
Vector2 vertexLast = vertices[^1];
Vector2D vertexFirst = vertices[0];
Vector2D vertexLast = vertices[^1];
if (From == vertexFirst && To == vertexLast) return true;
if (From == vertexLast && To == vertexFirst) return true;
return false;
@@ -80,20 +80,20 @@ public record Line(Vector2 From, Vector2 To)
return numerator / denominator;
}
public Vector2 Lerp(float t)
=> new Vector2(
public Vector2D Lerp(float t)
=> new Vector2D(
From.X + (To.X - From.X) * t,
From.Y + (To.Y - From.Y) * t
);
public Vector2 Resolve(float x)
=> new Vector2(x, LineEquation.Resolve(x));
public Vector2D Resolve(float x)
=> new Vector2D(x, LineEquation.Resolve(x));
public Vector2 ClosestPointTo(Vector2 point)
public Vector2D ClosestPointTo(Vector2D point)
{
// Convert edge points to vectors
var edgeVector = new Vector2(To.X - From.X, To.Y - From.Y);
var pointVector = new Vector2(point.X - From.X, point.Y - From.Y);
var edgeVector = new Vector2D(To.X - From.X, To.Y - From.Y);
var pointVector = new Vector2D(point.X - From.X, point.Y - From.Y);
// Calculate the projection of pointVector onto edgeVector
float t = (pointVector.X * edgeVector.X + pointVector.Y * edgeVector.Y) / (edgeVector.X * edgeVector.X + edgeVector.Y * edgeVector.Y);
@@ -105,11 +105,11 @@ public record Line(Vector2 From, Vector2 To)
float closestX = From.X + t * edgeVector.X;
float closestY = From.Y + t * edgeVector.Y;
return new Vector2((float)closestX, (float)closestY);
return new Vector2D((float)closestX, (float)closestY);
}
public Vector2 IntersectionPoint(Line other)
=> Vector2.Lerp(From, To, IntersectionParameterT(other));
public Vector2D IntersectionPoint(Line other)
=> Vector2D.Lerp(From, To, IntersectionParameterT(other));
public bool Intersects(Line other)
{
@@ -129,7 +129,7 @@ public record Line(Vector2 From, Vector2 To)
return false;
}
public bool Intersects(Line other, [NotNullWhen(returnValue: true)] out Vector2? point)
public bool Intersects(Line other, [NotNullWhen(returnValue: true)] out Vector2D? point)
{
point = null;

View File

@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D.Primitives;
public record Shape(IList<Vector2> Vertices)
public record Shape(IList<Vector2D> Vertices)
{
public Triangle SuperTriangle
{
@@ -14,7 +14,7 @@ public record Shape(IList<Vector2> Vertices)
float minX = float.MaxValue, minY = float.MaxValue;
float maxX = float.MinValue, maxY = float.MinValue;
foreach (Vector2 point in Vertices)
foreach (Vector2D point in Vertices)
{
minX = MathF.Min(minX, point.X);
minY = MathF.Min(minY, point.Y);
@@ -28,9 +28,9 @@ public record Shape(IList<Vector2> Vertices)
float midX = (minX + maxX) / 2;
float midY = (minY + maxY) / 2;
Vector2 p1 = new Vector2((float)midX - 20f * (float)deltaMax, (float)midY - (float)deltaMax);
Vector2 p2 = new Vector2((float)midX, (float)midY + 20 * (float)deltaMax);
Vector2 p3 = new Vector2((float)midX + 20 * (float)deltaMax, (float)midY - (float)deltaMax);
Vector2D p1 = new Vector2D((float)midX - 20f * (float)deltaMax, (float)midY - (float)deltaMax);
Vector2D p2 = new Vector2D((float)midX, (float)midY + 20 * (float)deltaMax);
Vector2D p3 = new Vector2D((float)midX + 20 * (float)deltaMax, (float)midY - (float)deltaMax);
return new Triangle(p1, p2, p3);
}

View File

@@ -1,10 +1,10 @@
using System;
using Microsoft.Xna.Framework;
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Physics2D.Primitives;
public record Triangle(Vector2 A, Vector2 B, Vector2 C)
public record Triangle(Vector2D A, Vector2D B, Vector2D C)
{
public float Area => MathF.Abs((
A.X * (B.Y - C.Y) +
@@ -16,27 +16,27 @@ public record Triangle(Vector2 A, Vector2 B, Vector2 C)
{
get
{
Vector2 midAB = (A + B) / 2;
Vector2 midBC = (B + C) / 2;
Vector2D midAB = (A + B) / 2;
Vector2D midBC = (B + C) / 2;
float slopeAB = (B.Y - A.Y) / (B.X - A.X);
float slopeBC = (C.Y - B.Y) / (C.X - B.X);
Vector2 center;
Vector2D center;
if (MathF.Abs(slopeAB - slopeBC) > float.Epsilon)
{
float x = (slopeAB * slopeBC * (A.Y - C.Y) + slopeBC * (A.X + B.X) - slopeAB * (B.X + C.X)) / (2 * (slopeBC - slopeAB));
float y = -(x - (A.X + B.X) / 2) / slopeAB + (A.Y + B.Y) / 2;
center = new Vector2(x, y);
center = new Vector2D(x, y);
}
else
center = (midAB + midBC) * .5f;
return new(center, Vector2.Distance(center, A));
return new(center, Vector2D.Distance(center, A));
}
}
public bool Overlaps(Vector2 point)
public bool Overlaps(Vector2D point)
{
float originalTriangleArea = Area;