I managed to get bridges working in my #pico8 test project!
Took me long enough as some things turned out to be more complicated than antipated (thread)
Took me long enough as some things turned out to be more complicated than antipated (thread)
The idea was simple: Find the intersection of the river with the road, place a bridge onto it - simple, right? So I searched for the point of a river sprite closest to the direct line of the road path. I already know the point's direction so how hard could it be?
Constructing the bridge took also some time using sprites and aligning them, looked like I had it solved! Time for testing some other configurations!
So I activated a bit of randomization on a keypress to see what would happen. Looking fine so far, good, good, good.... WHA!?
Some debugging revealed that the river point's direction was too random, leading to lots of followup intersections. Ok! So let's take the points and find out the average line! (I found this page here very useful: https://www.mathsisfun.com/data/least-squares-regression.html). However, it had problems...
I kept having outliers. It dawned on me that the limited floating point range of Pico8 is causing this - squaring numbers is something that quickly leads to overflows. Oh my. Time to rethink.
In the end I found a solution that seems to work well enough for now: Finding the two points with greatest distance to each other. It requires doing n*n tests, but hey, it's numerically stable!
I hope you enjoyed reading this!
I hope you enjoyed reading this!