Hey this actually is deterministic atm
This commit is contained in:
@@ -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--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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));
|
||||
|
Reference in New Issue
Block a user