This commit is contained in:
Syntriax 2023-12-06 17:36:56 +03:00
parent 73c939805f
commit fd8964e58b
2 changed files with 61 additions and 28 deletions

View File

@ -155,28 +155,52 @@ public class Game1 : Game
if (Keyboard.GetState().IsKeyDown(Keys.E)) if (Keyboard.GetState().IsKeyDown(Keys.E))
gameManager.Camera.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.000025f; gameManager.Camera.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.000025f;
// if (Keyboard.GetState().IsKeyDown(Keys.N)) 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 < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds)
{ {
// Console.WriteLine($"Physics Timer: {physicsTimer}"); seconds = 70f;
physicsTimer += 0.01f; while (physicsTimer + 0.01f < seconds)
engine.Step(.01f); {
physicsTimer += 0.01f;
engine.Step(.01f);
}
} }
if (Keyboard.GetState().IsKeyDown(Keys.M))
{
seconds = 0f;
while (physicsTimer - 0.01f > seconds)
{
physicsTimer -= 0.01f;
engine.Step(-.01f);
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Space))
{
seconds += gameTime.ElapsedGameTime.Milliseconds * .0025f;
while (physicsTimer + 0.01f < seconds)
{
Console.WriteLine($"Physics Timer: {physicsTimer}");
physicsTimer += 0.01f;
engine.Step(.01f);
}
}
if (Keyboard.GetState().IsKeyDown(Keys.B))
{
seconds -= gameTime.ElapsedGameTime.Milliseconds * .0025f;
while (physicsTimer - 0.01f > seconds)
{
Console.WriteLine($"Physics Timer: {physicsTimer}");
physicsTimer -= 0.01f;
engine.Step(-.01f);
}
}
// while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds)
// {
// Console.WriteLine($"Physics Timer: {physicsTimer}");
// physicsTimer += 0.01f;
// engine.Step(.01f);
// }
gameManager.Update(gameTime); gameManager.Update(gameTime);
base.Update(gameTime); base.Update(gameTime);
} }

View File

@ -56,10 +56,10 @@ public class PhysicsEngine2D : IPhysicsEngine2D
CheckCollisions(colliders[i], colliders, (collider1, collider2, collisionVertex) => CheckCollisions(colliders[i], colliders, (collider1, collider2, collisionVertex) =>
{ {
ResolveCollision(collider1, collider2, collisionVertex, intervalDeltaTime); ResolveCollision(collider1, collider2, collisionVertex, intervalDeltaTime);
ResolveCollision(collider2, collider1, collisionVertex, intervalDeltaTime); // ResolveCollision(collider2, collider1, collisionVertex, intervalDeltaTime);
collider1.Recalculate(); // collider1.Recalculate();
collider2.Recalculate(); // collider2.Recalculate();
}); });
} }
} }
@ -76,7 +76,8 @@ public class PhysicsEngine2D : IPhysicsEngine2D
c2.Vertices.ToLines(lines); c2.Vertices.ToLines(lines);
Vector2 normal = Vector2.UnitY; Vector2 normal = Vector2.UnitY;
float t = 0f; float t = 1f;
bool hasIntersectedWithLines = false;
foreach (var line in lines) foreach (var line in lines)
{ {
@ -86,16 +87,24 @@ public class PhysicsEngine2D : IPhysicsEngine2D
if (intersectionPoint is not Vector2 ip) if (intersectionPoint is not Vector2 ip)
continue; continue;
t = vertexTrajectory.GetT(ip); float tTemp = vertexTrajectory.GetT(ip);
if (tTemp > t) // React to the earliest collision value
continue;
t = tTemp;
Vector2 lineDirection = line.Direction; Vector2 lineDirection = line.Direction;
normal = new(lineDirection.Y, lineDirection.X); normal = new(lineDirection.Y, lineDirection.X);
break; hasIntersectedWithLines = true;
} }
StepRigidBody(c1.RigidBody2D, -t * intervalDeltaTime); if (!hasIntersectedWithLines)
return;
float rewindInterval = t * intervalDeltaTime;
StepRigidBody(c1.RigidBody2D, -rewindInterval);
c1.RigidBody2D.Velocity = Vector2.Reflect(c1.RigidBody2D.Velocity, normal); c1.RigidBody2D.Velocity = Vector2.Reflect(c1.RigidBody2D.Velocity, normal);
StepRigidBody(c1.RigidBody2D, intervalDeltaTime - t * intervalDeltaTime); StepRigidBody(c1.RigidBody2D, rewindInterval);
} }
private void CheckCollisions(ICollider2D collider2D, List<ICollider2D> collider2Ds, Action<ICollider2D, ICollider2D, Vector2> OnCollisionDetectedAction) private void CheckCollisions(ICollider2D collider2D, List<ICollider2D> collider2Ds, Action<ICollider2D, ICollider2D, Vector2> OnCollisionDetectedAction)