From d21c6674eabfdb79aba4d10c2771d479f6ee932c Mon Sep 17 00:00:00 2001 From: Regalis Date: Tue, 11 Oct 2016 18:18:54 +0300 Subject: [PATCH] Level generation bugfixes: - cell bodies match the shape of the cell (triangulation didn't work correctly because the center of the cell was given in display units while vertices were in sim units) - ignoring graph edges whose length is zero when generating cells (fixes "invalid left/right normal" errors) --- Subsurface/Source/Map/Levels/CaveGenerator.cs | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Subsurface/Source/Map/Levels/CaveGenerator.cs b/Subsurface/Source/Map/Levels/CaveGenerator.cs index e1836f09d..7f68a6a0a 100644 --- a/Subsurface/Source/Map/Levels/CaveGenerator.cs +++ b/Subsurface/Source/Map/Levels/CaveGenerator.cs @@ -185,6 +185,8 @@ namespace Barotrauma foreach (GraphEdge ge in graphEdges) { + if (ge.point1 == ge.point2) continue; + for (int i = 0; i < 2; i++) { Site site = (i == 0) ? ge.site1 : ge.site2; @@ -408,31 +410,29 @@ namespace Barotrauma bodyPoints[i] = ConvertUnits.ToSimUnits(bodyPoints[i]); } + if (cell.CellType == CellType.Empty) continue; - triangles = MathUtils.TriangulateConvexHull(bodyPoints, cell.Center); + triangles = MathUtils.TriangulateConvexHull(bodyPoints, ConvertUnits.ToSimUnits(cell.Center)); - Body edgeBody = new Body(GameMain.World); + Body cellBody = new Body(GameMain.World); for (int i = 0; i < triangles.Count; i++) { if (triangles[i][0].Y == triangles[i][1].Y && triangles[i][0].Y == triangles[i][2].Y) continue; if (triangles[i][0].X == triangles[i][1].X && triangles[i][0].X == triangles[i][2].X) continue; - if (Vector2.DistanceSquared(triangles[i][0], triangles[i][1]) < 0.1f) continue; - if (Vector2.DistanceSquared(triangles[i][1], triangles[i][2]) < 0.1f) continue; - Vertices bodyVertices = new Vertices(triangles[i]); - FixtureFactory.AttachPolygon(bodyVertices, 5.0f, edgeBody); + FixtureFactory.AttachPolygon(bodyVertices, 5.0f, cellBody); } - edgeBody.UserData = cell; - edgeBody.SleepingAllowed = false; - edgeBody.BodyType = BodyType.Kinematic; - edgeBody.CollisionCategories = Physics.CollisionLevel; + cellBody.UserData = cell; + cellBody.SleepingAllowed = false; + cellBody.BodyType = BodyType.Kinematic; + cellBody.CollisionCategories = Physics.CollisionLevel; - cell.body = edgeBody; - bodies.Add(edgeBody); + cell.body = cellBody; + bodies.Add(cellBody); } return bodies; @@ -489,7 +489,7 @@ namespace Barotrauma if (!MathUtils.IsValid(leftNormal)) { #if DEBUG - DebugConsole.ThrowError("Invalid right normal"); + DebugConsole.ThrowError("Invalid left normal"); #endif if (cell.body != null) {