The ability to roll cubes on the ground in 3D seemed elusive, yet the math would surely be graspable to someone with an elementary training in maths and python scripting. These are the results of my endeavour, one is straight forward, one is more complex -both of which have been documented as a tutorial into rigging and scripting in Blender 2.5- and the last is the simplest and most fun to integrate, which was only thought of and applied last.
The animation piece involves the use of the following in Blender 2.5:
- Using drivers to roll and bounce cubes.
- Using python scripting to roll a cube on one and two planes
- A python adoption of Conway's Game of Life in Blender
The process was documented for educational purposes, and can be found below. It consists of a video tutorial series split into three chapters and a written explanation of the techniques that are not covered in the video tutorials. @ The more advanced scripters and programmers: any advice or corrections you may have to offer with regards to my work here is greatly appreciated. Blender's API had been developing rapidly, and there may be better solutions I have not been aware of.
The video tutorial series covers the following:
- Creating a rig with armatures to roll a cube, which makes use of an Action constraint and control bone for animation.
- Deconstructing the rig and writting a python script to animate the cube. This encompasses rolling a cube in two planes (forward, backward, upward and downward)
- These are split into 3 Chapters: Chapter 1 involves the rig, Chapter 2 and 3 involves the scripting.
The technique of using drivers and the adoption of Conway's Game of Life is illustrated below in this article.
- 01 - Backwards Forwards
- 02 - Check Orientation
- 03 - Switch Orientation & set key
- 04 - Roll Cube
- 05 - Upwards Downwards - 01
- 05 - Upwards Downwards - 02
- 01 - Determine Quadrant
- 02 - Check Orientation
- 03 - Switch Orientation - 01
- 03 - Switch Orientation - 02
- 04 - Roll Cube
- 05 - Testing - 01
- 05 - Testing - 02
- include "import bpy" at the top of the script
- Do NOT include the poll functions
- Do NOT include the Registration of operators at the bottom
The driver technique makes use of a sin equation to approximate the cube's roll. This works and is believable, but if you look closely you'll see the corner slip somewhat, a result of the sin graph not perfectly representing a circular arc. An equation representing a circle works perfectly, and while Bassam figured this out and showed me how its done, I haven't been able to do it yet and won't be referring to it. Generally this technique requires one controller bone which is keyed.
This is how it works:
The X Location of a bone drives the roll movement of the cube. The movement of the cube is driven in three parts: Z Location (Up and down), X Location (Forward and backward) and Y Rotation (for rolling). While the cube rotates it will move both in a horizontal direction and in a vertical direction to mimic pivoting from its corners.
Z Location (Up and down):
A sin function is used to mimic the cyclic up and down movement. The absolute value of this is used for the bouncing effect. The peak of the sin wave is it's highest point, which is the length from the center of the cube to it's corner. The cubes length is 2units, therefore the center of the cube is 1unit in X and 1unit in Y from any corner. The distance from center to corner is the hypotenuse of a right angle triangle, which as per pythagorus will be sqrt(1^2 + 1^2) == sqrt(2). The center of the cube will start at distance 1 and move up to a height of sqrt(2), therefore the displacement over this period will be sqrt(2) - 1.
X Location (Left and right):
The X Location of the cube is equal to the X Location of the control bone.
Y Rotation (roll):
The cube rolls 90 degrees (pi/2) each time it travels a distance equal to its length. It's length is 2 units, therefore it moves distance*(pi/2)/2
Conway's Game of Life
Being on the topic of cubes, my friend Greg -the sound guy roped into all the animation I do- introduced me to 'Conways game of Life' - an insanely cool and simple algorithm for generating 'life-forms'. We adopted the algorithm in Python for Blender 2.5 from G-Do's post here and had fun generating two patterns. The first is simple and loops, the other is a more complex interaction on a larger grid. Have a look at some of the life forms which have been discovered - they're beautiful! In a nutshell, the python script would generate a specific sized grid of cubes and link them to a default material, the pattern of 'live' cubes would also be declared in the script. A button is presented in the UI which steps forward one frame and updates the grid. Depending on how old a cube is it'll get assigned a specific material colour, when it's dead it becomes black. The rendering of the animation was done from the command line which invoked this python script in a loop, with a little flag to render and save the frame images.