Over the last couple of weeks development on ZER0ED has slowed a bit. Primarily due to the finer details of fit and finishes taking longer, but I’ve had less time to devote to this side project. That being said I have been actively working on improvements. One of the biggest changes involves how the spent fuel charges are animated and it was a deep dive into SKPhysics and SpriteKit. Let’s dive in!

Simple Concepts Big Changes

The original implementation was to have all fuel charges (spent and gained) originate from the source of the interaction. The game would generate fuel charges at the resource. The charges would fall to the Fuel label attracted by the gravity field. Then a simple collision detection was performed and various actions were triggered.

This presented a few problems though. For one thing, the fuel charges are created before they are subtracted from the fuel count. More importantly, though it just didn’t make sense that spent fuel would appear at the resource.

The solution was to use the same gravitational field technique. Instead, the spent fuel would originate at the fuel source and attract them to the Target that consumed that fuel. By doing this the game now triggers the loss of available fuel instantly and makes the visual connection more accurate.

SKPhysics and SpriteKit and BitMasks equal Headaches, Hairpulling, and Finally Success!

To make this idea work I had to rethink the way I handled collisions. In the original implementation since all fuel charges would collide with the same field mask, it was fairly simple. I would assign a field mask to the charge label and match it up. Also, having specific collision types wasn’t necessary because there was only one place for the fuel to collide.

The new idea introduced a substantial amount of complexity to this idea. Now each Target would become a unique gravitational and collision center for the physics engine to deal with. The first step was to start with an enum to define the collision types.

SpriteKit and skphysics - defining bitmasks

After this (which let me just say took a lot of digging around the Apple Documentation to figure out) the rest of the process was fairly straightforward. Step one was to ensure the correct Target attracted the spent fuel. Then I had to ensure that they would collide with that Target and only that Target.

skphysics and spritekit - defining the charge particle.

Then it was just a matter of checking for collisions and triggering actions accordingly. SpriteKit has a built-in function didBegin(_contact: ) which allows you to check for collisions and upon finding a collision trigger actions based upon it.

skphysics and SpriteKit - didBeginContact

As you can see depending on the collision type that occurs different actions will take place. For example, negative charges reduce available fuel and produce a red shockwave. The switch statement is repeated for nodeB.name to capture all possible cases.

Finishing Touches

ZER0ED is coming along nicely. I’m finishing up the artwork and putting the final touches on the code itself. I hope that I’ll have the first public release in a few short weeks.

I want to thank you for coming along this journey with me and I hope that you do enjoy the game that I’ve made. If you want to follow along with my development journey join me on Twitter.



Comments are closed