Part of my ongoing procedural generation adventure is devising a way to connect arbitrary points in space at varying angles using a series of adjustable prefabricated segments. This can provide the important ability for a level generator to create cycles or closed paths that allow a player to return to a previous area via a different route, and it can enable chunks in my 3D chunk system to connect their interior geometry with the "doors" on their borders. Both of these concepts are or will soon be covered on my blog in more detail.

The gist of the algorithm:
1. Verify that the line connecting the end points and the rays defined by their facing directions can form a triangle; otherwise it is (probably) impossible to form a bridge connecting them without having to curve back and forth or more than 180 degrees.
2. Calculate (via multiple steps of trigonometry) where to insert a new corner that forms an isosceles triangle centered around the intersection of the two rays. The symmetry of the isosceles triangle is important to ensure that the curved segments can align with each other as well as both of the end points.
3. Instantiate curved segments and adjust their curvature until the angle between the end points is eliminated, and position these using the corners of the isosceles triangle so that they align precisely.
4. Fill the gaps between the two sets of curved segments with straight segments, using adjustable straight segments at the ends as needed to achieve a precise fit.

WASD or arrow keys to move a tower
Q and E to rotate a tower
M to switch control between towers
B to generate a bridge (and delete any existing one)
If a bridge does not appear, try adjusting the towers and make sure they point vaguely toward one another.

UPDATE 23 April 2021:
- When adding straight segments, the algorithm now has a list of segments from which to choose and can check whether a given segment will fit in the available space, allowing it to choose whichever is the longest segment that will fit. This enables it to construct a bridge from fewer total segments, improving performance.
- Bridge construction is now "animated" using a coroutine.

Leave a comment

Log in with itch.io to leave a comment.