Compare commits
	
		
			19 Commits
		
	
	
		
			breaking-c
			...
			physics
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 2f500bef19 | |||
| 7ed6c1e050 | |||
| bd9974f410 | |||
| 42b427f49b | |||
| ef08eedc34 | |||
| 9eed074025 | |||
| 427ab5ed54 | |||
| e252d8fbb2 | |||
| 80c0f4cdd6 | |||
| 91ad13628c | |||
| 76484017e1 | |||
| faf30b8227 | |||
| bf825fd961 | |||
| 6e6475f8bf | |||
| 5b484eb38c | |||
| 04653711e9 | |||
| 40903eacc3 | |||
| a70a934b3d | |||
| 8ea76f91f6 | 
							
								
								
									
										2
									
								
								Engine
									
									
									
									
									
								
							
							
								
								
								
								
								
							
						
						
									
										2
									
								
								Engine
									
									
									
									
									
								
							 Submodule Engine updated: d08495afbb...6a104d8abd
									
								
							
							
								
								
									
										23
									
								
								Game/Behaviours/CircleBehaviour.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								Game/Behaviours/CircleBehaviour.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
			
		||||
using Microsoft.Xna.Framework;
 | 
			
		||||
 | 
			
		||||
using Apos.Shapes;
 | 
			
		||||
 | 
			
		||||
using Syntriax.Engine.Physics2D.Primitives;
 | 
			
		||||
 | 
			
		||||
namespace Pong.Behaviours;
 | 
			
		||||
 | 
			
		||||
public class CircleBehaviour : Syntriax.Engine.Physics2D.Collider2DCircleBehaviour, IDisplayableShape
 | 
			
		||||
{
 | 
			
		||||
    public CircleBehaviour(Circle circle) { this.CircleLocal = circle; }
 | 
			
		||||
    public CircleBehaviour(Circle circle, float Thickness) { this.CircleLocal = circle; this.Thickness = Thickness; }
 | 
			
		||||
    public CircleBehaviour(Circle circle, Color color) { this.CircleLocal = circle; Color = color; }
 | 
			
		||||
    public CircleBehaviour(Circle circle, Color color, float Thickness) { this.CircleLocal = circle; this.Thickness = Thickness; Color = color; }
 | 
			
		||||
 | 
			
		||||
    public Color Color { get; set; } = Color.White;
 | 
			
		||||
    public float Thickness { get; set; } = .5f;
 | 
			
		||||
 | 
			
		||||
    public void Draw(ShapeBatch shapeBatch)
 | 
			
		||||
    {
 | 
			
		||||
        shapeBatch.BorderCircle(CircleWorld.Center.ToDisplayVector2(), CircleWorld.Radius, Color);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										8
									
								
								Game/Behaviours/IDisplayableShape.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Game/Behaviours/IDisplayableShape.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
using Apos.Shapes;
 | 
			
		||||
 | 
			
		||||
namespace Pong.Behaviours;
 | 
			
		||||
 | 
			
		||||
public interface IDisplayableShape
 | 
			
		||||
{
 | 
			
		||||
    void Draw(ShapeBatch shapeBatch);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								Game/Behaviours/MovementMouseBehaviour.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								Game/Behaviours/MovementMouseBehaviour.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
using System;
 | 
			
		||||
using Microsoft.Xna.Framework.Input;
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
namespace Pong.Behaviours;
 | 
			
		||||
 | 
			
		||||
public class MovementMouseBehaviour : BehaviourOverride
 | 
			
		||||
{
 | 
			
		||||
    protected override void OnUpdate()
 | 
			
		||||
    {
 | 
			
		||||
        GameObject.Transform.Position = Mouse.GetState().Position.ToVector2D().ApplyDisplayScale();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										25
									
								
								Game/Behaviours/RotatableBehaviour.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								Game/Behaviours/RotatableBehaviour.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
namespace Pong.Behaviours;
 | 
			
		||||
 | 
			
		||||
public class RotatableBehaviour : BehaviourOverride
 | 
			
		||||
{
 | 
			
		||||
    private KeyboardInputsBehaviour? inputs = null;
 | 
			
		||||
 | 
			
		||||
    protected override void OnFirstActiveFrame()
 | 
			
		||||
    {
 | 
			
		||||
        if (!BehaviourController.TryGetBehaviour(out inputs))
 | 
			
		||||
            inputs = BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected override void OnUpdate()
 | 
			
		||||
    {
 | 
			
		||||
        if (inputs is null)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        if (inputs.IsPressed(Microsoft.Xna.Framework.Input.Keys.NumPad4))
 | 
			
		||||
            Transform.Rotation += Time.Elapsed.Nanoseconds * 0.0025f;
 | 
			
		||||
        if (inputs.IsPressed(Microsoft.Xna.Framework.Input.Keys.NumPad6))
 | 
			
		||||
            Transform.Rotation -= Time.Elapsed.Nanoseconds * 0.0025f;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										47
									
								
								Game/Behaviours/ShapeAABBBehaviour.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								Game/Behaviours/ShapeAABBBehaviour.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
using Microsoft.Xna.Framework;
 | 
			
		||||
 | 
			
		||||
using Apos.Shapes;
 | 
			
		||||
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
using Syntriax.Engine.Input;
 | 
			
		||||
using Syntriax.Engine.Physics2D.Abstract;
 | 
			
		||||
using Syntriax.Engine.Physics2D.Primitives;
 | 
			
		||||
 | 
			
		||||
namespace Pong.Behaviours;
 | 
			
		||||
 | 
			
		||||
public class ShapeAABBBehaviour : BehaviourOverride, IDisplayableShape
 | 
			
		||||
{
 | 
			
		||||
    private IShapeCollider2D? shapeCollider = null;
 | 
			
		||||
 | 
			
		||||
    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;
 | 
			
		||||
    public bool display = true;
 | 
			
		||||
 | 
			
		||||
    protected override void OnFirstActiveFrame()
 | 
			
		||||
    {
 | 
			
		||||
        BehaviourController.TryGetBehaviour(out shapeCollider);
 | 
			
		||||
 | 
			
		||||
        if (BehaviourController.TryGetBehaviour(out IButtonInputs<Microsoft.Xna.Framework.Input.Keys>? keys))
 | 
			
		||||
            keys.RegisterOnPress(Microsoft.Xna.Framework.Input.Keys.D, (_1, _2) => display = !display);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void Draw(ShapeBatch shapeBatch)
 | 
			
		||||
    {
 | 
			
		||||
        if (!display)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        if (shapeCollider is null)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        AABB aabb = AABB.FromVectors(shapeCollider.ShapeWorld);
 | 
			
		||||
 | 
			
		||||
        shapeBatch.BorderCircle(aabb.Center.ToDisplayVector2(), 7.5f, Color.Beige);
 | 
			
		||||
 | 
			
		||||
        shapeBatch.DrawRectangle(aabb.Center.ApplyDisplayScale().Subtract(aabb.SizeHalf).ToVector2(), aabb.Size.ToVector2(), Color.Transparent, Color.Blue);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										29
									
								
								Game/Behaviours/ShapeBehaviour.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								Game/Behaviours/ShapeBehaviour.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
using Microsoft.Xna.Framework;
 | 
			
		||||
 | 
			
		||||
using Apos.Shapes;
 | 
			
		||||
 | 
			
		||||
using Syntriax.Engine.Physics2D.Primitives;
 | 
			
		||||
 | 
			
		||||
namespace Pong.Behaviours;
 | 
			
		||||
 | 
			
		||||
public class ShapeBehaviour : Syntriax.Engine.Physics2D.Collider2DShapeBehaviour, IDisplayableShape
 | 
			
		||||
{
 | 
			
		||||
    public ShapeBehaviour(Shape Shape) { this.ShapeLocal = Shape; }
 | 
			
		||||
    public ShapeBehaviour(Shape Shape, float Thickness) { this.ShapeLocal = Shape; this.Thickness = Thickness; }
 | 
			
		||||
    public ShapeBehaviour(Shape Shape, Color color) { this.ShapeLocal = Shape; Color = color; }
 | 
			
		||||
    public ShapeBehaviour(Shape Shape, Color color, float Thickness) { this.ShapeLocal = Shape; this.Thickness = Thickness; Color = color; }
 | 
			
		||||
 | 
			
		||||
    public Color Color { get; set; } = Color.White;
 | 
			
		||||
    public float Thickness { get; set; } = .5f;
 | 
			
		||||
 | 
			
		||||
    public void Draw(ShapeBatch shapeBatch)
 | 
			
		||||
    {
 | 
			
		||||
        int count = ShapeWorld.Vertices.Count;
 | 
			
		||||
 | 
			
		||||
        shapeBatch.BorderCircle(Transform.Position.ToDisplayVector2(), 5f, Color.DarkRed);
 | 
			
		||||
 | 
			
		||||
        for (int i = 0; i < count - 1; i++)
 | 
			
		||||
            shapeBatch.DrawLine(ShapeWorld[i].ToDisplayVector2(), ShapeWorld[i + 1].ToDisplayVector2(), Thickness, Color, Color);
 | 
			
		||||
        shapeBatch.DrawLine(ShapeWorld[0].ToDisplayVector2(), ShapeWorld[^1].ToDisplayVector2(), Thickness, Color, Color);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -6,10 +6,18 @@ namespace Pong;
 | 
			
		||||
 | 
			
		||||
public static class EngineConverter
 | 
			
		||||
{
 | 
			
		||||
    public readonly static Vector2D screenScale = Vector2D.Down + Vector2D.Right;
 | 
			
		||||
 | 
			
		||||
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 | 
			
		||||
    public static EngineTime ToEngineTime(this GameTime gameTime) => new(gameTime.TotalGameTime, gameTime.ElapsedGameTime);
 | 
			
		||||
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 | 
			
		||||
    public static Vector2D ToVector2D(this Vector2 vector) => new(vector.X, vector.Y);
 | 
			
		||||
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 | 
			
		||||
    public static Vector2D ToVector2D(this Point point) => new(point.X, point.Y);
 | 
			
		||||
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 | 
			
		||||
    public static Vector2 ToVector2(this Vector2D vector) => new(vector.X, vector.Y);
 | 
			
		||||
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 | 
			
		||||
    public static Vector2 ToDisplayVector2(this Vector2D vector) => vector.Scale(screenScale).ToVector2();
 | 
			
		||||
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 | 
			
		||||
    public static Vector2D ApplyDisplayScale(this Vector2D vector) => vector.Scale(screenScale);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -20,12 +20,14 @@
 | 
			
		||||
    <EmbeddedResource Include="Icon.bmp" />
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <PackageReference Include="Apos.Shapes" Version="0.2.3" />
 | 
			
		||||
    <PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
 | 
			
		||||
    <PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <ProjectReference Include="..\Engine\Engine.Core\Engine.Core.csproj" />
 | 
			
		||||
    <ProjectReference Include="..\Engine\Engine.Input\Engine.Input.csproj" />
 | 
			
		||||
    <ProjectReference Include="..\Engine\Engine.Physics2D\Engine.Physics2D.csproj" />
 | 
			
		||||
  </ItemGroup>
 | 
			
		||||
  <Target Name="RestoreDotnetTools" BeforeTargets="Restore">
 | 
			
		||||
    <Message Text="Restoring dotnet tools" Importance="High" />
 | 
			
		||||
 
 | 
			
		||||
@@ -1,31 +0,0 @@
 | 
			
		||||
 | 
			
		||||
Microsoft Visual Studio Solution File, Format Version 12.00
 | 
			
		||||
# Visual Studio Version 17
 | 
			
		||||
VisualStudioVersion = 17.5.002.0
 | 
			
		||||
MinimumVisualStudioVersion = 10.0.40219.1
 | 
			
		||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Game", "Game.csproj", "{42644486-9F9E-4242-B6C4-AF31BBFA31D2}"
 | 
			
		||||
EndProject
 | 
			
		||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Engine.Core", "..\Engine\Engine.Core\Engine.Core.csproj", "{EF1FE4A2-40DF-4967-8003-CF6D98010D02}"
 | 
			
		||||
EndProject
 | 
			
		||||
Global
 | 
			
		||||
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 | 
			
		||||
		Debug|Any CPU = Debug|Any CPU
 | 
			
		||||
		Release|Any CPU = Release|Any CPU
 | 
			
		||||
	EndGlobalSection
 | 
			
		||||
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
 | 
			
		||||
		{42644486-9F9E-4242-B6C4-AF31BBFA31D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 | 
			
		||||
		{42644486-9F9E-4242-B6C4-AF31BBFA31D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
 | 
			
		||||
		{42644486-9F9E-4242-B6C4-AF31BBFA31D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
 | 
			
		||||
		{42644486-9F9E-4242-B6C4-AF31BBFA31D2}.Release|Any CPU.Build.0 = Release|Any CPU
 | 
			
		||||
		{EF1FE4A2-40DF-4967-8003-CF6D98010D02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 | 
			
		||||
		{EF1FE4A2-40DF-4967-8003-CF6D98010D02}.Debug|Any CPU.Build.0 = Debug|Any CPU
 | 
			
		||||
		{EF1FE4A2-40DF-4967-8003-CF6D98010D02}.Release|Any CPU.ActiveCfg = Release|Any CPU
 | 
			
		||||
		{EF1FE4A2-40DF-4967-8003-CF6D98010D02}.Release|Any CPU.Build.0 = Release|Any CPU
 | 
			
		||||
	EndGlobalSection
 | 
			
		||||
	GlobalSection(SolutionProperties) = preSolution
 | 
			
		||||
		HideSolutionNode = FALSE
 | 
			
		||||
	EndGlobalSection
 | 
			
		||||
	GlobalSection(ExtensibilityGlobals) = postSolution
 | 
			
		||||
		SolutionGuid = {AB3225EE-1621-439F-8F83-DF4515922FEF}
 | 
			
		||||
	EndGlobalSection
 | 
			
		||||
EndGlobal
 | 
			
		||||
							
								
								
									
										165
									
								
								Game/Game1.cs
									
									
									
									
									
								
							
							
						
						
									
										165
									
								
								Game/Game1.cs
									
									
									
									
									
								
							@@ -4,10 +4,10 @@ using Microsoft.Xna.Framework.Graphics;
 | 
			
		||||
using Microsoft.Xna.Framework.Input;
 | 
			
		||||
 | 
			
		||||
using Pong.Behaviours;
 | 
			
		||||
using Apos.Shapes;
 | 
			
		||||
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
using Syntriax.Engine.Core.Abstract;
 | 
			
		||||
using Syntriax.Engine.Graphics.TwoDimensional;
 | 
			
		||||
using Syntriax.Engine.Physics2D;
 | 
			
		||||
using Syntriax.Engine.Physics2D.Primitives;
 | 
			
		||||
 | 
			
		||||
@@ -18,6 +18,7 @@ public class Game1 : Game
 | 
			
		||||
    private GraphicsDeviceManager _graphics = null!;
 | 
			
		||||
    private PhysicsEngine2D engine;
 | 
			
		||||
    private SpriteBatch _spriteBatch = null!;
 | 
			
		||||
    private ShapeBatch _shapeBatch = null!;
 | 
			
		||||
    public static GameManager gameManager = null!;
 | 
			
		||||
    public static Sprite spriteBox = null!;
 | 
			
		||||
    private MonoGameCameraBehaviour cameraBehaviour = null!;
 | 
			
		||||
@@ -30,7 +31,8 @@ public class Game1 : Game
 | 
			
		||||
        _graphics = new GraphicsDeviceManager(this)
 | 
			
		||||
        {
 | 
			
		||||
            PreferredBackBufferWidth = 1024,
 | 
			
		||||
            PreferredBackBufferHeight = 576
 | 
			
		||||
            PreferredBackBufferHeight = 576,
 | 
			
		||||
            GraphicsProfile = GraphicsProfile.HiDef
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        Content.RootDirectory = "Content";
 | 
			
		||||
@@ -50,9 +52,10 @@ public class Game1 : Game
 | 
			
		||||
    protected override void LoadContent()
 | 
			
		||||
    {
 | 
			
		||||
        _spriteBatch = new SpriteBatch(GraphicsDevice);
 | 
			
		||||
        _shapeBatch = new ShapeBatch(GraphicsDevice, Content);
 | 
			
		||||
 | 
			
		||||
        spriteBox = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Pixel") };
 | 
			
		||||
        Sprite spriteBall = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Circle") };
 | 
			
		||||
        // spriteBox = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Pixel") };
 | 
			
		||||
        // Sprite spriteBall = new Sprite() { Texture2D = Content.Load<Texture2D>("Sprites/Circle") };
 | 
			
		||||
 | 
			
		||||
        IGameObject gameObjectCamera = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
        gameObjectCamera.Name = "Camera";
 | 
			
		||||
@@ -62,76 +65,72 @@ public class Game1 : Game
 | 
			
		||||
        cameraBehaviour.Viewport = GraphicsDevice.Viewport;
 | 
			
		||||
        gameManager.Camera = cameraBehaviour;
 | 
			
		||||
 | 
			
		||||
        GameObject gameObjectBall = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
        gameObjectBall.Name = "Ball";
 | 
			
		||||
        gameObjectBall.Transform.Position = Vector2D.Zero;
 | 
			
		||||
        gameObjectBall.Transform.Scale = new Vector2D(1f / 51.2f, 1f / 51.2f);
 | 
			
		||||
        engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>());
 | 
			
		||||
        gameObjectBall.BehaviourController.AddBehaviour<MovementBallBehaviour>(new Vector2D(.1f, .1f), 500f);
 | 
			
		||||
        gameObjectBall.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall);
 | 
			
		||||
        gameObjectBall.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2D.One * 512f * .5f, Vector2D.One * 512f * .5f);
 | 
			
		||||
        // gameObjectBall = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
        // gameObjectBall.Name = "Ball";
 | 
			
		||||
        // gameObjectBall.Transform.Position = Vector2D.UnitY * 30f;
 | 
			
		||||
        // gameObjectBall.Transform.Scale = new Vector2D(1f / 51.2f, 1f / 51.2f);
 | 
			
		||||
        // engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour<RigidBody2D>());
 | 
			
		||||
        // gameObjectBall.BehaviourController.AddBehaviour<MovementBallBehaviour>(new Vector2D(.1f, .01f), 500f);
 | 
			
		||||
        // gameObjectBall.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall);
 | 
			
		||||
        // gameObjectBall.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2D.One * 512f * .5f, Vector2D.One * 512f * .5f);
 | 
			
		||||
 | 
			
		||||
        // IGameObject gameObjectLeft = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
        // gameObjectLeft.Name = "Left";
 | 
			
		||||
        // gameObjectLeft.Transform.Position = new Vector2D(-452, 0f);
 | 
			
		||||
        // gameObjectLeft.Transform.Scale = new Vector2D(10f, 40f);
 | 
			
		||||
        // gameObjectLeft.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
 | 
			
		||||
        // gameObjectLeft.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
 | 
			
		||||
        // gameObjectLeft.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
 | 
			
		||||
        // gameObjectLeft.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2D.One * .5f, Vector2D.One * .5f);
 | 
			
		||||
        // engine.AddRigidBody(gameObjectLeft.BehaviourController.AddBehaviour<RigidBody2D>());
 | 
			
		||||
        // IGameObject gameObjectCircle = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
        // gameObjectCircle.Name = "Circle";
 | 
			
		||||
        // gameObjectCircle.Transform.Position = new Vector2D(0f, -50f);
 | 
			
		||||
        // gameObjectCircle.Transform.Scale = new Vector2D(25f, 25f);
 | 
			
		||||
        // gameObjectCircle.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
 | 
			
		||||
        // gameObjectCircle.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
 | 
			
		||||
        // gameObjectCircle.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
 | 
			
		||||
        // engine.AddRigidBody(gameObjectCircle.BehaviourController.AddBehaviour<RigidBody2D>());
 | 
			
		||||
 | 
			
		||||
        // IGameObject gameObjectRight = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
        // gameObjectRight.Name = "Right";
 | 
			
		||||
        // gameObjectRight.Transform.Position = new Vector2D(452, 0f);
 | 
			
		||||
        // gameObjectRight.Transform.Scale = new Vector2D(10f, 40f);
 | 
			
		||||
        // gameObjectRight.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
 | 
			
		||||
        // gameObjectRight.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.Up, Keys.Down, 268f, -268f, 400f);
 | 
			
		||||
        // gameObjectRight.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
 | 
			
		||||
        // gameObjectRight.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2D.One * .5f, Vector2D.One * .5f);
 | 
			
		||||
        // engine.AddRigidBody(gameObjectRight.BehaviourController.AddBehaviour<RigidBody2D>());
 | 
			
		||||
        IGameObject gameObjectCircle2 = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
        gameObjectCircle2.Name = "Circle2";
 | 
			
		||||
        gameObjectCircle2.Transform.Position = new Vector2D(5f, 50f);
 | 
			
		||||
        gameObjectCircle2.Transform.Scale = new Vector2D(25f, 25f);
 | 
			
		||||
        gameObjectCircle2.BehaviourController.AddBehaviour<MovementMouseBehaviour>();
 | 
			
		||||
        gameObjectCircle2.BehaviourController.AddBehaviour<CircleBehaviour>(new Circle(Vector2D.Zero, 1f));
 | 
			
		||||
        engine.AddRigidBody(gameObjectCircle2.BehaviourController.AddBehaviour<RigidBody2D>());
 | 
			
		||||
 | 
			
		||||
        IGameObject gameObjectDiamond = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
        gameObjectDiamond.Name = "Diamond";
 | 
			
		||||
        gameObjectDiamond.Transform.Position = new Vector2D(-150f, -150f);
 | 
			
		||||
        gameObjectDiamond.Transform.Scale = new Vector2D(50f, 50f);
 | 
			
		||||
        gameObjectDiamond.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
 | 
			
		||||
        gameObjectDiamond.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
 | 
			
		||||
        gameObjectDiamond.BehaviourController.AddBehaviour<RotatableBehaviour>();
 | 
			
		||||
        gameObjectDiamond.BehaviourController.AddBehaviour<ShapeBehaviour>(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, -Vector2D.One, Vector2D.Left]));
 | 
			
		||||
        gameObjectDiamond.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
 | 
			
		||||
        engine.AddRigidBody(gameObjectDiamond.BehaviourController.AddBehaviour<RigidBody2D>());
 | 
			
		||||
 | 
			
		||||
        IGameObject gameObjectBox = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
        gameObjectBox.Name = "Box";
 | 
			
		||||
        gameObjectBox.Transform.Position = new Vector2D(150f, -150f);
 | 
			
		||||
        gameObjectBox.Transform.Scale = new Vector2D(100f, 100f);
 | 
			
		||||
        gameObjectBox.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
 | 
			
		||||
        gameObjectBox.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
 | 
			
		||||
        gameObjectBox.BehaviourController.AddBehaviour<RotatableBehaviour>();
 | 
			
		||||
        gameObjectBox.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.Pentagon);
 | 
			
		||||
        gameObjectBox.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
 | 
			
		||||
        engine.AddRigidBody(gameObjectBox.BehaviourController.AddBehaviour<RigidBody2D>());
 | 
			
		||||
 | 
			
		||||
        for (int i = 3; i < 10; i++)
 | 
			
		||||
        {
 | 
			
		||||
            IGameObject Test = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
            Test.Name = i.ToString();
 | 
			
		||||
            Test.Transform.Position = new Vector2D((i - 6) * 150, 0f);
 | 
			
		||||
            Test.Transform.Scale = new Vector2D(75f, 75f);
 | 
			
		||||
            Test.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
 | 
			
		||||
            Test.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
 | 
			
		||||
            Test.BehaviourController.AddBehaviour<RotatableBehaviour>();
 | 
			
		||||
            Test.BehaviourController.AddBehaviour<ShapeBehaviour>(Shape.CreateNgon(i));
 | 
			
		||||
            Test.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
 | 
			
		||||
            RigidBody2D rigidBody = Test.BehaviourController.AddBehaviour<RigidBody2D>();
 | 
			
		||||
            rigidBody.AngularVelocity = 90f;
 | 
			
		||||
            engine.AddRigidBody(rigidBody);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        IGameObject goPlayAreaTop = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
        goPlayAreaTop.Transform.Position = new Vector2D(0f, 288f + 20f);
 | 
			
		||||
        goPlayAreaTop.Transform.Scale = new Vector2D(10240f, 40f);
 | 
			
		||||
        goPlayAreaTop.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
 | 
			
		||||
        goPlayAreaTop.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2D.One * .5f, Vector2D.One * .5f);
 | 
			
		||||
        engine.AddRigidBody(goPlayAreaTop.BehaviourController.AddBehaviour<RigidBody2D>());
 | 
			
		||||
        IGameObject goPlayAreaBottom = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
        goPlayAreaBottom.Transform.Position = new Vector2D(0f, -(288f + 20f));
 | 
			
		||||
        goPlayAreaBottom.Transform.Scale = new Vector2D(10240f, 40f);
 | 
			
		||||
        goPlayAreaBottom.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
 | 
			
		||||
        goPlayAreaBottom.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2D.One * .5f, Vector2D.One * .5f);
 | 
			
		||||
        engine.AddRigidBody(goPlayAreaBottom.BehaviourController.AddBehaviour<RigidBody2D>());
 | 
			
		||||
 | 
			
		||||
        IGameObject goPlayAreaRight = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
        goPlayAreaRight.Transform.Position = new Vector2D(512f + 20f, 0f);
 | 
			
		||||
        goPlayAreaRight.Transform.Scale = new Vector2D(40f, 5760f);
 | 
			
		||||
        goPlayAreaRight.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
 | 
			
		||||
        goPlayAreaRight.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2D.One * .5f, Vector2D.One * .5f);
 | 
			
		||||
        engine.AddRigidBody(goPlayAreaRight.BehaviourController.AddBehaviour<RigidBody2D>());
 | 
			
		||||
        IGameObject goPlayAreaLeft = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
        goPlayAreaLeft.Transform.Position = new Vector2D(-(512f + 20f), 0f);
 | 
			
		||||
        goPlayAreaLeft.Transform.Scale = new Vector2D(40f, 5760f);
 | 
			
		||||
        goPlayAreaLeft.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
 | 
			
		||||
        goPlayAreaLeft.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2D.One * .5f, Vector2D.One * .5f);
 | 
			
		||||
        engine.AddRigidBody(goPlayAreaLeft.BehaviourController.AddBehaviour<RigidBody2D>());
 | 
			
		||||
 | 
			
		||||
        // IGameObject goPlayAreaCenter = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
        // goPlayAreaCenter.Transform.Position = new Vector2D(100f, 100f);
 | 
			
		||||
        // goPlayAreaCenter.Transform.Scale = new Vector2D(40f, 40f);
 | 
			
		||||
        // // goPlayAreaCenter.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
 | 
			
		||||
        // engine.AddRigidBody(goPlayAreaCenter.BehaviourController.AddBehaviour<RigidBody2D>());
 | 
			
		||||
        // TODO: use this.Content to load your game content here
 | 
			
		||||
        // IGameObject gameObjectShape = gameManager.InstantiateGameObject<GameObject>();
 | 
			
		||||
        // gameObjectShape.Name = "Shape";
 | 
			
		||||
        // gameObjectShape.Transform.Position = new Vector2D(250f, 0f);
 | 
			
		||||
        // gameObjectShape.Transform.Scale = new Vector2D(100f, 100f);
 | 
			
		||||
        // gameObjectShape.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
 | 
			
		||||
        // gameObjectShape.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
 | 
			
		||||
        // gameObjectShape.BehaviourController.AddBehaviour<RotatableBehaviour>();
 | 
			
		||||
        // gameObjectShape.BehaviourController.AddBehaviour<ShapeBehaviour>(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, Vector2D.Zero, Vector2D.Left]));
 | 
			
		||||
        // gameObjectShape.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected override void Update(GameTime gameTime)
 | 
			
		||||
@@ -206,12 +205,12 @@ public class Game1 : Game
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        // while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds)
 | 
			
		||||
        // {
 | 
			
		||||
        //     Console.WriteLine($"Physics Timer: {physicsTimer}");
 | 
			
		||||
        //     physicsTimer += 0.01f;
 | 
			
		||||
        //     engine.Step(.01f);
 | 
			
		||||
        // }
 | 
			
		||||
        while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds)
 | 
			
		||||
        {
 | 
			
		||||
            // Console.WriteLine($"Physics Timer: {physicsTimer}");
 | 
			
		||||
            physicsTimer += 0.01f;
 | 
			
		||||
            engine.Step(.01f);
 | 
			
		||||
        }
 | 
			
		||||
        gameManager.Update(gameTime.ToEngineTime());
 | 
			
		||||
        base.Update(gameTime);
 | 
			
		||||
    }
 | 
			
		||||
@@ -231,14 +230,16 @@ public class Game1 : Game
 | 
			
		||||
 | 
			
		||||
        _spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cameraBehaviour.MatrixTransform);
 | 
			
		||||
        foreach (IGameObject gameObject in gameManager)
 | 
			
		||||
        {
 | 
			
		||||
            if (!gameObject.BehaviourController.TryGetBehaviour(out IDisplayable? displayable))
 | 
			
		||||
                continue;
 | 
			
		||||
 | 
			
		||||
            displayable.Draw(_spriteBatch);
 | 
			
		||||
        }
 | 
			
		||||
            foreach (var displayable in gameObject.BehaviourController.GetBehaviours<IDisplayable>())
 | 
			
		||||
                displayable.Draw(_spriteBatch);
 | 
			
		||||
        _spriteBatch.End();
 | 
			
		||||
 | 
			
		||||
        _shapeBatch.Begin(cameraBehaviour.MatrixTransform);
 | 
			
		||||
        foreach (IGameObject gameObject in gameManager)
 | 
			
		||||
            foreach (var displayableShape in gameObject.BehaviourController.GetBehaviours<IDisplayableShape>())
 | 
			
		||||
                displayableShape.Draw(_shapeBatch);
 | 
			
		||||
        _shapeBatch.End();
 | 
			
		||||
 | 
			
		||||
        base.Draw(gameTime);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,19 +0,0 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
using Syntriax.Engine.Core.Abstract;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Physics2D.Abstract;
 | 
			
		||||
 | 
			
		||||
public interface ICollider2D : IBehaviour, IAssignableTransform
 | 
			
		||||
{
 | 
			
		||||
    Action<ICollider2D, ICollider2D>? OnCollisionPreResolve { get; set; }
 | 
			
		||||
 | 
			
		||||
    IRigidBody2D? RigidBody2D { get; }
 | 
			
		||||
 | 
			
		||||
    IList<Vector2D> Vertices { get; }
 | 
			
		||||
 | 
			
		||||
    bool CheckCollision(Vector2D point);
 | 
			
		||||
    void Recalculate();
 | 
			
		||||
}
 | 
			
		||||
@@ -1,6 +0,0 @@
 | 
			
		||||
namespace Syntriax.Engine.Physics2D.Abstract;
 | 
			
		||||
 | 
			
		||||
public interface ICollisionResolver2D
 | 
			
		||||
{
 | 
			
		||||
    void ResolveCollision(ICollider2D colliderA, ICollider2D colliderB);
 | 
			
		||||
}
 | 
			
		||||
@@ -1,11 +0,0 @@
 | 
			
		||||
namespace Syntriax.Engine.Physics2D.Abstract;
 | 
			
		||||
 | 
			
		||||
public interface IPhysicsEngine2D
 | 
			
		||||
{
 | 
			
		||||
    int IterationCount { get; set; }
 | 
			
		||||
 | 
			
		||||
    void AddRigidBody(IRigidBody2D rigidBody);
 | 
			
		||||
    void RemoveRigidBody(IRigidBody2D rigidBody);
 | 
			
		||||
 | 
			
		||||
    void Step(float deltaTime);
 | 
			
		||||
}
 | 
			
		||||
@@ -1,7 +0,0 @@
 | 
			
		||||
namespace Syntriax.Engine.Physics2D.Abstract;
 | 
			
		||||
 | 
			
		||||
public interface IPhysicsMaterial2D
 | 
			
		||||
{
 | 
			
		||||
    float Friction { get; }
 | 
			
		||||
    float Restitution { get; }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,14 +0,0 @@
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
using Syntriax.Engine.Core.Abstract;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Physics2D.Abstract;
 | 
			
		||||
 | 
			
		||||
public interface IRigidBody2D : IBehaviour, IAssignableTransform
 | 
			
		||||
{
 | 
			
		||||
    IPhysicsMaterial2D Material { get; set; }
 | 
			
		||||
 | 
			
		||||
    Vector2D Velocity { get; set; }
 | 
			
		||||
    float AngularVelocity { get; set; }
 | 
			
		||||
 | 
			
		||||
    float Mass { get; set; }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,67 +0,0 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
using Syntriax.Engine.Core.Abstract;
 | 
			
		||||
using Syntriax.Engine.Physics2D.Abstract;
 | 
			
		||||
using Syntriax.Engine.Physics2D.Primitives;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Physics2D;
 | 
			
		||||
 | 
			
		||||
public class Collider2DAABBBehaviour : BehaviourOverride, ICollider2D
 | 
			
		||||
{
 | 
			
		||||
    public AABB AABBLocal { get; set; } = null!;
 | 
			
		||||
    public AABB AABBWorld { get; private set; } = null!;
 | 
			
		||||
 | 
			
		||||
    private IRigidBody2D? _rigidBody2D = null;
 | 
			
		||||
    private List<Vector2D> _vertices = new List<Vector2D>(4);
 | 
			
		||||
 | 
			
		||||
    public IRigidBody2D? RigidBody2D
 | 
			
		||||
    {
 | 
			
		||||
        get
 | 
			
		||||
        {
 | 
			
		||||
            if (_rigidBody2D is null)
 | 
			
		||||
                BehaviourController.TryGetBehaviour(out _rigidBody2D);
 | 
			
		||||
 | 
			
		||||
            return _rigidBody2D;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Action<ICollider2D, ICollider2D>? OnCollisionPreResolve { get; set; } = null;
 | 
			
		||||
 | 
			
		||||
    public Action<IAssignableTransform>? OnTransformAssigned { get => GameObject.OnTransformAssigned; set => GameObject.OnTransformAssigned = value; }
 | 
			
		||||
    ITransform IAssignableTransform.Transform => Transform;
 | 
			
		||||
    public bool Assign(ITransform transform) => GameObject.Assign(transform);
 | 
			
		||||
 | 
			
		||||
    public IList<Vector2D> Vertices => _vertices;
 | 
			
		||||
 | 
			
		||||
    public bool CheckCollision(Vector2D point)
 | 
			
		||||
    {
 | 
			
		||||
        return AABBWorld.Overlaps(point);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void Recalculate()
 | 
			
		||||
    {
 | 
			
		||||
        AABBWorld = new AABB(
 | 
			
		||||
            AABBLocal.LowerBoundary.Scale(Transform.Scale) + Transform.Position,
 | 
			
		||||
            AABBLocal.UpperBoundary.Scale(Transform.Scale) + Transform.Position
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        Vertices.Clear();
 | 
			
		||||
        Vertices.Add(AABBWorld.LowerBoundary);
 | 
			
		||||
        Vertices.Add(new Vector2D(AABBWorld.LowerBoundary.X, AABBWorld.UpperBoundary.Y));
 | 
			
		||||
        Vertices.Add(AABBWorld.UpperBoundary);
 | 
			
		||||
        Vertices.Add(new Vector2D(AABBWorld.UpperBoundary.X, AABBWorld.LowerBoundary.Y));
 | 
			
		||||
    }
 | 
			
		||||
    public Collider2DAABBBehaviour(Vector2D lowerBoundary, Vector2D upperBoundary)
 | 
			
		||||
    {
 | 
			
		||||
        AABBLocal = new AABB(lowerBoundary, upperBoundary);
 | 
			
		||||
        AABBWorld = new AABB(lowerBoundary, upperBoundary);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Collider2DAABBBehaviour()
 | 
			
		||||
    {
 | 
			
		||||
        AABBLocal = new(Vector2D.Zero, Vector2D.Zero);
 | 
			
		||||
        AABBWorld = new(Vector2D.Zero, Vector2D.Zero);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,9 +0,0 @@
 | 
			
		||||
using Microsoft.Xna.Framework;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Physics2D;
 | 
			
		||||
 | 
			
		||||
public record CollisionInformation
 | 
			
		||||
(
 | 
			
		||||
    Vector2 Normal,
 | 
			
		||||
    Vector2 ContactPosition
 | 
			
		||||
);
 | 
			
		||||
@@ -1,75 +0,0 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
using Syntriax.Engine.Core.Abstract;
 | 
			
		||||
using Syntriax.Engine.Physics2D.Abstract;
 | 
			
		||||
using Syntriax.Engine.Physics2D.Primitives;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Physics2D;
 | 
			
		||||
 | 
			
		||||
public class PhysicsEngine2D : IPhysicsEngine2D
 | 
			
		||||
{
 | 
			
		||||
    private List<IRigidBody2D> rigidBodies = new List<IRigidBody2D>(32);
 | 
			
		||||
    private List<ICollider2D> colliders = new List<ICollider2D>(64);
 | 
			
		||||
 | 
			
		||||
    private int _iterationCount = 1;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public int IterationCount { get => _iterationCount; set => _iterationCount = value < 1 ? 1 : value; }
 | 
			
		||||
 | 
			
		||||
    public void AddRigidBody(IRigidBody2D rigidBody)
 | 
			
		||||
    {
 | 
			
		||||
        if (rigidBodies.Contains(rigidBody))
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        rigidBodies.Add(rigidBody);
 | 
			
		||||
 | 
			
		||||
        foreach (var collider2D in rigidBody.BehaviourController.GetBehaviours<ICollider2D>())
 | 
			
		||||
            colliders.Add(collider2D);
 | 
			
		||||
 | 
			
		||||
        rigidBody.BehaviourController.OnBehaviourAdded += OnBehaviourAdded;
 | 
			
		||||
        rigidBody.BehaviourController.OnBehaviourRemoved += OnBehaviourRemoved;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void RemoveRigidBody(IRigidBody2D rigidBody)
 | 
			
		||||
    {
 | 
			
		||||
        rigidBodies.Remove(rigidBody);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void Step(float deltaTime)
 | 
			
		||||
    {
 | 
			
		||||
        float intervalDeltaTime = deltaTime / IterationCount;
 | 
			
		||||
 | 
			
		||||
        for (int iterationIndex = 0; iterationIndex < IterationCount; iterationIndex++)
 | 
			
		||||
        {
 | 
			
		||||
            foreach (var rigidBody in rigidBodies)
 | 
			
		||||
                StepRigidBody(rigidBody, intervalDeltaTime);
 | 
			
		||||
 | 
			
		||||
            foreach (var collider in colliders)
 | 
			
		||||
                collider.Recalculate();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime)
 | 
			
		||||
    {
 | 
			
		||||
        rigidBody.Transform.Position += rigidBody.Velocity * intervalDeltaTime;
 | 
			
		||||
        rigidBody.Transform.Rotation += rigidBody.AngularVelocity * intervalDeltaTime;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void OnBehaviourAdded(IBehaviourController controller, IBehaviour behaviour)
 | 
			
		||||
    {
 | 
			
		||||
        if (behaviour is not ICollider2D collider2D)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        colliders.Add(collider2D);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void OnBehaviourRemoved(IBehaviourController controller, IBehaviour behaviour)
 | 
			
		||||
    {
 | 
			
		||||
        if (behaviour is not ICollider2D collider2D)
 | 
			
		||||
            return;
 | 
			
		||||
 | 
			
		||||
        colliders.Remove(collider2D);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,5 +0,0 @@
 | 
			
		||||
using Syntriax.Engine.Physics2D.Abstract;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Physics2D;
 | 
			
		||||
 | 
			
		||||
public record PhysicsMaterial2D(float Friction, float Restitution) : IPhysicsMaterial2D { }
 | 
			
		||||
@@ -1,6 +0,0 @@
 | 
			
		||||
namespace Syntriax.Engine.Physics2D;
 | 
			
		||||
 | 
			
		||||
public record PhysicsMaterial2DDefault : PhysicsMaterial2D
 | 
			
		||||
{
 | 
			
		||||
    public PhysicsMaterial2DDefault() : base(.1f, .1f) { }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,118 +0,0 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
using Microsoft.Xna.Framework;
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
using Syntriax.Engine.Physics2D.Primitives;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Physics2D;
 | 
			
		||||
 | 
			
		||||
public static class PhysicsMath
 | 
			
		||||
{
 | 
			
		||||
    public static Vector2D Scale(this Vector2D original, Vector2D scale)
 | 
			
		||||
        => new Vector2D(original.X * scale.X, original.Y * scale.Y);
 | 
			
		||||
 | 
			
		||||
    public static Triangle ToSuperTriangle(this IList<Vector2D> vertices)
 | 
			
		||||
    {
 | 
			
		||||
        float minX = float.MaxValue, minY = float.MaxValue;
 | 
			
		||||
        float maxX = float.MinValue, maxY = float.MinValue;
 | 
			
		||||
 | 
			
		||||
        foreach (Vector2D point in vertices)
 | 
			
		||||
        {
 | 
			
		||||
            minX = MathF.Min(minX, point.X);
 | 
			
		||||
            minY = MathF.Min(minY, point.Y);
 | 
			
		||||
            maxX = MathF.Max(maxX, point.X);
 | 
			
		||||
            maxY = MathF.Max(maxY, point.Y);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        float dx = maxX - minX;
 | 
			
		||||
        float dy = maxY - minY;
 | 
			
		||||
        float deltaMax = MathF.Max(dx, dy);
 | 
			
		||||
        float midX = (minX + maxX) / 2;
 | 
			
		||||
        float midY = (minY + maxY) / 2;
 | 
			
		||||
 | 
			
		||||
        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);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static IList<Line> ToLines(this IList<Vector2D> vertices)
 | 
			
		||||
    {
 | 
			
		||||
        List<Line> lines = new List<Line>(vertices.Count - 1);
 | 
			
		||||
        ToLines(vertices, lines);
 | 
			
		||||
        return lines;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void ToLines(this IList<Vector2D> vertices, IList<Line> lines)
 | 
			
		||||
    {
 | 
			
		||||
        lines.Clear();
 | 
			
		||||
        for (int i = 0; i < vertices.Count - 1; i++)
 | 
			
		||||
            lines.Add(new(vertices[i], vertices[i + 1]));
 | 
			
		||||
        lines.Add(new(vertices[^1], vertices[0]));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static bool LaysOn(this Vector2D point, Line line)
 | 
			
		||||
        => line.Resolve(point.X).ApproximatelyEquals(point);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // Given three collinear points p, q, r, the function checks if 
 | 
			
		||||
    // point q lies on line segment 'pr' 
 | 
			
		||||
    public static bool OnSegment(Vector2D p, Vector2D q, Vector2D r)
 | 
			
		||||
    {
 | 
			
		||||
        if (q.X <= MathF.Max(p.X, r.X) && q.X >= MathF.Min(p.X, r.X) &&
 | 
			
		||||
            q.Y <= MathF.Max(p.Y, r.Y) && q.Y >= MathF.Min(p.Y, r.Y))
 | 
			
		||||
            return true;
 | 
			
		||||
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // To find orientation of ordered triplet (p, q, r). 
 | 
			
		||||
    // The function returns following values 
 | 
			
		||||
    // 0 --> p, q and r are collinear 
 | 
			
		||||
    // 1 --> Clockwise 
 | 
			
		||||
    // 2 --> Counterclockwise 
 | 
			
		||||
    public static int Orientation(Vector2D p, Vector2D q, Vector2D r)
 | 
			
		||||
    {
 | 
			
		||||
        // See https://www.geeksforgeeks.org/orientation-3-ordered-points/ 
 | 
			
		||||
        // for details of below formula. 
 | 
			
		||||
        float val = (q.Y - p.Y) * (r.X - q.X) -
 | 
			
		||||
                (q.X - p.X) * (r.Y - q.Y);
 | 
			
		||||
 | 
			
		||||
        if (val == 0) return 0; // collinear 
 | 
			
		||||
 | 
			
		||||
        return (val > 0) ? 1 : 2; // clock or counterclock wise 
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static float IntersectionParameterT(Vector2D p0, Vector2D p1, Vector2D q0, Vector2D q1)
 | 
			
		||||
        => ((q0.X - p0.X) * (p1.Y - p0.Y) - (q0.Y - p0.Y) * (p1.X - p0.X)) /
 | 
			
		||||
            ((q1.Y - q0.Y) * (p1.X - p0.X) - (q1.X - q0.X) * (p1.Y - p0.Y));
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public static bool ApproximatelyEquals(this float a, float b)
 | 
			
		||||
        => ApproximatelyEquals(a, b, float.Epsilon);
 | 
			
		||||
    public static bool ApproximatelyEquals(this Vector2 a, Vector2 b)
 | 
			
		||||
        => ApproximatelyEquals(a, b, float.Epsilon);
 | 
			
		||||
    public static bool ApproximatelyEquals(this Vector2 a, Vector2 b, float epsilon)
 | 
			
		||||
        => ApproximatelyEquals(a.X, b.X, epsilon) && ApproximatelyEquals(a.Y, b.Y, epsilon);
 | 
			
		||||
    public static bool ApproximatelyEquals(this Vector2D a, Vector2D b)
 | 
			
		||||
        => ApproximatelyEquals(a, b, float.Epsilon);
 | 
			
		||||
    public static bool ApproximatelyEquals(this Vector2D a, Vector2D b, float epsilon)
 | 
			
		||||
        => ApproximatelyEquals(a.X, b.X, epsilon) && ApproximatelyEquals(a.Y, b.Y, epsilon);
 | 
			
		||||
    public static bool ApproximatelyEquals(this float a, float b, float epsilon)
 | 
			
		||||
    {
 | 
			
		||||
        if (a == b)
 | 
			
		||||
            return true;
 | 
			
		||||
 | 
			
		||||
        const float floatNormal = (1 << 23) * float.Epsilon;
 | 
			
		||||
        float absA = MathF.Abs(a);
 | 
			
		||||
        float absB = MathF.Abs(b);
 | 
			
		||||
        float diff = MathF.Abs(a - b);
 | 
			
		||||
 | 
			
		||||
        if (a == 0.0f || b == 0.0f || diff < floatNormal)
 | 
			
		||||
            return diff < (epsilon * floatNormal);
 | 
			
		||||
 | 
			
		||||
        return diff / MathF.Min(absA + absB, float.MaxValue) < epsilon;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,17 +0,0 @@
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Physics2D.Primitives;
 | 
			
		||||
 | 
			
		||||
public record AABB(Vector2D LowerBoundary, Vector2D UpperBoundary)
 | 
			
		||||
{
 | 
			
		||||
    public bool Overlaps(Vector2D point)
 | 
			
		||||
        => point.X >= LowerBoundary.X && point.X <= UpperBoundary.X &&
 | 
			
		||||
            point.Y >= LowerBoundary.Y && point.Y <= UpperBoundary.Y;
 | 
			
		||||
 | 
			
		||||
    public bool Overlaps(AABB other)
 | 
			
		||||
        => LowerBoundary.X <= other.UpperBoundary.X && UpperBoundary.X >= other.LowerBoundary.X &&
 | 
			
		||||
            LowerBoundary.Y <= other.UpperBoundary.Y && UpperBoundary.Y >= other.LowerBoundary.Y;
 | 
			
		||||
 | 
			
		||||
    public bool ApproximatelyEquals(AABB other)
 | 
			
		||||
        => LowerBoundary.ApproximatelyEquals(other.LowerBoundary) && UpperBoundary.ApproximatelyEquals(other.UpperBoundary);
 | 
			
		||||
}
 | 
			
		||||
@@ -1,18 +0,0 @@
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Physics2D.Primitives;
 | 
			
		||||
 | 
			
		||||
public record Circle(Vector2D Position, float Radius)
 | 
			
		||||
{
 | 
			
		||||
    public bool Intersects(Circle other)
 | 
			
		||||
    {
 | 
			
		||||
        float distanceSquared = (Position - other.Position).LengthSquared();
 | 
			
		||||
        float radiusSumSquared = Radius * Radius + other.Radius * other.Radius;
 | 
			
		||||
 | 
			
		||||
        return distanceSquared < radiusSumSquared;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public bool Overlaps(Vector2D point) => (Position - point).LengthSquared() <= Radius * Radius;
 | 
			
		||||
    public bool ApproximatelyEquals(Circle other)
 | 
			
		||||
        => Position.ApproximatelyEquals(other.Position) && Radius.ApproximatelyEquals(other.Radius);
 | 
			
		||||
}
 | 
			
		||||
@@ -1,146 +0,0 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Diagnostics.CodeAnalysis;
 | 
			
		||||
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Physics2D.Primitives;
 | 
			
		||||
 | 
			
		||||
public record Line(Vector2D From, Vector2D To)
 | 
			
		||||
{
 | 
			
		||||
    public Line Reversed => new(To, From);
 | 
			
		||||
    public Vector2D Direction => Vector2D.Normalize(To - From);
 | 
			
		||||
    public float Length => (From - To).Length();
 | 
			
		||||
    public float LengthSquared => (From - To).LengthSquared();
 | 
			
		||||
 | 
			
		||||
    public LineEquation LineEquation
 | 
			
		||||
    {
 | 
			
		||||
        get
 | 
			
		||||
        {
 | 
			
		||||
            Vector2D slopeVector = To - From;
 | 
			
		||||
            float slope = slopeVector.Y / slopeVector.X;
 | 
			
		||||
 | 
			
		||||
            float yOffset = From.Y - (slope * From.X);
 | 
			
		||||
 | 
			
		||||
            return new LineEquation(slope, yOffset);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public bool Intersects(Vector2D point)
 | 
			
		||||
        => Resolve(point.X).ApproximatelyEquals(point);
 | 
			
		||||
 | 
			
		||||
    public float GetT(Vector2D point)
 | 
			
		||||
    {
 | 
			
		||||
        float fromX = MathF.Abs(From.X);
 | 
			
		||||
        float toX = MathF.Abs(To.X);
 | 
			
		||||
        float pointX = MathF.Abs(point.X);
 | 
			
		||||
 | 
			
		||||
        float min = MathF.Min(fromX, toX);
 | 
			
		||||
        float max = MathF.Max(fromX, toX) - min;
 | 
			
		||||
 | 
			
		||||
        pointX -= min;
 | 
			
		||||
 | 
			
		||||
        float t = pointX / max;
 | 
			
		||||
 | 
			
		||||
        // FIXME
 | 
			
		||||
        // I don't even know, apparently whatever I wrote up there doesn't take into account of the direction of the line
 | 
			
		||||
        // Which... I can see how, but I am also not sure how I can make it take into account. Or actually I'm for some reason
 | 
			
		||||
        // too unmotivated to find a solution. Future me, find a better way if possible, please.
 | 
			
		||||
        if (!Lerp(t).ApproximatelyEquals(point))
 | 
			
		||||
            return 1f - t;
 | 
			
		||||
        return t;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public bool Exist(List<Vector2D> vertices)
 | 
			
		||||
    {
 | 
			
		||||
        for (int i = 0; i < vertices.Count - 1; i++)
 | 
			
		||||
        {
 | 
			
		||||
            Vector2D vertexCurrent = vertices[i];
 | 
			
		||||
            Vector2D vertexNext = vertices[i];
 | 
			
		||||
            if (From == vertexCurrent && To == vertexNext) return true;
 | 
			
		||||
            if (From == vertexNext && To == vertexCurrent) return true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Vector2D vertexFirst = vertices[0];
 | 
			
		||||
        Vector2D vertexLast = vertices[^1];
 | 
			
		||||
        if (From == vertexFirst && To == vertexLast) return true;
 | 
			
		||||
        if (From == vertexLast && To == vertexFirst) return true;
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public float IntersectionParameterT(Line other)
 | 
			
		||||
    {
 | 
			
		||||
        float numerator = (From.X - other.From.X) * (other.From.Y - other.To.Y) - (From.Y - other.From.Y) * (other.From.X - other.To.X);
 | 
			
		||||
        float denominator = (From.X - To.X) * (other.From.Y - other.To.Y) - (From.Y - To.Y) * (other.From.X - other.To.X);
 | 
			
		||||
 | 
			
		||||
        // Lines are parallel
 | 
			
		||||
        if (denominator == 0)
 | 
			
		||||
            return float.NaN;
 | 
			
		||||
 | 
			
		||||
        return numerator / denominator;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Vector2D Lerp(float t)
 | 
			
		||||
        => new Vector2D(
 | 
			
		||||
            From.X + (To.X - From.X) * t,
 | 
			
		||||
            From.Y + (To.Y - From.Y) * t
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
    public Vector2D Resolve(float x)
 | 
			
		||||
        => new Vector2D(x, LineEquation.Resolve(x));
 | 
			
		||||
 | 
			
		||||
    public Vector2D ClosestPointTo(Vector2D point)
 | 
			
		||||
    {
 | 
			
		||||
        // Convert edge points to vectors
 | 
			
		||||
        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);
 | 
			
		||||
 | 
			
		||||
        // Clamp t to the range [0, 1] to ensure the closest point is on the edge
 | 
			
		||||
        t = MathF.Max(0, MathF.Min(1, t));
 | 
			
		||||
 | 
			
		||||
        // Calculate the closest point on the edge
 | 
			
		||||
        float closestX = From.X + t * edgeVector.X;
 | 
			
		||||
        float closestY = From.Y + t * edgeVector.Y;
 | 
			
		||||
 | 
			
		||||
        return new Vector2D((float)closestX, (float)closestY);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Vector2D IntersectionPoint(Line other)
 | 
			
		||||
        => Vector2D.Lerp(From, To, IntersectionParameterT(other));
 | 
			
		||||
 | 
			
		||||
    public bool Intersects(Line other)
 | 
			
		||||
    {
 | 
			
		||||
        int o1 = PhysicsMath.Orientation(From, To, other.From);
 | 
			
		||||
        int o2 = PhysicsMath.Orientation(From, To, other.To);
 | 
			
		||||
        int o3 = PhysicsMath.Orientation(other.From, other.To, From);
 | 
			
		||||
        int o4 = PhysicsMath.Orientation(other.From, other.To, To);
 | 
			
		||||
 | 
			
		||||
        if (o1 != o2 && o3 != o4)
 | 
			
		||||
            return true;
 | 
			
		||||
 | 
			
		||||
        if (o1 == 0 && PhysicsMath.OnSegment(From, other.From, To)) return true;
 | 
			
		||||
        if (o2 == 0 && PhysicsMath.OnSegment(From, other.To, To)) return true;
 | 
			
		||||
        if (o3 == 0 && PhysicsMath.OnSegment(other.From, From, other.To)) return true;
 | 
			
		||||
        if (o4 == 0 && PhysicsMath.OnSegment(other.From, To, other.To)) return true;
 | 
			
		||||
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public bool Intersects(Line other, [NotNullWhen(returnValue: true)] out Vector2D? point)
 | 
			
		||||
    {
 | 
			
		||||
        point = null;
 | 
			
		||||
 | 
			
		||||
        bool result = Intersects(other);
 | 
			
		||||
 | 
			
		||||
        if (result)
 | 
			
		||||
            point = IntersectionPoint(other);
 | 
			
		||||
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public bool ApproximatelyEquals(Line other)
 | 
			
		||||
        => From.ApproximatelyEquals(other.From) && To.ApproximatelyEquals(other.To);
 | 
			
		||||
}
 | 
			
		||||
@@ -1,8 +0,0 @@
 | 
			
		||||
namespace Syntriax.Engine.Physics2D.Primitives;
 | 
			
		||||
 | 
			
		||||
public record LineEquation(float Slope, float OffsetY)
 | 
			
		||||
{
 | 
			
		||||
    public float Resolve(float x) => Slope * x + OffsetY; // y = mx + b
 | 
			
		||||
    public bool ApproximatelyEquals(LineEquation other)
 | 
			
		||||
        => Slope.ApproximatelyEquals(other.Slope) && OffsetY.ApproximatelyEquals(other.OffsetY);
 | 
			
		||||
}
 | 
			
		||||
@@ -1,9 +0,0 @@
 | 
			
		||||
namespace Syntriax.Engine.Physics2D.Primitives;
 | 
			
		||||
 | 
			
		||||
public static class Math
 | 
			
		||||
{
 | 
			
		||||
    public const float RadianToDegree = 57.29577866666166f;
 | 
			
		||||
    public const float DegreeToRadian = 0.01745329277777778f;
 | 
			
		||||
 | 
			
		||||
    public static float Clamp(float value, float min, float max) => (value < min) ? min : (value > max) ? max : value;
 | 
			
		||||
}
 | 
			
		||||
@@ -1,68 +0,0 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Physics2D.Primitives;
 | 
			
		||||
 | 
			
		||||
public record Shape(IList<Vector2D> Vertices)
 | 
			
		||||
{
 | 
			
		||||
    public Triangle SuperTriangle
 | 
			
		||||
    {
 | 
			
		||||
        get
 | 
			
		||||
        {
 | 
			
		||||
            float minX = float.MaxValue, minY = float.MaxValue;
 | 
			
		||||
            float maxX = float.MinValue, maxY = float.MinValue;
 | 
			
		||||
 | 
			
		||||
            foreach (Vector2D point in Vertices)
 | 
			
		||||
            {
 | 
			
		||||
                minX = MathF.Min(minX, point.X);
 | 
			
		||||
                minY = MathF.Min(minY, point.Y);
 | 
			
		||||
                maxX = MathF.Max(maxX, point.X);
 | 
			
		||||
                maxY = MathF.Max(maxY, point.Y);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            float dx = maxX - minX;
 | 
			
		||||
            float dy = maxY - minY;
 | 
			
		||||
            float deltaMax = MathF.Max(dx, dy);
 | 
			
		||||
            float midX = (minX + maxX) / 2;
 | 
			
		||||
            float midY = (minY + maxY) / 2;
 | 
			
		||||
 | 
			
		||||
            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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public List<Line> Lines
 | 
			
		||||
    {
 | 
			
		||||
        get
 | 
			
		||||
        {
 | 
			
		||||
            List<Line> lines = new List<Line>(Vertices.Count - 1);
 | 
			
		||||
            GetLinesNonAlloc(lines);
 | 
			
		||||
            return lines;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void GetLinesNonAlloc(IList<Line> lines)
 | 
			
		||||
    {
 | 
			
		||||
        lines.Clear();
 | 
			
		||||
        for (int i = 0; i < Vertices.Count - 1; i++)
 | 
			
		||||
            lines.Add(new(Vertices[i], Vertices[i + 1]));
 | 
			
		||||
        lines.Add(new(Vertices[^1], Vertices[0]));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public bool ApproximatelyEquals(Shape other)
 | 
			
		||||
    {
 | 
			
		||||
        if (Vertices.Count != other.Vertices.Count)
 | 
			
		||||
            return false;
 | 
			
		||||
 | 
			
		||||
        for (int i = 0; i < Vertices.Count; i++)
 | 
			
		||||
            if (!Vertices[i].ApproximatelyEquals(other.Vertices[i]))
 | 
			
		||||
                return false;
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,54 +0,0 @@
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Physics2D.Primitives;
 | 
			
		||||
 | 
			
		||||
public record Triangle(Vector2D A, Vector2D B, Vector2D C)
 | 
			
		||||
{
 | 
			
		||||
    public float Area => MathF.Abs((
 | 
			
		||||
            A.X * (B.Y - C.Y) +
 | 
			
		||||
            B.X * (C.Y - A.Y) +
 | 
			
		||||
            C.X * (A.Y - B.Y)
 | 
			
		||||
            ) * .5f);
 | 
			
		||||
 | 
			
		||||
    public Circle CircumCircle
 | 
			
		||||
    {
 | 
			
		||||
        get
 | 
			
		||||
        {
 | 
			
		||||
            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);
 | 
			
		||||
 | 
			
		||||
            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 Vector2D(x, y);
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
                center = (midAB + midBC) * .5f;
 | 
			
		||||
 | 
			
		||||
            return new(center, Vector2D.Distance(center, A));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public bool Overlaps(Vector2D point)
 | 
			
		||||
    {
 | 
			
		||||
        float originalTriangleArea = Area;
 | 
			
		||||
 | 
			
		||||
        float pointTriangleArea1 = new Triangle(point, B, C).Area;
 | 
			
		||||
        float pointTriangleArea2 = new Triangle(A, point, C).Area;
 | 
			
		||||
        float pointTriangleArea3 = new Triangle(A, B, point).Area;
 | 
			
		||||
 | 
			
		||||
        float pointTriangleAreasSum = pointTriangleArea1 + pointTriangleArea2 + pointTriangleArea3;
 | 
			
		||||
 | 
			
		||||
        return originalTriangleArea.ApproximatelyEquals(pointTriangleAreasSum, float.Epsilon * 3f);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public bool ApproximatelyEquals(Triangle other)
 | 
			
		||||
        => A.ApproximatelyEquals(other.A) && B.ApproximatelyEquals(other.B) && C.ApproximatelyEquals(other.C);
 | 
			
		||||
}
 | 
			
		||||
@@ -1,24 +0,0 @@
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
using Syntriax.Engine.Core;
 | 
			
		||||
using Syntriax.Engine.Core.Abstract;
 | 
			
		||||
using Syntriax.Engine.Physics2D.Abstract;
 | 
			
		||||
 | 
			
		||||
namespace Syntriax.Engine.Physics2D;
 | 
			
		||||
 | 
			
		||||
public class RigidBody2D : BehaviourOverride, IRigidBody2D
 | 
			
		||||
{
 | 
			
		||||
    public Action<IAssignableTransform>? OnTransformAssigned { get => GameObject.OnTransformAssigned; set => GameObject.OnTransformAssigned = value; }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public IPhysicsMaterial2D Material { get; set; } = new PhysicsMaterial2DDefault();
 | 
			
		||||
 | 
			
		||||
    public Vector2D Velocity { get; set; } = Vector2D.Zero;
 | 
			
		||||
    public float AngularVelocity { get; set; } = 0f;
 | 
			
		||||
    public float Mass { get; set; } = 0f;
 | 
			
		||||
 | 
			
		||||
    ITransform IAssignableTransform.Transform => Transform;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public bool Assign(ITransform transform) => GameObject.Assign(transform);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								Pong.sln
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								Pong.sln
									
									
									
									
									
								
							@@ -11,7 +11,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Game", "Game\Game.csproj",
 | 
			
		||||
EndProject
 | 
			
		||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Engine.Input", "Engine\Engine.Input\Engine.Input.csproj", "{7EED4EC3-79D5-4C6C-A54D-1B396213C0E4}"
 | 
			
		||||
EndProject
 | 
			
		||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Engine.Graphics", "Engine\Engine.Graphics\Engine.Graphics.csproj", "{7371D9AF-6F4D-4F05-8458-197C4EE66B01}"
 | 
			
		||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Engine.Physics2D", "Engine\Engine.Physics2D\Engine.Physics2D.csproj", "{0D97F83C-B043-48B1-B155-7354C4E84FC0}"
 | 
			
		||||
EndProject
 | 
			
		||||
Global
 | 
			
		||||
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 | 
			
		||||
@@ -34,14 +34,14 @@ Global
 | 
			
		||||
		{7EED4EC3-79D5-4C6C-A54D-1B396213C0E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
 | 
			
		||||
		{7EED4EC3-79D5-4C6C-A54D-1B396213C0E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
 | 
			
		||||
		{7EED4EC3-79D5-4C6C-A54D-1B396213C0E4}.Release|Any CPU.Build.0 = Release|Any CPU
 | 
			
		||||
		{7371D9AF-6F4D-4F05-8458-197C4EE66B01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 | 
			
		||||
		{7371D9AF-6F4D-4F05-8458-197C4EE66B01}.Debug|Any CPU.Build.0 = Debug|Any CPU
 | 
			
		||||
		{7371D9AF-6F4D-4F05-8458-197C4EE66B01}.Release|Any CPU.ActiveCfg = Release|Any CPU
 | 
			
		||||
		{7371D9AF-6F4D-4F05-8458-197C4EE66B01}.Release|Any CPU.Build.0 = Release|Any CPU
 | 
			
		||||
		{0D97F83C-B043-48B1-B155-7354C4E84FC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
 | 
			
		||||
		{0D97F83C-B043-48B1-B155-7354C4E84FC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
 | 
			
		||||
		{0D97F83C-B043-48B1-B155-7354C4E84FC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
 | 
			
		||||
		{0D97F83C-B043-48B1-B155-7354C4E84FC0}.Release|Any CPU.Build.0 = Release|Any CPU
 | 
			
		||||
	EndGlobalSection
 | 
			
		||||
	GlobalSection(NestedProjects) = preSolution
 | 
			
		||||
		{990CA10C-1EBB-4395-A43A-456B7029D8C9} = {F7F62670-237A-4C93-A30E-CE661C6FC401}
 | 
			
		||||
		{7EED4EC3-79D5-4C6C-A54D-1B396213C0E4} = {F7F62670-237A-4C93-A30E-CE661C6FC401}
 | 
			
		||||
		{7371D9AF-6F4D-4F05-8458-197C4EE66B01} = {F7F62670-237A-4C93-A30E-CE661C6FC401}
 | 
			
		||||
		{0D97F83C-B043-48B1-B155-7354C4E84FC0} = {F7F62670-237A-4C93-A30E-CE661C6FC401}
 | 
			
		||||
	EndGlobalSection
 | 
			
		||||
EndGlobal
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user