Forensic Friday 6: Power System Comes Online + Sexy Curves
For once Forensic Friday is on a Friday! That’s almost enough to celebrate as it is, but there is in fact more! Saving and loading of the game is a constant thing that needs updating every time we add new content but is now quite streamlined so doesn’t have any issues. Instead, it’s time to go down memory lane and talk about electricity…
The Story of the Power System #
Looking back, a long time ago now, at our 4th ever blog post, one of the bullet points that was marked down as a “near future” change was rewriting the power system. Before we started writing blogs about our progress, in the very early days of development when we were quite new to unity, one of the systems we had somewhat attempted was power. When placing down a wire, this wire would be able to see the wires around itself, and assign itself an ID. This all sounds quite trivial in theory, and it should have been, but hysterically it was a mess. Wires for some reason had physics colliders that checked if they were overlapping, the actual power networks never actually transferred power, just had an ID, and to top things all off, this updated every frame to set the exact same values when nothing had changed. It didn’t take long to realise when the FPS dropped that this just wouldn’t do, and it was straight up deleted immediately.
Therefore, unlike other rewrites – an example being the damage system – this rewrite was truly starting from scratch since there was no old material to scavenge from. Whilst originally there were only wires, we had since agreed that pylons should be an option for transferring power. Therefore, the first thing I implemented was connecting pylons together and creating power networks. This was simple enough data structure wise besides a couple of quirks, but visually drawing the overhanging powerlines required myself to learn how to plot points using Bézier Curves – a form of parametric curves. In our case I used cubic Bézier curves. A great gif from Wikipedia that I used for reference is below.
Taking the first point as a place on the first pylon, and the last point as a place on the second pylon, a smooth curve is generated between connected pylons by plotting points along the line, and then feeding these into a line renderer. This allowed me to make it so the longer a power line is, the more slack it appears etc. A couple of pictures demonstrating this are here.
As seen in the 2nd picture, we also have multiple different sized pylons. There are small (which deal with low powers), medium (which deal with medium powers) and large ones (which deal with high powers), each of which can only connect to devices that require the same types of power. The larger the pylon, the bigger its range, the greater power it can transfer and the more things it can connect to simultaneously. So why would you need to use anything other than large power poles? Larger power poles carry such a high power that they will likely fry most devices that you would want to connect to power. In real life, since large power poles carry such a high potential difference (voltage), they have a low current and therefore reduce your power loss through transmission since a low current means less heat. I wanted to create a similar system so that it makes your power arrangement in game a bit more thought compelling. Therefore, large power poles are great for transferring power since less power will be lost.
To transfer your power from low to medium, medium to high, high to medium or medium to low, there are transformers. These are step-up and step-down transformers that I have personally been wrestling with for the last couple of days and are still a tiny bit buggy. A small example of how they work is shown in the video below. This video shows how stepping up and then stepping down will help reduce power losses when transferring electricity across your facility. Please note that the power losses likely won’t be as severe as the values shown in the video and are subject to a lot of tweaks and that the power lines going to the bottom left of some of the sprites is purely because I haven’t got round to specifically choosing the points on the sprite where the lines will connect to.
I could keep going but this is really getting quite hefty so I’ll leave it there for now. This has pretty much been my life for the last week and probably for the next week but it’s been quite enjoyable doing something completely new.
Case Closed #
Besides the power system being created, there is not too much else to talk about. @𝕋 𝔻 𝟛 𝟝 𝟘 took a tiny break off after some burnout, but he’s been back optimising dragging to create less lag, which I’m sure he can expand on next time. In any case, thanks for reading and I hope you found this interesting!