private void GenerateShape(List<Vector2> points, MountainChunk mountainChunk, GroundChunk previousGroundChunk, float distanceFromMountain) { float directDistFromMountain = distanceFromMountain; Vector2 mountainVector = mountainChunk.GetLastEdgePoint().vector - mountainChunk.GetFirstEdgePoint().vector; Vector2 slopePerp = new Vector2(slopeVector.y, -slopeVector.x); int numPoints = mountainChunk.edgePoints.Count; float totalDistance = mountainVector.magnitude; float distPerPoint = totalDistance / (numPoints - 1); float angleFromMountain = Vector2.Angle(Vector2.down, slopePerp * directDistFromMountain); float verticalDistFromMountain = directDistFromMountain / Mathf.Cos(angleFromMountain * Mathf.Deg2Rad); Vector2 verticalVectorFromMountain = new Vector2(0, -verticalDistFromMountain); Vector2 origin = mountainChunk.GetFirstEdgePoint().vector + verticalVectorFromMountain; Vector2 prevPoint = origin; points.Add(prevPoint); for (int i = 0; i < numPoints; i++) { Vector2 delta = slopeVector * distPerPoint; Vector2 point = prevPoint + delta; points.Add(point); prevPoint = point; } // make the first point of any new chunk the same as the last point of the previous chunk if (previousGroundChunk != null) { Vector2 previousFirstPoint = previousGroundChunk.GetLastEdgePoint().vector; Vector2 fourthPoint = points[3]; Vector2 vector = fourthPoint - previousFirstPoint; Vector2 direction = vector.normalized; float dist = vector.magnitude; float distPer = dist / 3; points[0] = previousFirstPoint; points[1] = points[0] + direction * distPer; points[2] = points[1] + direction * distPer; } for (int i = 4; i < points.Count; i++) { Vector2 point = points[i]; point.y += UnityEngine.Random.Range(-maxBumpHeight, maxBumpHeight); points[i] = point; } foreach (Vector2 point in points) { Point pointObject = new Point(point); edgePoints.Add(pointObject); } // add one more point, this time with margin Vector2 lastPoint = points[points.Count - 1]; Vector2 lastPointWithMargin = lastPoint; lastPointWithMargin.x += marginSize; points.Add(lastPointWithMargin); prevPoint = lastPoint; int extraDepthOnBottom = -30; Vector2 first = points[0]; points.Add(new Vector2(lastPointWithMargin.x, first.y + extraDepthOnBottom)); points.Add(new Vector2(first.x, first.y + extraDepthOnBottom)); points.Add(new Vector2(first.x, first.y - 1)); }
public void Generate(MountainChunk mountainChunk, GroundChunk previousGroundChunk, float distanceFromMountain) { Reset(); slopeVector = (mountainChunk.GetLastEdgePoint().vector - mountainChunk.GetFirstEdgePoint().vector).normalized; List<Vector2> points = new List<Vector2>(); GenerateShape(points, mountainChunk, previousGroundChunk, distanceFromMountain); Vector2[] pointsArray = points.ToArray(); polygonCollider.points = pointsArray; meshCreator.InitMesh(pointsArray); CalculateDistances(); }