Hey this actually is deterministic atm

This commit is contained in:
Syntriax 2023-12-18 22:49:26 +03:00
parent 2e72347721
commit dd9950d6c5
3 changed files with 53 additions and 24 deletions

View File

@ -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<RigidBody2D>());
gameObjectBall.BehaviourController.AddBehaviour<MovementBallBehaviour>(new Vector2(.1f, .01f), 500f);
gameObjectBall.BehaviourController.AddBehaviour<MovementBallBehaviour>(new Vector2(.1f, .1f), 500f);
gameObjectBall.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall);
gameObjectBall.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2.One * 512f * .5f, Vector2.One * 512f * .5f);
// gameObjectBall = gameManager.InstantiateGameObject<GameObject>();
@ -76,25 +77,25 @@ public class Game1 : Game
// gameObjectBall.BehaviourController.AddBehaviour<DisplayableSpriteBehaviour>().Assign(spriteBall);
// gameObjectBall.BehaviourController.AddBehaviour<Collider2DAABBBehaviour>().AABBLocal = new AABB(-Vector2.One * 512f * .5f, Vector2.One * 512f * .5f);
IGameObject gameObjectLeft = gameManager.InstantiateGameObject<GameObject>();
gameObjectLeft.Name = "Left";
gameObjectLeft.Transform.Position = new Vector2(-452, 0f);
gameObjectLeft.Transform.Scale = new Vector2(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(-Vector2.One * .5f, Vector2.One * .5f);
engine.AddRigidBody(gameObjectLeft.BehaviourController.AddBehaviour<RigidBody2D>());
// IGameObject gameObjectLeft = gameManager.InstantiateGameObject<GameObject>();
// gameObjectLeft.Name = "Left";
// gameObjectLeft.Transform.Position = new Vector2(-452, 0f);
// gameObjectLeft.Transform.Scale = new Vector2(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(-Vector2.One * .5f, Vector2.One * .5f);
// engine.AddRigidBody(gameObjectLeft.BehaviourController.AddBehaviour<RigidBody2D>());
IGameObject gameObjectRight = gameManager.InstantiateGameObject<GameObject>();
gameObjectRight.Name = "Right";
gameObjectRight.Transform.Position = new Vector2(452, 0f);
gameObjectRight.Transform.Scale = new Vector2(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(-Vector2.One * .5f, Vector2.One * .5f);
engine.AddRigidBody(gameObjectRight.BehaviourController.AddBehaviour<RigidBody2D>());
// IGameObject gameObjectRight = gameManager.InstantiateGameObject<GameObject>();
// gameObjectRight.Name = "Right";
// gameObjectRight.Transform.Position = new Vector2(452, 0f);
// gameObjectRight.Transform.Scale = new Vector2(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(-Vector2.One * .5f, Vector2.One * .5f);
// engine.AddRigidBody(gameObjectRight.BehaviourController.AddBehaviour<RigidBody2D>());
IGameObject goPlayAreaTop = gameManager.InstantiateGameObject<GameObject>();

View File

@ -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--;
}
}
}
}

View File

@ -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<Vector2> 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));