Testset
This commit is contained in:
parent
259941c867
commit
1e8cc82dc4
|
@ -152,7 +152,7 @@ public class Game1 : Game
|
||||||
seconds += gameTime.ElapsedGameTime.Milliseconds * .0005f;
|
seconds += gameTime.ElapsedGameTime.Milliseconds * .0005f;
|
||||||
|
|
||||||
// TODO: Add your update logic here
|
// TODO: Add your update logic here
|
||||||
while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)
|
while (physicsTimer + 0.01f < seconds)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Physics Timer: {physicsTimer}");
|
Console.WriteLine($"Physics Timer: {physicsTimer}");
|
||||||
physicsTimer += 0.01f;
|
physicsTimer += 0.01f;
|
||||||
|
|
|
@ -42,12 +42,7 @@ public class PhysicsEngine2D : IPhysicsEngine2D
|
||||||
for (int i = 0; i < IterationCount; i++)
|
for (int i = 0; i < IterationCount; i++)
|
||||||
{
|
{
|
||||||
foreach (var rigidBody in rigidBodies)
|
foreach (var rigidBody in rigidBodies)
|
||||||
{
|
StepRigidBody(rigidBody, intervalDeltaTime);
|
||||||
Vector2 nextPosition = rigidBody.Transform.Position;
|
|
||||||
nextPosition += rigidBody.Velocity * intervalDeltaTime;
|
|
||||||
|
|
||||||
rigidBody.Transform.Position = nextPosition;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
foreach (var collider in colliders)
|
foreach (var collider in colliders)
|
||||||
|
@ -57,47 +52,49 @@ public class PhysicsEngine2D : IPhysicsEngine2D
|
||||||
{
|
{
|
||||||
ICollider2D colliderX = colliders[colliderIX];
|
ICollider2D colliderX = colliders[colliderIX];
|
||||||
for (int colliderIY = colliderIX + 1; colliderIY < colliders.Count; colliderIY++)
|
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(colliderX.Vertices[verticesIndex], colliderX, out var collisionInformation))
|
||||||
if (!colliders[colliderIY].CheckCollision(vertex, colliderX, out var collisionInformation))
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
Console.WriteLine($"normal = {collisionInformation.Normal}");
|
ICollider2D colliderY = colliders[colliderIY];
|
||||||
// // if (colliders[colliderIX].BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidX))
|
if (colliderX.BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidX))
|
||||||
// // rigidX.Velocity = Vector2.Reflect(rigidX.Velocity, collisionInformation.Normal);
|
|
||||||
if (colliders[colliderIX].BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidX))
|
|
||||||
{
|
{
|
||||||
Vector2 toContact = colliderX.Transform.Position - collisionInformation.ContactPosition;
|
Vector2 xVertex = colliderX.Vertices[verticesIndex];
|
||||||
Vector2 mirrored = Vector2.Reflect(toContact, new Vector2(collisionInformation.Normal.X, collisionInformation.Normal.Y));
|
Vector2 edgeDirection = new Vector2(collisionInformation.Normal.Y, collisionInformation.Normal.X);
|
||||||
Console.WriteLine($"rigidX.Velocity = {rigidX.Velocity}");
|
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.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);
|
StepRigidBody(rigidX, intervalDeltaTime * (1f - t));
|
||||||
Console.WriteLine($"Displacement = {vector2}");
|
colliders[colliderIX].RecalculateVertices();
|
||||||
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();
|
|
||||||
colliders[colliderIX].RecalculateVertices();
|
|
||||||
|
|
||||||
|
Console.WriteLine($"/////////////////////////////////////////////");
|
||||||
// Console.WriteLine($"Collision");
|
// 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)
|
private void OnBehaviourAdded(IBehaviourController controller, IBehaviour behaviour)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue