The Power of Hexagons: Cube in a Spin with Swift


In an earlier post, rotation was considered and here I consider spinning a cube.



In order to achieve a clockwise spin:

  • index 0 follows the path of the hexagon's surrounding radius to index 5
  • index 1 follows the path of the hexagon's surrounding radius to the index 0
  • origin follows the path of a circle (centred on index 0) to the index 1
  • index 2 follows the path of a circle (centred on index 3) to the (rear) origin
  • index 3 follows the path of the hexagon's surrounding radius to index 2
  • index 4 follows the path of the hexagon's surrounding radius to index 3
  • rear origin follows the path of a circle (centred on index 3) to index 4
  • index 5 follows the path of a circle (centred on index 0) to the origin

The rotation happens for a distance of 60 degrees and then repeats. The colours fading between one another appropriately to reproduce a very basic imitation of light shining on the surfaces. The code leverages the CAKeyframeAnimation class to stitch the rotating paths together.

The rough results of which can be seen here (note starts about 3 or 4 seconds in):




To reverse the orientation, it would be necessary simply to reverse the array of values in the CAKeyframeAnimation. And adjust the colouring accordingly.

Zooming

Combining the spinning with a zoom effect is also possible.

The zooming in the video was achieved by simply altering the radius used to calculate the points of the hexagon/cube (proportionally to the spin rotation of the cube) rather than keeping it static.

Rotating prisms

Being able to spin a cube, in addition to being able to rotate a cube, means being able to turn the perspective of anything constructed from within the boundaries of a cube logic.
For the code see here.

Looking to the future

One of the future things I want to look at, alongside future posts on animating 3D-objects, is the stylistic sharpening of perspective.

Working with 3D spaces isn't all about the type of realism we find in games like The Longest Journey, it's about bring together a cohesive world and this is just as evident in games like Monument Valley.

The advantage that Monument Valley has over The Longest Journey is that we suspended our disbelief going into the game, whereas in The Longest Journey we're always testing what we see against reality. Being wowed by what is done well, but always aware of any movement or thing not convincingly formed.

Despite attempts like Euclideon to scan the whole world into a game, there will still be the space for games that use stylised 3D, like Monument Valley. For at least two reasons: (1) because even when we push things as far as Euclideon, the telling signs of realism failing to represent the real are there (2) we marvel at alternate representations and their ability to create impossible worlds, touching on elements and experiences in the real world that realism cannot capture.



Endorse on Coderwall

Comments