The Power of Hexagons: Dig your own holes in Swift (CAShapeLayer, CGPath, 3D Drawing)



This post is all about digging holes, but the first questions is how do we draw the ground? The answer to which is very simple, the ground is created using the base of a cube, which is repeated and used to tile the necessary area.

As before we know the placement of the tile because it is drawn within a hexagon placed on one of the vertices of the hexagon that it will sit next to.

In the code you'll find both a positionNextCube() function and an enum (CubeRelativePosition) for determining where the next block will be placed. There's also a drawCubeBase() function to draw the tile (which could also be used for drawing the hidden base of a cube in the same position).

Digging the hole

The shape of the tile that we are using to draw the ground is a particular shape. It is a rhombus and a rhombus is constructed from two equilateral triangles. This is convenient for creating the corner of a hole, as you will see.


But first it is important to understand that drawing, especially in 3D, is all about light and shade. Objects take on the majority of their shape through our perception of where light is coming from. A hole is underground and so has less light than the ground above it. It will therefore be darker. But one side of the hole will still be lighter than the other because it will be exposed to more light due to the way the sun is illuminating it, whereas the other side will lack light or have a shadow cast on it.



Where one side meets the other is where we'll split the rhombus in two and make one side light and one side shadow. We could use a single split rhombus to make a hole but let's be a bit more adventurous:


We've dug a two cube deep hole by placing one split rhombus above another and then filling in the adjacent tiles.

Conclusion

Thanks for tuning into the latest power of hexagons post, please explore the code and play around with what's possible. Next time will either be animation or placing a cube on a slope, or both. I haven't quite decided yet.




Endorse on Coderwall

Comments