chore!: renamed AABB to AABB2D

This commit is contained in:
2025-10-19 18:33:01 +03:00
parent 61ff0887e2
commit 1d63391975
11 changed files with 165 additions and 165 deletions

View File

@@ -1,111 +0,0 @@
using System;
using System.Collections.Generic;
namespace Engine.Core;
/// <summary>
/// Represents an Axis-Aligned Bounding Box (AABB) in 2D space.
/// </summary>
/// <param name="lowerBoundary">The lower boundary of the <see cref="AABB"/>.</param>
/// <param name="upperBoundary">The upper boundary of the <see cref="AABB"/>.</param>
/// <remarks>
/// Initializes a new instance of the <see cref="AABB"/> struct with the specified lower and upper boundaries.
/// </remarks>
[System.Diagnostics.DebuggerDisplay("LowerBoundary: {LowerBoundary.ToString(), nq}, UpperBoundary: {UpperBoundary.ToString(), nq}")]
public readonly struct AABB(Vector2D lowerBoundary, Vector2D upperBoundary) : IEquatable<AABB>
{
/// <summary>
/// The lower boundary of the <see cref="AABB"/>.
/// </summary>
public readonly Vector2D LowerBoundary = lowerBoundary;
/// <summary>
/// The upper boundary of the <see cref="AABB"/>.
/// </summary>
public readonly Vector2D UpperBoundary = upperBoundary;
/// <summary>
/// Gets the center point of the <see cref="AABB"/>.
/// </summary>
public readonly Vector2D Center => (LowerBoundary + UpperBoundary) * .5f;
/// <summary>
/// Gets the size of the <see cref="AABB"/>.
/// </summary>
public readonly Vector2D Size => LowerBoundary.FromTo(UpperBoundary).Abs();
/// <summary>
/// Gets half the size of the <see cref="AABB"/>.
/// </summary>
public readonly Vector2D SizeHalf => Size * .5f;
public static bool operator ==(AABB left, AABB right) => left.UpperBoundary == right.UpperBoundary && left.LowerBoundary == right.LowerBoundary;
public static bool operator !=(AABB left, AABB right) => left.UpperBoundary != right.UpperBoundary || left.LowerBoundary != right.LowerBoundary;
/// <summary>
/// Creates an <see cref="AABB"/> from a collection of <see cref="Vector2D"/>s.
/// </summary>
/// <param name="vectors">The collection of <see cref="Vector2D"/>s.</param>
/// <returns>An <see cref="AABB"/> that bounds all the <see cref="Vector2D"/>s.</returns>
public static AABB FromVectors(IEnumerable<Vector2D> vectors)
{
int counter = 0;
Vector2D lowerBoundary = new(float.MaxValue, float.MaxValue);
Vector2D upperBoundary = new(float.MinValue, float.MinValue);
foreach (Vector2D vector in vectors)
{
lowerBoundary = Vector2D.Min(lowerBoundary, vector);
upperBoundary = Vector2D.Max(upperBoundary, vector);
counter++;
}
if (counter < 2)
throw new System.ArgumentException($"Parameter {nameof(vectors)} must have at least 2 items.");
return new(lowerBoundary, upperBoundary);
}
/// <summary>
/// Checks if two <see cref="AABB"/>s are approximately equal.
/// </summary>
/// <param name="left">The first <see cref="AABB"/>.</param>
/// <param name="right">The second <see cref="AABB"/>.</param>
/// <param name="epsilon">The epsilon range.</param>
/// <returns><see cref="true"/> if the <see cref="AABB"/>s are approximately equal; otherwise, <see cref="false"/>.</returns>
public static bool ApproximatelyEquals(AABB left, AABB right, float epsilon = float.Epsilon)
=> left.LowerBoundary.ApproximatelyEquals(right.LowerBoundary, epsilon) && left.UpperBoundary.ApproximatelyEquals(right.UpperBoundary, epsilon);
/// <summary>
/// Determines whether the specified object is equal to the current <see cref="AABB"/>.
/// </summary>
/// <param name="obj">The object to compare with the current <see cref="AABB"/>.</param>
/// <returns><see cref="true"/> if the specified object is equal to the current <see cref="AABB"/>; otherwise, <see cref="false"/>.</returns>
public override bool Equals(object? obj) => obj is AABB aabb && this == aabb;
public bool Equals(AABB other) => this == other;
/// <summary>
/// Generates a hash code for the <see cref="AABB"/>.
/// </summary>
/// <returns>A hash code for the <see cref="AABB"/>.</returns>
public override int GetHashCode() => System.HashCode.Combine(LowerBoundary, UpperBoundary);
/// <summary>
/// Converts the <see cref="AABB"/> to its string representation.
/// </summary>
/// <returns>A string representation of the <see cref="AABB"/>.</returns>
public override string ToString() => $"{nameof(AABB)}({LowerBoundary}, {UpperBoundary})";
}
/// <summary>
/// Provides extension methods for the <see cref="AABB"/> struct.
/// </summary>
public static class AABBExtensions
{
/// <inheritdoc cref="AABB.ToAABB" />
public static AABB ToAABB(this IEnumerable<Vector2D> vectors) => AABB.FromVectors(vectors);
/// <inheritdoc cref="AABB.ApproximatelyEquals" />
public static bool ApproximatelyEquals(this AABB left, AABB right, float epsilon = float.Epsilon) => AABB.ApproximatelyEquals(left, right, epsilon);
}

View File

@@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
namespace Engine.Core;
/// <summary>
/// Represents an Axis-Aligned Bounding Box (AABB) in 2D space.
/// </summary>
/// <param name="lowerBoundary">The lower boundary of the <see cref="AABB2D"/>.</param>
/// <param name="upperBoundary">The upper boundary of the <see cref="AABB2D"/>.</param>
/// <remarks>
/// Initializes a new instance of the <see cref="AABB2D"/> struct with the specified lower and upper boundaries.
/// </remarks>
[System.Diagnostics.DebuggerDisplay("LowerBoundary: {LowerBoundary.ToString(), nq}, UpperBoundary: {UpperBoundary.ToString(), nq}")]
public readonly struct AABB2D(Vector2D lowerBoundary, Vector2D upperBoundary) : IEquatable<AABB2D>
{
/// <summary>
/// The lower boundary of the <see cref="AABB2D"/>.
/// </summary>
public readonly Vector2D LowerBoundary = lowerBoundary;
/// <summary>
/// The upper boundary of the <see cref="AABB2D"/>.
/// </summary>
public readonly Vector2D UpperBoundary = upperBoundary;
/// <summary>
/// Gets the center point of the <see cref="AABB2D"/>.
/// </summary>
public readonly Vector2D Center => (LowerBoundary + UpperBoundary) * .5f;
/// <summary>
/// Gets the size of the <see cref="AABB2D"/>.
/// </summary>
public readonly Vector2D Size => LowerBoundary.FromTo(UpperBoundary).Abs();
/// <summary>
/// Gets half the size of the <see cref="AABB2D"/>.
/// </summary>
public readonly Vector2D SizeHalf => Size * .5f;
public static bool operator ==(AABB2D left, AABB2D right) => left.UpperBoundary == right.UpperBoundary && left.LowerBoundary == right.LowerBoundary;
public static bool operator !=(AABB2D left, AABB2D right) => left.UpperBoundary != right.UpperBoundary || left.LowerBoundary != right.LowerBoundary;
/// <summary>
/// Creates an <see cref="AABB2D"/> from a collection of <see cref="Vector2D"/>s.
/// </summary>
/// <param name="vectors">The collection of <see cref="Vector2D"/>s.</param>
/// <returns>An <see cref="AABB2D"/> that bounds all the <see cref="Vector2D"/>s.</returns>
public static AABB2D FromVectors(IEnumerable<Vector2D> vectors)
{
int counter = 0;
Vector2D lowerBoundary = new(float.MaxValue, float.MaxValue);
Vector2D upperBoundary = new(float.MinValue, float.MinValue);
foreach (Vector2D vector in vectors)
{
lowerBoundary = Vector2D.Min(lowerBoundary, vector);
upperBoundary = Vector2D.Max(upperBoundary, vector);
counter++;
}
if (counter < 2)
throw new ArgumentException($"Parameter {nameof(vectors)} must have at least 2 items.");
return new(lowerBoundary, upperBoundary);
}
/// <summary>
/// Checks if two <see cref="AABB2D"/>s are approximately equal.
/// </summary>
/// <param name="left">The first <see cref="AABB2D"/>.</param>
/// <param name="right">The second <see cref="AABB2D"/>.</param>
/// <param name="epsilon">The epsilon range.</param>
/// <returns><see cref="true"/> if the <see cref="AABB2D"/>s are approximately equal; otherwise, <see cref="false"/>.</returns>
public static bool ApproximatelyEquals(AABB2D left, AABB2D right, float epsilon = float.Epsilon)
=> left.LowerBoundary.ApproximatelyEquals(right.LowerBoundary, epsilon) && left.UpperBoundary.ApproximatelyEquals(right.UpperBoundary, epsilon);
/// <summary>
/// Determines whether the specified object is equal to the current <see cref="AABB2D"/>.
/// </summary>
/// <param name="obj">The object to compare with the current <see cref="AABB2D"/>.</param>
/// <returns><see cref="true"/> if the specified object is equal to the current <see cref="AABB2D"/>; otherwise, <see cref="false"/>.</returns>
public override bool Equals(object? obj) => obj is AABB2D aabb && this == aabb;
public bool Equals(AABB2D other) => this == other;
/// <summary>
/// Generates a hash code for the <see cref="AABB2D"/>.
/// </summary>
/// <returns>A hash code for the <see cref="AABB2D"/>.</returns>
public override int GetHashCode() => System.HashCode.Combine(LowerBoundary, UpperBoundary);
/// <summary>
/// Converts the <see cref="AABB2D"/> to its string representation.
/// </summary>
/// <returns>A string representation of the <see cref="AABB2D"/>.</returns>
public override string ToString() => $"{nameof(AABB2D)}({LowerBoundary}, {UpperBoundary})";
}
/// <summary>
/// Provides extension methods for the <see cref="AABB2D"/> struct.
/// </summary>
public static class AABBExtensions
{
/// <inheritdoc cref="AABB2D.FromVectors" />
public static AABB2D ToAABB(this IEnumerable<Vector2D> vectors) => AABB2D.FromVectors(vectors);
/// <inheritdoc cref="AABB2D.ApproximatelyEquals" />
public static bool ApproximatelyEquals(this AABB2D left, AABB2D right, float epsilon = float.Epsilon) => AABB2D.ApproximatelyEquals(left, right, epsilon);
}

View File

@@ -143,7 +143,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, INetworkCommunicat
private void SetupEnginePackets()
{
// I know, ugly af. I need to find a better way
netPacketProcessor.RegisterNestedType(AABBNetPacker.Write, AABBNetPacker.Read);
netPacketProcessor.RegisterNestedType(AABB2DNetPacker.Write, AABB2DNetPacker.Read);
netPacketProcessor.RegisterNestedType(CircleNetPacker.Write, CircleNetPacker.Read);
netPacketProcessor.RegisterNestedType(ColorHSVNetPacker.Write, ColorHSVNetPacker.Read);
netPacketProcessor.RegisterNestedType(ColorRGBANetPacker.Write, ColorRGBANetPacker.Read);

View File

@@ -4,19 +4,19 @@ using Engine.Core;
namespace Engine.Systems.Network;
internal static class AABBNetPacker
internal static class AABB2DNetPacker
{
internal static void Write(NetDataWriter writer, AABB data)
internal static void Write(NetDataWriter writer, AABB2D data)
{
Vector2DNetPacker.Write(writer, data.LowerBoundary);
Vector2DNetPacker.Write(writer, data.UpperBoundary);
}
internal static AABB Read(NetDataReader reader)
internal static AABB2D Read(NetDataReader reader)
{
Vector2D lowerBoundary = Vector2DNetPacker.Read(reader);
Vector2D upperBoundary = Vector2DNetPacker.Read(reader);
return new AABB(lowerBoundary, upperBoundary);
return new AABB2D(lowerBoundary, upperBoundary);
}
}

View File

@@ -10,13 +10,13 @@ namespace Engine.Integration.MonoGame;
public interface ISpriteBatch
{
void Begin(SpriteSortMode sortMode = SpriteSortMode.Deferred, BlendState? blendState = null, SamplerState? samplerState = null, DepthStencilState? depthStencilState = null, RasterizerState? rasterizerState = null, Effect? effect = null, Matrix? transformMatrix = null);
void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth);
void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth);
void Draw(Texture2D texture, AABB destinationAABB, AABB? sourceAABB, Color color, float rotation, Vector2D origin, SpriteEffects effects, float layerDepth);
void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color);
void Draw(Texture2D texture, AABB destinationAABB, AABB? sourceAABB, Color color);
void Draw(Texture2D texture, Vector2D position, AABB2D? sourceAABB, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth);
void Draw(Texture2D texture, Vector2D position, AABB2D? sourceAABB, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth);
void Draw(Texture2D texture, AABB2D destinationAABB, AABB2D? sourceAABB, Color color, float rotation, Vector2D origin, SpriteEffects effects, float layerDepth);
void Draw(Texture2D texture, Vector2D position, AABB2D? sourceAABB, Color color);
void Draw(Texture2D texture, AABB2D destinationAABB, AABB2D? sourceAABB, Color color);
void Draw(Texture2D texture, Vector2D position, Color color);
void Draw(Texture2D texture, AABB destinationAABB, Color color);
void Draw(Texture2D texture, AABB2D destinationAABB, Color color);
void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color);
void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth);
void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth);

View File

@@ -14,25 +14,25 @@ public class SpriteBatchWrapper(GraphicsDevice graphicsDevice) : ISpriteBatch
public void Begin(SpriteSortMode sortMode = SpriteSortMode.Deferred, BlendState? blendState = null, SamplerState? samplerState = null, DepthStencilState? depthStencilState = null, RasterizerState? rasterizerState = null, Effect? effect = null, Matrix? transformMatrix = null)
=> spriteBatch.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, transformMatrix);
public void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth)
public void Draw(Texture2D texture, Vector2D position, AABB2D? sourceAABB, Color color, float rotation, Vector2D origin, Vector2D scale, SpriteEffects effects, float layerDepth)
=> spriteBatch.Draw(texture, position.ToDisplayVector2(), sourceAABB?.ToRectangle(), color, rotation, origin.ToDisplayVector2(), scale.ToDisplayVector2(), effects, layerDepth);
public void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth)
public void Draw(Texture2D texture, Vector2D position, AABB2D? sourceAABB, Color color, float rotation, Vector2D origin, float scale, SpriteEffects effects, float layerDepth)
=> spriteBatch.Draw(texture, position.ToDisplayVector2(), sourceAABB?.ToRectangle(), color, rotation, origin.ToDisplayVector2(), scale, effects, layerDepth);
public void Draw(Texture2D texture, AABB destinationAABB, AABB? sourceAABB, Color color, float rotation, Vector2D origin, SpriteEffects effects, float layerDepth)
public void Draw(Texture2D texture, AABB2D destinationAABB, AABB2D? sourceAABB, Color color, float rotation, Vector2D origin, SpriteEffects effects, float layerDepth)
=> spriteBatch.Draw(texture, destinationAABB.ToRectangle(), sourceAABB?.ToRectangle(), color, rotation, origin.ToDisplayVector2(), effects, layerDepth);
public void Draw(Texture2D texture, Vector2D position, AABB? sourceAABB, Color color)
public void Draw(Texture2D texture, Vector2D position, AABB2D? sourceAABB, Color color)
=> spriteBatch.Draw(texture, position.ToDisplayVector2(), sourceAABB?.ToRectangle(), color);
public void Draw(Texture2D texture, AABB destinationAABB, AABB? sourceAABB, Color color)
public void Draw(Texture2D texture, AABB2D destinationAABB, AABB2D? sourceAABB, Color color)
=> spriteBatch.Draw(texture, destinationAABB.ToRectangle(), sourceAABB?.ToRectangle(), color);
public void Draw(Texture2D texture, Vector2D position, Color color)
=> spriteBatch.Draw(texture, position.ToDisplayVector2(), color);
public void Draw(Texture2D texture, AABB destinationAABB, Color color)
public void Draw(Texture2D texture, AABB2D destinationAABB, Color color)
=> spriteBatch.Draw(texture, destinationAABB.ToRectangle(), color);
public void DrawString(SpriteFont spriteFont, string text, Vector2D position, Color color)

View File

@@ -60,7 +60,7 @@ public static class EngineConverterExtensions
};
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rectangle ToRectangle(this AABB aabb) => new()
public static Rectangle ToRectangle(this AABB2D aabb) => new()
{
X = (int)(aabb.LowerBoundary.X * screenScale.X),
Y = (int)(aabb.LowerBoundary.Y * screenScale.Y),

View File

@@ -8,33 +8,33 @@ using YamlDotNet.Serialization;
namespace Engine.Serializers.Yaml;
public class AABBConverter : EngineTypeYamlSerializerBase<AABB>
public class AABB2DConverter : EngineTypeYamlSerializerBase<AABB2D>
{
public override AABB Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
public override AABB2D Read(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
parser.Consume<MappingStart>();
if (parser.Consume<Scalar>().Value.CompareTo(nameof(AABB.LowerBoundary)) != 0)
throw new ArgumentException($"{nameof(AABB)} mapping must start with {nameof(AABB.LowerBoundary)}");
if (parser.Consume<Scalar>().Value.CompareTo(nameof(AABB2D.LowerBoundary)) != 0)
throw new ArgumentException($"{nameof(AABB2D)} mapping must start with {nameof(AABB2D.LowerBoundary)}");
Vector2D lowerBoundary = (Vector2D)rootDeserializer(typeof(Vector2D))!;
if (parser.Consume<Scalar>().Value.CompareTo(nameof(AABB.UpperBoundary)) != 0)
throw new ArgumentException($"{nameof(AABB)} mapping must end with {nameof(AABB.UpperBoundary)}");
if (parser.Consume<Scalar>().Value.CompareTo(nameof(AABB2D.UpperBoundary)) != 0)
throw new ArgumentException($"{nameof(AABB2D)} mapping must end with {nameof(AABB2D.UpperBoundary)}");
Vector2D upperBoundary = (Vector2D)rootDeserializer(typeof(Vector2D))!;
parser.Consume<MappingEnd>();
return new AABB(lowerBoundary, upperBoundary);
return new AABB2D(lowerBoundary, upperBoundary);
}
public override void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
AABB aabb = (AABB)value!;
AABB2D aabb = (AABB2D)value!;
emitter.Emit(new MappingStart());
emitter.Emit(new Scalar(nameof(AABB.LowerBoundary)));
emitter.Emit(new Scalar(nameof(AABB2D.LowerBoundary)));
serializer(aabb.LowerBoundary, typeof(Vector2D));
emitter.Emit(new Scalar(nameof(AABB.UpperBoundary)));
emitter.Emit(new Scalar(nameof(AABB2D.UpperBoundary)));
serializer(aabb.UpperBoundary, typeof(Vector2D));
emitter.Emit(new MappingEnd());
}

View File

@@ -68,11 +68,11 @@ public static class Physics2D
return isOverlapping;
}
public static bool Overlaps(this AABB aabb, Vector2D point)
public static bool Overlaps(this AABB2D aabb, Vector2D point)
=> point.X >= aabb.LowerBoundary.X && point.X <= aabb.UpperBoundary.X &&
point.Y >= aabb.LowerBoundary.Y && point.Y <= aabb.UpperBoundary.Y;
public static bool Overlaps(this AABB left, AABB right)
public static bool Overlaps(this AABB2D left, AABB2D right)
=> left.LowerBoundary.X <= right.UpperBoundary.X && left.UpperBoundary.X >= right.LowerBoundary.X &&
left.LowerBoundary.Y <= right.UpperBoundary.Y && left.UpperBoundary.Y >= right.LowerBoundary.Y;

View File

@@ -0,0 +1,24 @@
using Engine.Core;
namespace Engine.Systems.Tween;
public static class TweenAABB2DExtensions
{
private static readonly BoxedPool<AABB2D> boxedAABBPool = new(2);
public static ITween TweenAABB(this AABB2D initialAABB, ITweenManager tweenManager, float duration, AABB2D targetAABB, System.Action<AABB2D> setMethod)
{
Boxed<AABB2D> boxedInitial = boxedAABBPool.Get(initialAABB);
Boxed<AABB2D> boxedTarget = boxedAABBPool.Get(targetAABB);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(new AABB2D(boxedInitial.Value.LowerBoundary.Lerp(boxedTarget.Value.LowerBoundary, t), boxedInitial.Value.UpperBoundary.Lerp(boxedTarget.Value.UpperBoundary, t))));
tween.OnComplete(() =>
{
boxedAABBPool.Return(boxedInitial);
boxedAABBPool.Return(boxedTarget);
});
return tween;
}
}

View File

@@ -1,24 +0,0 @@
using Engine.Core;
namespace Engine.Systems.Tween;
public static class TweenAABBExtensions
{
private static readonly BoxedPool<AABB> boxedAABBPool = new(2);
public static ITween TweenAABB(this AABB initialAABB, ITweenManager tweenManager, float duration, AABB targetAABB, System.Action<AABB> setMethod)
{
Boxed<AABB> boxedInitial = boxedAABBPool.Get(initialAABB);
Boxed<AABB> boxedTarget = boxedAABBPool.Get(targetAABB);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(new AABB(boxedInitial.Value.LowerBoundary.Lerp(boxedTarget.Value.LowerBoundary, t), boxedInitial.Value.UpperBoundary.Lerp(boxedTarget.Value.UpperBoundary, t))));
tween.OnComplete(() =>
{
boxedAABBPool.Return(boxedInitial);
boxedAABBPool.Return(boxedTarget);
});
return tween;
}
}