diff --git a/Game/Game1.cs b/Game/Game1.cs index ceef5ae..7fa96c7 100644 --- a/Game/Game1.cs +++ b/Game/Game1.cs @@ -11,6 +11,7 @@ using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Graphics.TwoDimensional; using Syntriax.Engine.Input; using Syntriax.Engine.Physics2D; +using Syntriax.Engine.Physics2D.Primitives; namespace Pong; @@ -64,7 +65,7 @@ public class Game1 : Game gameObjectBall.Transform.Position = Vector2.Zero; gameObjectBall.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f); engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour()); - gameObjectBall.BehaviourController.AddBehaviour(new Vector2(.1f, .01f), 500f); + gameObjectBall.BehaviourController.AddBehaviour(new Vector2(.1f, .1f), 500f); gameObjectBall.BehaviourController.AddBehaviour().Assign(spriteBall); gameObjectBall.BehaviourController.AddBehaviour().AABBLocal = new AABB(-Vector2.One * 512f * .5f, Vector2.One * 512f * .5f); // gameObjectBall = gameManager.InstantiateGameObject(); @@ -76,25 +77,25 @@ public class Game1 : Game // gameObjectBall.BehaviourController.AddBehaviour().Assign(spriteBall); // gameObjectBall.BehaviourController.AddBehaviour().AABBLocal = new AABB(-Vector2.One * 512f * .5f, Vector2.One * 512f * .5f); - IGameObject gameObjectLeft = gameManager.InstantiateGameObject(); - gameObjectLeft.Name = "Left"; - gameObjectLeft.Transform.Position = new Vector2(-452, 0f); - gameObjectLeft.Transform.Scale = new Vector2(10f, 40f); - gameObjectLeft.BehaviourController.AddBehaviour(); - gameObjectLeft.BehaviourController.AddBehaviour(Keys.W, Keys.S, 268f, -268f, 400f); - gameObjectLeft.BehaviourController.AddBehaviour().Assign(spriteBox); - gameObjectLeft.BehaviourController.AddBehaviour().AABBLocal = new AABB(-Vector2.One * .5f, Vector2.One * .5f); - engine.AddRigidBody(gameObjectLeft.BehaviourController.AddBehaviour()); + // IGameObject gameObjectLeft = gameManager.InstantiateGameObject(); + // gameObjectLeft.Name = "Left"; + // gameObjectLeft.Transform.Position = new Vector2(-452, 0f); + // gameObjectLeft.Transform.Scale = new Vector2(10f, 40f); + // gameObjectLeft.BehaviourController.AddBehaviour(); + // gameObjectLeft.BehaviourController.AddBehaviour(Keys.W, Keys.S, 268f, -268f, 400f); + // gameObjectLeft.BehaviourController.AddBehaviour().Assign(spriteBox); + // gameObjectLeft.BehaviourController.AddBehaviour().AABBLocal = new AABB(-Vector2.One * .5f, Vector2.One * .5f); + // engine.AddRigidBody(gameObjectLeft.BehaviourController.AddBehaviour()); - IGameObject gameObjectRight = gameManager.InstantiateGameObject(); - gameObjectRight.Name = "Right"; - gameObjectRight.Transform.Position = new Vector2(452, 0f); - gameObjectRight.Transform.Scale = new Vector2(10f, 40f); - gameObjectRight.BehaviourController.AddBehaviour(); - gameObjectRight.BehaviourController.AddBehaviour(Keys.Up, Keys.Down, 268f, -268f, 400f); - gameObjectRight.BehaviourController.AddBehaviour().Assign(spriteBox); - gameObjectRight.BehaviourController.AddBehaviour().AABBLocal = new AABB(-Vector2.One * .5f, Vector2.One * .5f); - engine.AddRigidBody(gameObjectRight.BehaviourController.AddBehaviour()); + // IGameObject gameObjectRight = gameManager.InstantiateGameObject(); + // gameObjectRight.Name = "Right"; + // gameObjectRight.Transform.Position = new Vector2(452, 0f); + // gameObjectRight.Transform.Scale = new Vector2(10f, 40f); + // gameObjectRight.BehaviourController.AddBehaviour(); + // gameObjectRight.BehaviourController.AddBehaviour(Keys.Up, Keys.Down, 268f, -268f, 400f); + // gameObjectRight.BehaviourController.AddBehaviour().Assign(spriteBox); + // gameObjectRight.BehaviourController.AddBehaviour().AABBLocal = new AABB(-Vector2.One * .5f, Vector2.One * .5f); + // engine.AddRigidBody(gameObjectRight.BehaviourController.AddBehaviour()); IGameObject goPlayAreaTop = gameManager.InstantiateGameObject(); diff --git a/Game/Physics2D/PhysicsEngine2D.cs b/Game/Physics2D/PhysicsEngine2D.cs index 029686f..e1acfb6 100644 --- a/Game/Physics2D/PhysicsEngine2D.cs +++ b/Game/Physics2D/PhysicsEngine2D.cs @@ -65,7 +65,7 @@ public class PhysicsEngine2D : IPhysicsEngine2D if (c1.RigidBody2D is not IRigidBody2D) return; - Line vertexTrajectory = new Line(vertex, vertex - c1.RigidBody2D.Velocity * intervalDeltaTime); + Line vertexTrajectory = new Line(vertex - c1.RigidBody2D.Velocity * intervalDeltaTime, vertex); if (vertexTrajectory.LengthSquared <= 0.001f) return; @@ -96,7 +96,7 @@ public class PhysicsEngine2D : IPhysicsEngine2D if (!hasIntersectedWithLines) return; - float rewindInterval = t * intervalDeltaTime; + float rewindInterval = intervalDeltaTime - t * intervalDeltaTime; StepRigidBody(c1.RigidBody2D, -rewindInterval); c1.RigidBody2D.Velocity = Vector2.Reflect(c1.RigidBody2D.Velocity, normal); @@ -115,7 +115,13 @@ public class PhysicsEngine2D : IPhysicsEngine2D { Vector2 vertex = collider2D.Vertices[y]; if (collider2DItem.CheckCollision(vertex)) + { OnCollisionDetectedAction?.Invoke(collider2D, collider2DItem, vertex); + OnCollisionDetectedAction?.Invoke(collider2DItem, collider2D, vertex); + collider2D.Recalculate(); + collider2DItem.Recalculate(); + y--; + } } } } diff --git a/Game/Physics2D/Primitives/Line.cs b/Game/Physics2D/Primitives/Line.cs index 5553497..02cc06f 100644 --- a/Game/Physics2D/Primitives/Line.cs +++ b/Game/Physics2D/Primitives/Line.cs @@ -8,6 +8,7 @@ namespace Syntriax.Engine.Physics2D.Primitives; public record Line(Vector2 From, Vector2 To) { + public Line Reversed => new(To, From); public Vector2 Direction => Vector2.Normalize(To - From); public float Length => (From - To).Length(); public float LengthSquared => (From - To).LengthSquared(); @@ -39,7 +40,15 @@ public record Line(Vector2 From, Vector2 To) pointX -= min; - return pointX / max; + 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 vertices) @@ -60,9 +69,22 @@ public record Line(Vector2 From, Vector2 To) } public float IntersectionParameterT(Line other) - => ((other.From.X - From.X) * (To.Y - From.Y) - (other.From.Y - From.Y) * (To.X - From.X)) / - ((other.To.Y - other.From.Y) * (To.X - From.X) - (other.To.X - other.From.X) * (To.Y - From.Y)); + { + 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 Vector2 Lerp(float t) + => new Vector2( + From.X + (To.X - From.X) * t, + From.Y + (To.Y - From.Y) * t + ); public Vector2 Resolve(float x) => new Vector2(x, LineEquation.Resolve(x));