This commit is contained in:
Syntriax 2023-12-04 23:55:43 +03:00
parent 259941c867
commit 1e8cc82dc4
2 changed files with 31 additions and 34 deletions

View File

@ -152,7 +152,7 @@ public class Game1 : Game
seconds += gameTime.ElapsedGameTime.Milliseconds * .0005f;
// TODO: Add your update logic here
while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)
while (physicsTimer + 0.01f < seconds)
{
Console.WriteLine($"Physics Timer: {physicsTimer}");
physicsTimer += 0.01f;

View File

@ -42,12 +42,7 @@ public class PhysicsEngine2D : IPhysicsEngine2D
for (int i = 0; i < IterationCount; i++)
{
foreach (var rigidBody in rigidBodies)
{
Vector2 nextPosition = rigidBody.Transform.Position;
nextPosition += rigidBody.Velocity * intervalDeltaTime;
rigidBody.Transform.Position = nextPosition;
}
StepRigidBody(rigidBody, intervalDeltaTime);
foreach (var collider in colliders)
@ -57,47 +52,49 @@ public class PhysicsEngine2D : IPhysicsEngine2D
{
ICollider2D colliderX = colliders[colliderIX];
for (int colliderIY = colliderIX + 1; colliderIY < colliders.Count; colliderIY++)
for (int i1 = 0; i1 < colliderX.Vertices.Count; i1++)
for (int verticesIndex = 0; verticesIndex < colliderX.Vertices.Count; verticesIndex++)
{
Vector2 vertex = colliderX.Vertices[i1];
if (!colliders[colliderIY].CheckCollision(vertex, colliderX, out var collisionInformation))
if (!colliders[colliderIY].CheckCollision(colliderX.Vertices[verticesIndex], colliderX, out var collisionInformation))
continue;
Console.WriteLine($"normal = {collisionInformation.Normal}");
// // if (colliders[colliderIX].BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidX))
// // rigidX.Velocity = Vector2.Reflect(rigidX.Velocity, collisionInformation.Normal);
if (colliders[colliderIX].BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidX))
ICollider2D colliderY = colliders[colliderIY];
if (colliderX.BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidX))
{
Vector2 toContact = colliderX.Transform.Position - collisionInformation.ContactPosition;
Vector2 mirrored = Vector2.Reflect(toContact, new Vector2(collisionInformation.Normal.X, collisionInformation.Normal.Y));
Console.WriteLine($"rigidX.Velocity = {rigidX.Velocity}");
Vector2 xVertex = colliderX.Vertices[verticesIndex];
Vector2 edgeDirection = new Vector2(collisionInformation.Normal.Y, collisionInformation.Normal.X);
Vector2 p1 = xVertex - rigidX.Velocity * intervalDeltaTime;
float t = IntersectionParameterT(collisionInformation.ContactPosition - edgeDirection * 100f, collisionInformation.ContactPosition + edgeDirection * 100f, xVertex, p1);
Vector2 vertexNewPosition = Vector2.Lerp(xVertex, p1, t);
rigidX.Velocity = Vector2.Reflect(rigidX.Velocity, collisionInformation.Normal);
rigidX.Transform.Position -= xVertex - vertexNewPosition;
Vector2 vector2 = -new Vector2(collisionInformation.Normal.X * rigidX.Velocity.X, collisionInformation.Normal.Y * rigidX.Velocity.Y);
Console.WriteLine($"Displacement = {vector2}");
rigidX.Transform.Position -= mirrored * 2;
Console.WriteLine($"rigidX.Velocity = {rigidX.Velocity}");
}
// if (colliders[colliderIY].BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidY))
// {
// Console.WriteLine($"rigidY.Velocity = {rigidY.Velocity}");
// rigidY.Velocity = Vector2.Reflect(rigidY.Velocity, collisionInformation.Normal);
// rigidY.Velocity = Vector2.Reflect(rigidY.Velocity, collisionInformation.Normal);
// Vector2 vector2 = -new Vector2(collisionInformation.Normal.X * rigidY.Velocity.X, collisionInformation.Normal.Y * rigidY.Velocity.Y);
// Console.WriteLine($"Displacement = {vector2}");
// rigidY.Transform.Position += vector2 * intervalDeltaTime * 2;
// Console.WriteLine($"rigidY.Velocity = {rigidY.Velocity}");
// }
Console.WriteLine($"/////////////////////////////////////////////");
colliders[colliderIY].RecalculateVertices();
StepRigidBody(rigidX, intervalDeltaTime * (1f - t));
colliders[colliderIX].RecalculateVertices();
}
Console.WriteLine($"/////////////////////////////////////////////");
// Console.WriteLine($"Collision");
}
}
}
}
private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime)
{
Vector2 nextPosition = rigidBody.Transform.Position;
nextPosition += rigidBody.Velocity * intervalDeltaTime;
rigidBody.Transform.Position = nextPosition;
}
private float IntersectionParameterT(Vector2 p0, Vector2 p1, Vector2 q0, Vector2 q1)
{
// Solve the linear interpolation equation for 't'
float t = ((q0.X - p0.X) * (p1.Y - p0.Y) - (q0.Y - p0.Y) * (p1.X - p0.X)) /
((q1.Y - q0.Y) * (p1.X - p0.X) - (q1.X - q0.X) * (p1.Y - p0.Y));
return t;
}
private void OnBehaviourAdded(IBehaviourController controller, IBehaviour behaviour)
{