afgdggdfga
This commit is contained in:
@@ -194,16 +194,16 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
|
||||
var pointVector = new Vector2(point.X - edge.A.X, point.Y - edge.A.Y);
|
||||
|
||||
// Calculate the projection of pointVector onto edgeVector
|
||||
float t = (pointVector.X * edgeVector.X + pointVector.Y * edgeVector.Y) / (edgeVector.X * edgeVector.X + edgeVector.Y * edgeVector.Y);
|
||||
double t = (pointVector.X * edgeVector.X + pointVector.Y * edgeVector.Y) / (edgeVector.X * edgeVector.X + edgeVector.Y * edgeVector.Y);
|
||||
|
||||
// Clamp t to the range [0, 1] to ensure the closest point is on the edge
|
||||
t = Math.Max(0, Math.Min(1, t));
|
||||
|
||||
// Calculate the closest point on the edge
|
||||
float closestX = edge.A.X + t * edgeVector.X;
|
||||
float closestY = edge.A.Y + t * edgeVector.Y;
|
||||
double closestX = edge.A.X + t * edgeVector.X;
|
||||
double closestY = edge.A.Y + t * edgeVector.Y;
|
||||
|
||||
return new Vector2(closestX, closestY);
|
||||
return new Vector2((float)closestX, (float)closestY);
|
||||
}
|
||||
|
||||
private bool DoesEdgeExistInTriangles(Edge edge, List<Triangle> triangles)
|
||||
@@ -226,8 +226,8 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
|
||||
|
||||
private Triangle GetSuperTriangle(IReadOnlyList<Vector2> vertices)
|
||||
{
|
||||
float minX = float.MaxValue, minY = float.MaxValue;
|
||||
float maxX = float.MinValue, maxY = float.MinValue;
|
||||
double minX = double.MaxValue, minY = double.MaxValue;
|
||||
double maxX = double.MinValue, maxY = double.MinValue;
|
||||
|
||||
foreach (Vector2 point in vertices)
|
||||
{
|
||||
@@ -237,15 +237,15 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
|
||||
maxY = Math.Max(maxY, point.Y);
|
||||
}
|
||||
|
||||
float dx = maxX - minX;
|
||||
float dy = maxY - minY;
|
||||
float deltaMax = Math.Max(dx, dy);
|
||||
float midX = (minX + maxX) / 2;
|
||||
float midY = (minY + maxY) / 2;
|
||||
double dx = maxX - minX;
|
||||
double dy = maxY - minY;
|
||||
double deltaMax = Math.Max(dx, dy);
|
||||
double midX = (minX + maxX) / 2;
|
||||
double midY = (minY + maxY) / 2;
|
||||
|
||||
Vector2 p1 = new Vector2(midX - 20 * deltaMax, midY - deltaMax);
|
||||
Vector2 p2 = new Vector2(midX, midY + 20 * deltaMax);
|
||||
Vector2 p3 = new Vector2(midX + 20 * deltaMax, midY - deltaMax);
|
||||
Vector2 p1 = new Vector2((float)midX - 20f * (float)deltaMax, (float)midY - (float)deltaMax);
|
||||
Vector2 p2 = new Vector2((float)midX, (float)midY + 20 * (float)deltaMax);
|
||||
Vector2 p3 = new Vector2((float)midX + 20 * (float)deltaMax, (float)midY - (float)deltaMax);
|
||||
|
||||
return new Triangle() { A = p1, B = p2, C = p3 };
|
||||
}
|
||||
@@ -281,7 +281,7 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
|
||||
// double A3 = area(x1, y1, x2, y2, x, y);
|
||||
|
||||
/* Check if sum of A1, A2 and A3 is same as A */
|
||||
return A == A1 + A2 + A3;
|
||||
return A >= A1 + A2 + A3;
|
||||
}
|
||||
|
||||
private struct Edge
|
||||
@@ -303,16 +303,16 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
|
||||
Vector2 midAB = (triangle.A + triangle.B) / 2;
|
||||
Vector2 midBC = (triangle.B + triangle.C) / 2;
|
||||
|
||||
float slopeAB = (triangle.B.Y - triangle.A.Y) / (triangle.B.X - triangle.A.X);
|
||||
float slopeBC = (triangle.C.Y - triangle.B.Y) / (triangle.C.X - triangle.B.X);
|
||||
double slopeAB = (triangle.B.Y - triangle.A.Y) / (triangle.B.X - triangle.A.X);
|
||||
double slopeBC = (triangle.C.Y - triangle.B.Y) / (triangle.C.X - triangle.B.X);
|
||||
|
||||
// Check if the slopes are not parallel
|
||||
if (Math.Abs(slopeAB - slopeBC) > float.Epsilon)
|
||||
if (Math.Abs(slopeAB - slopeBC) > double.Epsilon)
|
||||
{
|
||||
float x = (slopeAB * slopeBC * (triangle.A.Y - triangle.C.Y) + slopeBC * (triangle.A.X + triangle.B.X) - slopeAB * (triangle.B.X + triangle.C.X)) / (2 * (slopeBC - slopeAB));
|
||||
float y = -(x - (triangle.A.X + triangle.B.X) / 2) / slopeAB + (triangle.A.Y + triangle.B.Y) / 2;
|
||||
double x = (slopeAB * slopeBC * (triangle.A.Y - triangle.C.Y) + slopeBC * (triangle.A.X + triangle.B.X) - slopeAB * (triangle.B.X + triangle.C.X)) / (2 * (slopeBC - slopeAB));
|
||||
double y = -(x - (triangle.A.X + triangle.B.X) / 2) / slopeAB + (triangle.A.Y + triangle.B.Y) / 2;
|
||||
|
||||
result.Center = new Vector2(x, y);
|
||||
result.Center = new Vector2((float)x, (float)y);
|
||||
result.Radius = Vector2.Distance(result.Center, triangle.A);
|
||||
}
|
||||
else
|
||||
@@ -345,7 +345,7 @@ public class Collider2DBehaviour(IList<Vector2> vertices) : BehaviourOverride, I
|
||||
{
|
||||
// See https://www.geeksforgeeks.org/orientation-3-ordered-points/
|
||||
// for details of below formula.
|
||||
float val = (q.Y - p.Y) * (r.X - q.X) -
|
||||
double val = (q.Y - p.Y) * (r.X - q.X) -
|
||||
(q.X - p.X) * (r.Y - q.Y);
|
||||
|
||||
if (val == 0) return 0; // collinear
|
||||
|
@@ -58,6 +58,7 @@ public class PhysicsEngine2D : IPhysicsEngine2D
|
||||
for (int verticesIndex = 0; verticesIndex < colliderX.Vertices.Count; verticesIndex++)
|
||||
{
|
||||
ICollider2D colliderY = colliders[colliderIY];
|
||||
bool v = colliderY.CheckCollision(colliderX.Vertices[verticesIndex], colliderX, out var _);
|
||||
if (!colliderY.CheckCollision(colliderX.Vertices[verticesIndex], colliderX, out var collisionInformation))
|
||||
continue;
|
||||
|
||||
@@ -109,6 +110,7 @@ public class PhysicsEngine2D : IPhysicsEngine2D
|
||||
|
||||
StepRigidBody(rigidX, intervalDeltaTime * (1f - t));
|
||||
colliders[colliderIX].RecalculateVertices();
|
||||
verticesIndex--;
|
||||
}
|
||||
Console.WriteLine($"/////////////////////////////////////////////");
|
||||
// Console.WriteLine($"Collision");
|
||||
|
Reference in New Issue
Block a user