feat: ShapeAABB

This commit is contained in:
2024-01-24 18:40:12 +03:00
parent 04653711e9
commit 5b484eb38c
4 changed files with 53 additions and 15 deletions

View File

@@ -0,0 +1,42 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Apos.Shapes;
using Syntriax.Engine.Core;
using Syntriax.Engine.Physics2D.Primitives;
using Syntriax.Engine.Core.Abstract;
namespace Pong.Behaviours;
public class ShapeAABBBehaviour : BehaviourOverride, IDisplayableShape
{
private readonly List<Vector2D> vectors = [];
private ShapeBehaviour? shapeBehaviour = null;
private readonly static Vector2D screenScale = Vector2D.Down + Vector2D.Right;
public ShapeAABBBehaviour() { }
public ShapeAABBBehaviour(float Thickness) { this.Thickness = Thickness; }
public ShapeAABBBehaviour(Color color) { Color = color; }
public ShapeAABBBehaviour(Color color, float Thickness) { this.Thickness = Thickness; Color = color; }
public Color Color { get; set; } = Color.White;
public float Thickness { get; set; } = .5f;
protected override void OnFirstActiveFrame()
{
BehaviourController.TryGetBehaviour(out shapeBehaviour);
}
public void Draw(ShapeBatch shapeBatch)
{
if (shapeBehaviour is null)
return;
shapeBehaviour.Shape.TransformShape(Transform, vectors);
AABB aabb = AABB.FromVectors(vectors);
shapeBatch.DrawRectangle(aabb.Center.Scale(screenScale).Subtract(aabb.SizeHalf).ToVector2(), aabb.Size.ToVector2(), Color.Transparent, Color.Blue);
}
}

View File

@@ -4,8 +4,6 @@ using Microsoft.Xna.Framework;
using Apos.Shapes;
using Engine.Physics2D;
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Physics2D.Primitives;
@@ -15,6 +13,7 @@ namespace Pong.Behaviours;
public class ShapeBehaviour : BehaviourOverride, IDisplayableShape
{
private readonly List<Vector2D> vectors = [];
private readonly static Vector2D screenScale = Vector2D.Down + Vector2D.Right;
public ShapeBehaviour(Shape Shape) { this.Shape = Shape; }
public ShapeBehaviour(Shape Shape, float Thickness) { this.Shape = Shape; this.Thickness = Thickness; }
@@ -28,10 +27,12 @@ public class ShapeBehaviour : BehaviourOverride, IDisplayableShape
public void Draw(ShapeBatch shapeBatch)
{
Shape.TransformShape(GameObject.Transform, vectors);
for (int i = 0; i < vectors.Count; i++)
vectors[i] = vectors[i].Scale(screenScale);
for (int i = 0; i < vectors.Count - 1; i++)
shapeBatch.DrawLine(vectors[i].Scale(Vector2D.Down + Vector2D.Right).ToVector2(), vectors[i + 1].Scale(Vector2D.Down + Vector2D.Right).ToVector2(), Thickness, Color, Color);
shapeBatch.DrawLine(vectors[0].Scale(Vector2D.Down + Vector2D.Right).ToVector2(), vectors[^1].Scale(Vector2D.Down + Vector2D.Right).ToVector2(), Thickness, Color, Color);
shapeBatch.DrawLine(vectors[i].ToVector2(), vectors[i + 1].ToVector2(), Thickness, Color, Color);
shapeBatch.DrawLine(vectors[0].ToVector2(), vectors[^1].ToVector2(), Thickness, Color, Color);
}
}
@@ -43,12 +44,6 @@ public static class ShapeTransform
int count = shape.Vertices.Count;
for (int i = 0; i < count; i++)
vectors.Add
(
shape[i]
.Scale(transform.Scale)
.Rotate(transform.Rotation * Physics2D.DegreeToRadian)
.Add(transform.Position)
);
vectors.Add(transform.TransformVector2D(shape[i]));
}
}