From 4692b4e25ebd093360c773dca682a67d966c6fba Mon Sep 17 00:00:00 2001 From: Syntriax Date: Tue, 5 Dec 2023 15:27:31 +0300 Subject: [PATCH] afgdggdfga --- Game/Game1.cs | 43 +++++++++++++++----------- Game/Physics2D/Collider2DBehaviour.cs | 44 +++++++++++++-------------- Game/Physics2D/PhysicsEngine2D.cs | 2 ++ 3 files changed, 49 insertions(+), 40 deletions(-) diff --git a/Game/Game1.cs b/Game/Game1.cs index 43c697e..f28e575 100644 --- a/Game/Game1.cs +++ b/Game/Game1.cs @@ -56,7 +56,7 @@ public class Game1 : Game gameManager.Camera = gameObjectCamera.BehaviourController.AddBehaviour(); gameManager.Camera.Viewport = GraphicsDevice.Viewport; - IGameObject gameObjectBall = gameManager.InstantiateGameObject(); + gameObjectBall = gameManager.InstantiateGameObject(); gameObjectBall.Name = "Ball"; gameObjectBall.Transform.Position = Vector2.Zero; gameObjectBall.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f); @@ -89,26 +89,26 @@ public class Game1 : Game IGameObject goPlayAreaTop = gameManager.InstantiateGameObject(); goPlayAreaTop.Transform.Position = new Vector2(0f, 288f + 20f); goPlayAreaTop.Transform.Scale = new Vector2(10240f, 40f); - goPlayAreaTop.BehaviourController.AddBehaviour().Assign(spriteBox); + // goPlayAreaTop.BehaviourController.AddBehaviour().Assign(spriteBox); goPlayAreaTop.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]); engine.AddRigidBody(goPlayAreaTop.BehaviourController.AddBehaviour()); IGameObject goPlayAreaBottom = gameManager.InstantiateGameObject(); goPlayAreaBottom.Transform.Position = new Vector2(0f, -(288f + 20f)); goPlayAreaBottom.Transform.Scale = new Vector2(10240f, 40f); - goPlayAreaBottom.BehaviourController.AddBehaviour().Assign(spriteBox); + // goPlayAreaBottom.BehaviourController.AddBehaviour().Assign(spriteBox); goPlayAreaBottom.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]); engine.AddRigidBody(goPlayAreaBottom.BehaviourController.AddBehaviour()); IGameObject goPlayAreaRight = gameManager.InstantiateGameObject(); goPlayAreaRight.Transform.Position = new Vector2(512f + 20f, 0f); goPlayAreaRight.Transform.Scale = new Vector2(40f, 5760f); - goPlayAreaRight.BehaviourController.AddBehaviour().Assign(spriteBox); + // goPlayAreaRight.BehaviourController.AddBehaviour().Assign(spriteBox); goPlayAreaRight.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]); engine.AddRigidBody(goPlayAreaRight.BehaviourController.AddBehaviour()); IGameObject goPlayAreaLeft = gameManager.InstantiateGameObject(); goPlayAreaLeft.Transform.Position = new Vector2(-(512f + 20f), 0f); goPlayAreaLeft.Transform.Scale = new Vector2(40f, 5760f); - goPlayAreaLeft.BehaviourController.AddBehaviour().Assign(spriteBox); + // goPlayAreaLeft.BehaviourController.AddBehaviour().Assign(spriteBox); 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()); @@ -154,20 +154,23 @@ public class Game1 : Game gameManager.Camera.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.000025f; - 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); - } - } + // if (Keyboard.GetState().IsKeyDown(Keys.N)) + // seconds = 39.12f; + // if (Keyboard.GetState().IsKeyDown(Keys.Space)) + // seconds += gameTime.ElapsedGameTime.Milliseconds * .005f; + // if (Keyboard.GetState().IsKeyDown(Keys.B)) + // { + // seconds -= gameTime.ElapsedGameTime.Milliseconds * .005f; + // while (physicsTimer - 0.01f > seconds) + // { + // Console.WriteLine($"Physics Timer: {physicsTimer}"); + // physicsTimer -= 0.01f; + // engine.Step(-.01f); + // } + // } - while (physicsTimer + 0.01f < seconds) + + while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds) { Console.WriteLine($"Physics Timer: {physicsTimer}"); physicsTimer += 0.01f; @@ -178,11 +181,15 @@ public class Game1 : Game } static float physicsTimer = 0f; static float seconds = 0f; + private GameObject gameObjectBall; protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(new Color() { R = 32, G = 32, B = 32 }); + // gameManager.Camera.Position = gameObjectBall.Transform.Position; + // Console.WriteLine($"Pos: {gameManager.Camera.Position}"); + // TODO: Add your drawing code here gameManager.PreDraw(gameTime); gameManager.Camera.Update(); diff --git a/Game/Physics2D/Collider2DBehaviour.cs b/Game/Physics2D/Collider2DBehaviour.cs index 4908688..b71af97 100644 --- a/Game/Physics2D/Collider2DBehaviour.cs +++ b/Game/Physics2D/Collider2DBehaviour.cs @@ -194,16 +194,16 @@ public class Collider2DBehaviour(IList vertices) : BehaviourOverride, I var pointVector = new Vector2(point.X - edge.A.X, point.Y - edge.A.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); + double 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 = Math.Max(0, Math.Min(1, t)); // Calculate the closest point on the edge - float closestX = edge.A.X + t * edgeVector.X; - float closestY = edge.A.Y + t * edgeVector.Y; + double closestX = edge.A.X + t * edgeVector.X; + double closestY = edge.A.Y + t * edgeVector.Y; - return new Vector2(closestX, closestY); + return new Vector2((float)closestX, (float)closestY); } private bool DoesEdgeExistInTriangles(Edge edge, List triangles) @@ -226,8 +226,8 @@ public class Collider2DBehaviour(IList vertices) : BehaviourOverride, I private Triangle GetSuperTriangle(IReadOnlyList vertices) { - float minX = float.MaxValue, minY = float.MaxValue; - float maxX = float.MinValue, maxY = float.MinValue; + double minX = double.MaxValue, minY = double.MaxValue; + double maxX = double.MinValue, maxY = double.MinValue; foreach (Vector2 point in vertices) { @@ -237,15 +237,15 @@ public class Collider2DBehaviour(IList vertices) : BehaviourOverride, I maxY = Math.Max(maxY, point.Y); } - float dx = maxX - minX; - float dy = maxY - minY; - float deltaMax = Math.Max(dx, dy); - float midX = (minX + maxX) / 2; - float midY = (minY + maxY) / 2; + double dx = maxX - minX; + double dy = maxY - minY; + double deltaMax = Math.Max(dx, dy); + double midX = (minX + maxX) / 2; + double midY = (minY + maxY) / 2; - Vector2 p1 = new Vector2(midX - 20 * deltaMax, midY - deltaMax); - Vector2 p2 = new Vector2(midX, midY + 20 * deltaMax); - Vector2 p3 = new Vector2(midX + 20 * deltaMax, midY - deltaMax); + Vector2 p1 = new Vector2((float)midX - 20f * (float)deltaMax, (float)midY - (float)deltaMax); + Vector2 p2 = new Vector2((float)midX, (float)midY + 20 * (float)deltaMax); + Vector2 p3 = new Vector2((float)midX + 20 * (float)deltaMax, (float)midY - (float)deltaMax); return new Triangle() { A = p1, B = p2, C = p3 }; } @@ -281,7 +281,7 @@ public class Collider2DBehaviour(IList vertices) : BehaviourOverride, I // double A3 = area(x1, y1, x2, y2, x, y); /* Check if sum of A1, A2 and A3 is same as A */ - return A == A1 + A2 + A3; + return A >= A1 + A2 + A3; } private struct Edge @@ -303,16 +303,16 @@ public class Collider2DBehaviour(IList vertices) : BehaviourOverride, I Vector2 midAB = (triangle.A + triangle.B) / 2; Vector2 midBC = (triangle.B + triangle.C) / 2; - float slopeAB = (triangle.B.Y - triangle.A.Y) / (triangle.B.X - triangle.A.X); - float slopeBC = (triangle.C.Y - triangle.B.Y) / (triangle.C.X - triangle.B.X); + double slopeAB = (triangle.B.Y - triangle.A.Y) / (triangle.B.X - triangle.A.X); + double slopeBC = (triangle.C.Y - triangle.B.Y) / (triangle.C.X - triangle.B.X); // Check if the slopes are not parallel - if (Math.Abs(slopeAB - slopeBC) > float.Epsilon) + if (Math.Abs(slopeAB - slopeBC) > double.Epsilon) { - float x = (slopeAB * slopeBC * (triangle.A.Y - triangle.C.Y) + slopeBC * (triangle.A.X + triangle.B.X) - slopeAB * (triangle.B.X + triangle.C.X)) / (2 * (slopeBC - slopeAB)); - float y = -(x - (triangle.A.X + triangle.B.X) / 2) / slopeAB + (triangle.A.Y + triangle.B.Y) / 2; + double x = (slopeAB * slopeBC * (triangle.A.Y - triangle.C.Y) + slopeBC * (triangle.A.X + triangle.B.X) - slopeAB * (triangle.B.X + triangle.C.X)) / (2 * (slopeBC - slopeAB)); + double y = -(x - (triangle.A.X + triangle.B.X) / 2) / slopeAB + (triangle.A.Y + triangle.B.Y) / 2; - result.Center = new Vector2(x, y); + result.Center = new Vector2((float)x, (float)y); result.Radius = Vector2.Distance(result.Center, triangle.A); } else @@ -345,7 +345,7 @@ public class Collider2DBehaviour(IList vertices) : BehaviourOverride, I { // See https://www.geeksforgeeks.org/orientation-3-ordered-points/ // for details of below formula. - float val = (q.Y - p.Y) * (r.X - q.X) - + double val = (q.Y - p.Y) * (r.X - q.X) - (q.X - p.X) * (r.Y - q.Y); if (val == 0) return 0; // collinear diff --git a/Game/Physics2D/PhysicsEngine2D.cs b/Game/Physics2D/PhysicsEngine2D.cs index ad4a36b..61646fb 100644 --- a/Game/Physics2D/PhysicsEngine2D.cs +++ b/Game/Physics2D/PhysicsEngine2D.cs @@ -58,6 +58,7 @@ public class PhysicsEngine2D : IPhysicsEngine2D for (int verticesIndex = 0; verticesIndex < colliderX.Vertices.Count; verticesIndex++) { ICollider2D colliderY = colliders[colliderIY]; + bool v = colliderY.CheckCollision(colliderX.Vertices[verticesIndex], colliderX, out var _); if (!colliderY.CheckCollision(colliderX.Vertices[verticesIndex], colliderX, out var collisionInformation)) continue; @@ -109,6 +110,7 @@ public class PhysicsEngine2D : IPhysicsEngine2D StepRigidBody(rigidX, intervalDeltaTime * (1f - t)); colliders[colliderIX].RecalculateVertices(); + verticesIndex--; } Console.WriteLine($"/////////////////////////////////////////////"); // Console.WriteLine($"Collision");