From b8706e82d8546271cc2690ad709a9c26d03b65cc Mon Sep 17 00:00:00 2001 From: Syntriax Date: Tue, 5 Dec 2023 14:47:17 +0300 Subject: [PATCH] Tetstests --- Game/Game1.cs | 17 +++++++- Game/Physics2D/Collider2DBehaviour.cs | 61 +++++++++++++++++++++------ Game/Physics2D/PhysicsEngine2D.cs | 44 +++++++++++++++++-- 3 files changed, 104 insertions(+), 18 deletions(-) diff --git a/Game/Game1.cs b/Game/Game1.cs index b7db248..43c697e 100644 --- a/Game/Game1.cs +++ b/Game/Game1.cs @@ -112,6 +112,12 @@ public class Game1 : Game goPlayAreaLeft.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]); engine.AddRigidBody(goPlayAreaLeft.BehaviourController.AddBehaviour()); + // IGameObject goPlayAreaCenter = gameManager.InstantiateGameObject(); + // goPlayAreaCenter.Transform.Position = new Vector2(100f, 100f); + // goPlayAreaCenter.Transform.Scale = new Vector2(40f, 40f); + // // goPlayAreaCenter.BehaviourController.AddBehaviour().Assign(spriteBox); + // goPlayAreaCenter.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]); + // engine.AddRigidBody(goPlayAreaCenter.BehaviourController.AddBehaviour()); // TODO: use this.Content to load your game content here } @@ -150,8 +156,17 @@ public class Game1 : Game if (Keyboard.GetState().IsKeyDown(Keys.Space)) seconds += gameTime.ElapsedGameTime.Milliseconds * .0005f; + if (Keyboard.GetState().IsKeyDown(Keys.B)) + { + seconds -= gameTime.ElapsedGameTime.Milliseconds * .0005f; + while (physicsTimer - 0.01f > seconds) + { + Console.WriteLine($"Physics Timer: {physicsTimer}"); + physicsTimer -= 0.01f; + engine.Step(-.01f); + } + } - // TODO: Add your update logic here while (physicsTimer + 0.01f < seconds) { Console.WriteLine($"Physics Timer: {physicsTimer}"); diff --git a/Game/Physics2D/Collider2DBehaviour.cs b/Game/Physics2D/Collider2DBehaviour.cs index f2b0c5c..4908688 100644 --- a/Game/Physics2D/Collider2DBehaviour.cs +++ b/Game/Physics2D/Collider2DBehaviour.cs @@ -64,15 +64,12 @@ public class Collider2DBehaviour(IList vertices) : BehaviourOverride, I Vector2 contactPoint = ClosestPointOnEdge(point, edge); Vector2 normal = contactPoint - point; + if (normal.LengthSquared() < 0.001f) + normal = new Vector2(0f, 1f); + normal.Normalize(); collisionInformation = new CollisionInformation(normal, contactPoint); - - GameObject gameObject = Game1.gameManager.InstantiateGameObject(); - gameObject.BehaviourController.AddBehaviour().Assign(Game1.spriteBox); - gameObject.Transform.Position = point; - gameObject.Transform.Scale = new Vector2(1f, .01f) * 100f; - gameObject.Transform.Rotation = (float)Math.Atan2(normal.Y, normal.X); break; } return true; @@ -142,18 +139,54 @@ public class Collider2DBehaviour(IList vertices) : BehaviourOverride, I triangles.RemoveAt(i); } - // for (int i = 0; i < triangles.Count; i++) - // { - // Triangle triangle = triangles[i]; + for (int i = gameObjects.Count - 1; i >= 0; i--) + { + IGameObject gameObject = gameObjects[i]; + Game1.gameManager.RemoveGameObject(gameObject); + gameObjects.RemoveAt(i); + } - // triangle.A += Transform.Position; - // triangle.B += Transform.Position; - // triangle.C += Transform.Position; - // triangles[i] = triangle; - // } + for (int i = 0; i < triangles.Count; i++) + { + Triangle triangle = triangles[i]; + foreach (var edge in GetEdges(triangle)) + { + GameObject gameObject = Game1.gameManager.InstantiateGameObject(); + DisplayableSpriteBehaviour displayableSpriteBehaviour = gameObject.BehaviourController.AddBehaviour(); + displayableSpriteBehaviour.Color = Color.Aqua; + displayableSpriteBehaviour.Origin = new(0.5f, 1f); + displayableSpriteBehaviour.Assign(Game1.spriteBox); + gameObject.Transform.Position = edge.A; + Vector2 vector2 = edge.B - edge.A; + gameObject.Transform.Scale = new Vector2(2f, .0f) + Vector2.UnitY * vector2.Length(); + gameObject.Transform.Rotation = (float)Math.Atan2(vector2.X, vector2.Y); + gameObjects.Add(gameObject); + + gameObject = Game1.gameManager.InstantiateGameObject(); + displayableSpriteBehaviour = gameObject.BehaviourController.AddBehaviour(); + displayableSpriteBehaviour.Color = Color.Crimson; + displayableSpriteBehaviour.Assign(Game1.spriteBox); + gameObject.Transform.Position = edge.B; + gameObject.Transform.Scale = new Vector2(4f, 4f); + gameObjects.Add(gameObject); + } + } + + foreach (var vertex in Vertices) + { + GameObject gameObject = Game1.gameManager.InstantiateGameObject(); + DisplayableSpriteBehaviour displayableSpriteBehaviour = gameObject.BehaviourController.AddBehaviour(); + displayableSpriteBehaviour.Color = Color.GreenYellow; + displayableSpriteBehaviour.Assign(Game1.spriteBox); + gameObject.Transform.Position = vertex; + gameObject.Transform.Scale = new Vector2(2f, 2f); + gameObjects.Add(gameObject); + } } + private List gameObjects = new List(32); + private Vector2 ClosestPointOnEdge(Vector2 point, Edge edge) { // Convert edge points to vectors diff --git a/Game/Physics2D/PhysicsEngine2D.cs b/Game/Physics2D/PhysicsEngine2D.cs index 2b1997c..ad4a36b 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; using Syntriax.Engine.Physics2D.Abstract; namespace Syntriax.Engine.Physics2D; @@ -54,10 +57,10 @@ public class PhysicsEngine2D : IPhysicsEngine2D for (int colliderIY = colliderIX + 1; colliderIY < colliders.Count; colliderIY++) for (int verticesIndex = 0; verticesIndex < colliderX.Vertices.Count; verticesIndex++) { - if (!colliders[colliderIY].CheckCollision(colliderX.Vertices[verticesIndex], colliderX, out var collisionInformation)) + ICollider2D colliderY = colliders[colliderIY]; + if (!colliderY.CheckCollision(colliderX.Vertices[verticesIndex], colliderX, out var collisionInformation)) continue; - ICollider2D colliderY = colliders[colliderIY]; if (colliderX.BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidX)) { Vector2 xVertex = colliderX.Vertices[verticesIndex]; @@ -68,10 +71,45 @@ public class PhysicsEngine2D : IPhysicsEngine2D rigidX.Velocity = Vector2.Reflect(rigidX.Velocity, collisionInformation.Normal); rigidX.Transform.Position -= xVertex - vertexNewPosition; + // { + // GameObject gameObject = Game1.gameManager.InstantiateGameObject(); + // DisplayableSpriteBehaviour displayableSpriteBehaviour = gameObject.BehaviourController.AddBehaviour(); + // displayableSpriteBehaviour.Assign(Game1.spriteBox); + // gameObject.Transform.Position = collisionInformation.ContactPosition; + // gameObject.Transform.Scale = new Vector2(1f, .01f) * 100f; + // gameObject.Transform.Rotation = (float)Math.Atan2(collisionInformation.Normal.Y, collisionInformation.Normal.X); + // } + // { + // GameObject gameObject = Game1.gameManager.InstantiateGameObject(); + // DisplayableSpriteBehaviour displayableSpriteBehaviour = gameObject.BehaviourController.AddBehaviour(); + // displayableSpriteBehaviour.Color = Color.Aqua; + // displayableSpriteBehaviour.Assign(Game1.spriteBox); + // gameObject.Transform.Position = collisionInformation.ContactPosition; + // gameObject.Transform.Scale = new Vector2(1f, .01f) * 100f; + // gameObject.Transform.Rotation = (float)Math.Atan2(rigidX.Velocity.Y, rigidX.Velocity.X); + // } + // { + // GameObject gameObject = Game1.gameManager.InstantiateGameObject(); + // DisplayableSpriteBehaviour displayableSpriteBehaviour = gameObject.BehaviourController.AddBehaviour(); + // displayableSpriteBehaviour.Color = Color.Pink; + // displayableSpriteBehaviour.Assign(Game1.spriteBox); + // gameObject.Transform.Position = collisionInformation.ContactPosition; + // gameObject.Transform.Scale = new Vector2(1f, .01f) * 100f; + // gameObject.Transform.Rotation = (float)Math.Atan2(edgeDirection.Y, edgeDirection.X); + // } + // { + // GameObject gameObject = Game1.gameManager.InstantiateGameObject(); + // DisplayableSpriteBehaviour displayableSpriteBehaviour = gameObject.BehaviourController.AddBehaviour(); + // displayableSpriteBehaviour.Color = Color.DarkGoldenrod; + // displayableSpriteBehaviour.Assign(Game1.spriteBox); + // gameObject.Transform.Position = collisionInformation.ContactPosition; + // gameObject.Transform.Scale = new Vector2(1f, .01f) * 100f; + // gameObject.Transform.Rotation = (float)Math.Atan2((xVertex - p1).Y, (xVertex - p1).X); + // } + StepRigidBody(rigidX, intervalDeltaTime * (1f - t)); colliders[colliderIX].RecalculateVertices(); } - Console.WriteLine($"/////////////////////////////////////////////"); // Console.WriteLine($"Collision"); }