afgdggdfga

This commit is contained in:
Syntriax 2023-12-05 15:27:31 +03:00
parent b8706e82d8
commit 4692b4e25e
3 changed files with 49 additions and 40 deletions

View File

@ -56,7 +56,7 @@ public class Game1 : Game
gameManager.Camera = gameObjectCamera.BehaviourController.AddBehaviour<CameraBehaviour>(); gameManager.Camera = gameObjectCamera.BehaviourController.AddBehaviour<CameraBehaviour>();
gameManager.Camera.Viewport = GraphicsDevice.Viewport; gameManager.Camera.Viewport = GraphicsDevice.Viewport;
IGameObject gameObjectBall = gameManager.InstantiateGameObject<GameObject>(); gameObjectBall = gameManager.InstantiateGameObject<GameObject>();
gameObjectBall.Name = "Ball"; gameObjectBall.Name = "Ball";
gameObjectBall.Transform.Position = Vector2.Zero; gameObjectBall.Transform.Position = Vector2.Zero;
gameObjectBall.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f); gameObjectBall.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f);
@ -89,26 +89,26 @@ public class Game1 : Game
IGameObject goPlayAreaTop = gameManager.InstantiateGameObject<GameObject>(); IGameObject goPlayAreaTop = gameManager.InstantiateGameObject<GameObject>();
goPlayAreaTop.Transform.Position = new Vector2(0f, 288f + 20f); goPlayAreaTop.Transform.Position = new Vector2(0f, 288f + 20f);
goPlayAreaTop.Transform.Scale = new Vector2(10240f, 40f); goPlayAreaTop.Transform.Scale = new Vector2(10240f, 40f);
goPlayAreaTop.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox); // goPlayAreaTop.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
goPlayAreaTop.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]); goPlayAreaTop.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]);
engine.AddRigidBody(goPlayAreaTop.BehaviourController.AddBehaviour<RigidBody2D>()); engine.AddRigidBody(goPlayAreaTop.BehaviourController.AddBehaviour<RigidBody2D>());
IGameObject goPlayAreaBottom = gameManager.InstantiateGameObject<GameObject>(); IGameObject goPlayAreaBottom = gameManager.InstantiateGameObject<GameObject>();
goPlayAreaBottom.Transform.Position = new Vector2(0f, -(288f + 20f)); goPlayAreaBottom.Transform.Position = new Vector2(0f, -(288f + 20f));
goPlayAreaBottom.Transform.Scale = new Vector2(10240f, 40f); goPlayAreaBottom.Transform.Scale = new Vector2(10240f, 40f);
goPlayAreaBottom.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox); // goPlayAreaBottom.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
goPlayAreaBottom.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]); goPlayAreaBottom.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]);
engine.AddRigidBody(goPlayAreaBottom.BehaviourController.AddBehaviour<RigidBody2D>()); engine.AddRigidBody(goPlayAreaBottom.BehaviourController.AddBehaviour<RigidBody2D>());
IGameObject goPlayAreaRight = gameManager.InstantiateGameObject<GameObject>(); IGameObject goPlayAreaRight = gameManager.InstantiateGameObject<GameObject>();
goPlayAreaRight.Transform.Position = new Vector2(512f + 20f, 0f); goPlayAreaRight.Transform.Position = new Vector2(512f + 20f, 0f);
goPlayAreaRight.Transform.Scale = new Vector2(40f, 5760f); goPlayAreaRight.Transform.Scale = new Vector2(40f, 5760f);
goPlayAreaRight.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox); // goPlayAreaRight.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
goPlayAreaRight.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]); goPlayAreaRight.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]);
engine.AddRigidBody(goPlayAreaRight.BehaviourController.AddBehaviour<RigidBody2D>()); engine.AddRigidBody(goPlayAreaRight.BehaviourController.AddBehaviour<RigidBody2D>());
IGameObject goPlayAreaLeft = gameManager.InstantiateGameObject<GameObject>(); IGameObject goPlayAreaLeft = gameManager.InstantiateGameObject<GameObject>();
goPlayAreaLeft.Transform.Position = new Vector2(-(512f + 20f), 0f); goPlayAreaLeft.Transform.Position = new Vector2(-(512f + 20f), 0f);
goPlayAreaLeft.Transform.Scale = new Vector2(40f, 5760f); goPlayAreaLeft.Transform.Scale = new Vector2(40f, 5760f);
goPlayAreaLeft.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox); // goPlayAreaLeft.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBox);
goPlayAreaLeft.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]); goPlayAreaLeft.BehaviourController.AddBehaviour<Collider2DBehaviour>((System.Collections.Generic.IList<Vector2>)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]);
engine.AddRigidBody(goPlayAreaLeft.BehaviourController.AddBehaviour<RigidBody2D>()); engine.AddRigidBody(goPlayAreaLeft.BehaviourController.AddBehaviour<RigidBody2D>());
@ -154,20 +154,23 @@ public class Game1 : Game
gameManager.Camera.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.000025f; gameManager.Camera.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.000025f;
if (Keyboard.GetState().IsKeyDown(Keys.Space)) // if (Keyboard.GetState().IsKeyDown(Keys.N))
seconds += gameTime.ElapsedGameTime.Milliseconds * .0005f; // seconds = 39.12f;
if (Keyboard.GetState().IsKeyDown(Keys.B)) // if (Keyboard.GetState().IsKeyDown(Keys.Space))
{ // seconds += gameTime.ElapsedGameTime.Milliseconds * .005f;
seconds -= gameTime.ElapsedGameTime.Milliseconds * .0005f; // if (Keyboard.GetState().IsKeyDown(Keys.B))
while (physicsTimer - 0.01f > seconds) // {
{ // seconds -= gameTime.ElapsedGameTime.Milliseconds * .005f;
Console.WriteLine($"Physics Timer: {physicsTimer}"); // while (physicsTimer - 0.01f > seconds)
physicsTimer -= 0.01f; // {
engine.Step(-.01f); // 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}"); Console.WriteLine($"Physics Timer: {physicsTimer}");
physicsTimer += 0.01f; physicsTimer += 0.01f;
@ -178,11 +181,15 @@ public class Game1 : Game
} }
static float physicsTimer = 0f; static float physicsTimer = 0f;
static float seconds = 0f; static float seconds = 0f;
private GameObject gameObjectBall;
protected override void Draw(GameTime gameTime) protected override void Draw(GameTime gameTime)
{ {
GraphicsDevice.Clear(new Color() { R = 32, G = 32, B = 32 }); 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 // TODO: Add your drawing code here
gameManager.PreDraw(gameTime); gameManager.PreDraw(gameTime);
gameManager.Camera.Update(); gameManager.Camera.Update();

View File

@ -194,16 +194,16 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
var pointVector = new Vector2(point.X - edge.A.X, point.Y - edge.A.Y); var pointVector = new Vector2(point.X - edge.A.X, point.Y - edge.A.Y);
// Calculate the projection of pointVector onto edgeVector // 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 // Clamp t to the range [0, 1] to ensure the closest point is on the edge
t = Math.Max(0, Math.Min(1, t)); t = Math.Max(0, Math.Min(1, t));
// Calculate the closest point on the edge // Calculate the closest point on the edge
float closestX = edge.A.X + t * edgeVector.X; double closestX = edge.A.X + t * edgeVector.X;
float closestY = edge.A.Y + t * edgeVector.Y; 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<Triangle> triangles) private bool DoesEdgeExistInTriangles(Edge edge, List<Triangle> triangles)
@ -226,8 +226,8 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
private Triangle GetSuperTriangle(IReadOnlyList<Vector2> vertices) private Triangle GetSuperTriangle(IReadOnlyList<Vector2> vertices)
{ {
float minX = float.MaxValue, minY = float.MaxValue; double minX = double.MaxValue, minY = double.MaxValue;
float maxX = float.MinValue, maxY = float.MinValue; double maxX = double.MinValue, maxY = double.MinValue;
foreach (Vector2 point in vertices) foreach (Vector2 point in vertices)
{ {
@ -237,15 +237,15 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
maxY = Math.Max(maxY, point.Y); maxY = Math.Max(maxY, point.Y);
} }
float dx = maxX - minX; double dx = maxX - minX;
float dy = maxY - minY; double dy = maxY - minY;
float deltaMax = Math.Max(dx, dy); double deltaMax = Math.Max(dx, dy);
float midX = (minX + maxX) / 2; double midX = (minX + maxX) / 2;
float midY = (minY + maxY) / 2; double midY = (minY + maxY) / 2;
Vector2 p1 = new Vector2(midX - 20 * deltaMax, midY - deltaMax); Vector2 p1 = new Vector2((float)midX - 20f * (float)deltaMax, (float)midY - (float)deltaMax);
Vector2 p2 = new Vector2(midX, midY + 20 * deltaMax); Vector2 p2 = new Vector2((float)midX, (float)midY + 20 * (float)deltaMax);
Vector2 p3 = new Vector2(midX + 20 * deltaMax, midY - deltaMax); Vector2 p3 = new Vector2((float)midX + 20 * (float)deltaMax, (float)midY - (float)deltaMax);
return new Triangle() { A = p1, B = p2, C = p3 }; return new Triangle() { A = p1, B = p2, C = p3 };
} }
@ -281,7 +281,7 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
// double A3 = area(x1, y1, x2, y2, x, y); // double A3 = area(x1, y1, x2, y2, x, y);
/* Check if sum of A1, A2 and A3 is same as A */ /* 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 private struct Edge
@ -303,16 +303,16 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
Vector2 midAB = (triangle.A + triangle.B) / 2; Vector2 midAB = (triangle.A + triangle.B) / 2;
Vector2 midBC = (triangle.B + triangle.C) / 2; Vector2 midBC = (triangle.B + triangle.C) / 2;
float slopeAB = (triangle.B.Y - triangle.A.Y) / (triangle.B.X - triangle.A.X); double 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 slopeBC = (triangle.C.Y - triangle.B.Y) / (triangle.C.X - triangle.B.X);
// Check if the slopes are not parallel // 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)); 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));
float y = -(x - (triangle.A.X + triangle.B.X) / 2) / slopeAB + (triangle.A.Y + triangle.B.Y) / 2; 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); result.Radius = Vector2.Distance(result.Center, triangle.A);
} }
else else
@ -345,7 +345,7 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
{ {
// See https://www.geeksforgeeks.org/orientation-3-ordered-points/ // See https://www.geeksforgeeks.org/orientation-3-ordered-points/
// for details of below formula. // 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); (q.X - p.X) * (r.Y - q.Y);
if (val == 0) return 0; // collinear if (val == 0) return 0; // collinear

View File

@ -58,6 +58,7 @@ public class PhysicsEngine2D : IPhysicsEngine2D
for (int verticesIndex = 0; verticesIndex < colliderX.Vertices.Count; verticesIndex++) for (int verticesIndex = 0; verticesIndex < colliderX.Vertices.Count; verticesIndex++)
{ {
ICollider2D colliderY = colliders[colliderIY]; ICollider2D colliderY = colliders[colliderIY];
bool v = colliderY.CheckCollision(colliderX.Vertices[verticesIndex], colliderX, out var _);
if (!colliderY.CheckCollision(colliderX.Vertices[verticesIndex], colliderX, out var collisionInformation)) if (!colliderY.CheckCollision(colliderX.Vertices[verticesIndex], colliderX, out var collisionInformation))
continue; continue;
@ -109,6 +110,7 @@ public class PhysicsEngine2D : IPhysicsEngine2D
StepRigidBody(rigidX, intervalDeltaTime * (1f - t)); StepRigidBody(rigidX, intervalDeltaTime * (1f - t));
colliders[colliderIX].RecalculateVertices(); colliders[colliderIX].RecalculateVertices();
verticesIndex--;
} }
Console.WriteLine($"/////////////////////////////////////////////"); Console.WriteLine($"/////////////////////////////////////////////");
// Console.WriteLine($"Collision"); // Console.WriteLine($"Collision");