From a05fb3ba33c03ef665c7d21e437679ef66e4f816 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Tue, 5 Dec 2023 17:39:38 +0300 Subject: [PATCH] rgrae --- Game/Physics2D/Abstract/ICollider2D.cs | 14 ++----- Game/Physics2D/Collider2DAABBBehaviour.cs | 49 +++++++++++++++++++++++ Game/Physics2D/PhysicsEngine2D.cs | 5 ++- 3 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 Game/Physics2D/Collider2DAABBBehaviour.cs diff --git a/Game/Physics2D/Abstract/ICollider2D.cs b/Game/Physics2D/Abstract/ICollider2D.cs index 131cac3..6edb610 100644 --- a/Game/Physics2D/Abstract/ICollider2D.cs +++ b/Game/Physics2D/Abstract/ICollider2D.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; + using Microsoft.Xna.Framework; using Syntriax.Engine.Core.Abstract; @@ -11,15 +12,8 @@ public interface ICollider2D : IBehaviour, IAssignableTransform { IRigidBody2D? RigidBody2D { get; } - Action? OnCollision { get; set; } + Action? OnCollisionPreResolve { get; set; } - Vector2 OffsetPosition { get; set; } - Vector2 OffsetScale { get; set; } - float OffsetRotation { get; set; } - IReadOnlyList Vertices { get; } - - - bool CheckCollision(Vector2 point, ICollider2D otherCollider, out CollisionInformation? collisionInformation); - - void RecalculateVertices(); + bool CheckCollision(Vector2 point, ICollider2D otherCollider); + void Recalculate(); } diff --git a/Game/Physics2D/Collider2DAABBBehaviour.cs b/Game/Physics2D/Collider2DAABBBehaviour.cs new file mode 100644 index 0000000..cfabc2a --- /dev/null +++ b/Game/Physics2D/Collider2DAABBBehaviour.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; + +using Microsoft.Xna.Framework; + +using Syntriax.Engine.Core; +using Syntriax.Engine.Core.Abstract; +using Syntriax.Engine.Physics2D.Abstract; + +namespace Syntriax.Engine.Physics2D; + +public class Collider2DAABBBehaviour(Vector2 lowerBound, Vector2 upperBound) : BehaviourOverride, ICollider2D +{ + public Vector2 LowerBound { get; } = lowerBound; + public Vector2 UpperBound { get; } = upperBound; + + private IRigidBody2D? _rigidBody2D = null; + + public IRigidBody2D? RigidBody2D + { + get + { + if (_rigidBody2D is null) + BehaviourController.TryGetBehaviour(out _rigidBody2D); + + return _rigidBody2D; + } + } + + public Action? OnCollisionPreResolve { get; set; } = null; + + public Action? OnTransformAssigned { get => GameObject.OnTransformAssigned; set => GameObject.OnTransformAssigned = value; } + ITransform IAssignableTransform.Transform => Transform; + public bool Assign(ITransform transform) => GameObject.Assign(transform); + + public bool CheckCollision(Vector2 point, ICollider2D otherCollider) + { + if (point.X >= LowerBound.X + Transform.Position.X && point.X <= UpperBound.X + Transform.Position.X && + point.Y >= LowerBound.Y + Transform.Position.Y && point.Y <= UpperBound.Y + Transform.Position.Y) + return true; + + return false; + } + + public void Recalculate() + { + throw new NotImplementedException(); + } +} diff --git a/Game/Physics2D/PhysicsEngine2D.cs b/Game/Physics2D/PhysicsEngine2D.cs index 4303d8f..c0a4ad7 100644 --- a/Game/Physics2D/PhysicsEngine2D.cs +++ b/Game/Physics2D/PhysicsEngine2D.cs @@ -1,7 +1,10 @@ using System; using System.Collections.Generic; + using Microsoft.Xna.Framework; + using Pong; + using Syntriax.Engine.Core; using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Graphics.TwoDimensional; @@ -49,7 +52,7 @@ public class PhysicsEngine2D : IPhysicsEngine2D foreach (var collider in colliders) - collider.RecalculateVertices(); + collider.Recalculate(); } }