In my animation post - here - I stated that I was planning to implement a node/spline system to allow fish to smoothly traverse any path I chose to setup. This post highlights recent developments in this area.
I'm using manually placed control points to set-up predefined paths within the Unity editor. The image below shows a set of control points - numbered 1 to 9 - which have been added as children to an empty game object called curve.
At present, these objects don't really do anything special other than give me a transform and visual representation of where I want the curve to go. The tricky stuff comes next...
Calculating the Curve
I'm calculating the curve at run-time. To do this I wrote a custom curve script and attached it to my curve object. The process of calculating the curve is quite technical and there's a few ways you can do it but this is the high level process I adopted:
(1) I assigned all control points (the children of my curve) to a list
(2) I iterated through this list of control points in order (ordering is implied by my naming/numbering)
(3) For each pair of control points I did the following:
- I calculated the tangents of the curve using the control points themselves
- I generated a series of nodes between them using a cubic Hermite algorithm
(4) Once all nodes had been calculated, I set the next node of each node generated to the following one
(5) Finally, I orient each node to face the next one in sequence - the last node I auto-orient
The images below shows the curve before and after a smoothing multiplier has been applied.
I also have control over the number of nodes to place between each control point: 10 is a good fit here.
My good friend Olly Boyce - a very talented coder who is also on Indie DB here - was the person who recommended I take a look into Hermite curves as they are pretty easy to calculate and quite powerful. You can find pseudocode for generating Hermite splines at various places online.
Moving between Nodes
Movement between nodes is handled elsewhere. I added special code to my ship and sealife objects so that they would detect a collision with a node and then proceed to traverse the rest of the network automatically using nodes as their path. Here's a video of the final outcome.
Notice also that the ship gets a little push at the end of the network.
Now for something a little fancier. Since I'm generating curves dynamically, it didn't take much extra effort to let the player sketch them as part of the constellation puzzles:
The stars themselves are fed into the curve generation algorithm as control points and that's pretty much the only change I needed to make: powerful - and reusable - stuff!
That's it for our look into the basic curve system used in Observatorium.
I'm still working hard to get the game to an alpha state. Things are progressing slowly but surely and the gameplay and story are evolving constantly/naturally throughout development. Suffice to say things have already changed alot since the first prototype.
Any comments or feedback, please post below!
Thanks for reading!
The Man Who Flew Away