# Rotate a Cube on a Plane Using Drivers

This is a simplified example which I wish to apply to a more complicated animation situation.

I have a cube, that is 1x1x1 in size, origin at the centre and is located at (0,0,0). It sits atop a plane with the origin of (0,0,-0.5).

I want to use drivers to drive the cube's X and Z coordinates when I change its Y rotation so that the cube does not pass through the plane. Like so:

I know the X and Z locations can be calculated using cos and sine but I'm having trouble figuring out the equation for the drivers.

• – batFINGER Dec 23 '16 at 12:36
• @batFINGER Damn it, hadn't found your example. It updates irregularly though. When I run it, the edge doesn't stick to the ground. – Leander Dec 23 '16 at 12:52

This could be a math question, but since we are using Blender we could use drivers tools, f-curve tools and empties.

### Analysis

Even though the objects center is at XZ (0, 0) the center of the first half rotation is at (1, -1).

We will need the trigonometric functions sinus and cosinus to get points on a circle.

Note that the object center and the center of revolution do not have a distance of 1. We will have to calculate the distance with the pythagorean formula.

Python

a**2 + b**2 = c**2


The are the relations of sinus cosinus and the angle. Since the values only apply to a circle with the radius of 1, we need to multiply if by the actual radius of the circle.

### Setting up the drivers

1. Add an empty. (This is necessary, because of how Blender editing interface works. If the rotation drives the location, Blender 3D Viewport editing rotation control behaves unexpectedly.)
2. Add a driver to the Y rotation of the cube.

Set the empty's Y rotation as the input. Use world space.
3. Add a driver to the X and Z rotation, use the same settings.
4. Add the basic trig functions as the expression.

X Loc: cos(var) Z Loc: sin(var)

Our cube now move in a circle based on the empty's rotation.

### Refining the expression.

Now we can work on the expressions. The Y rotation is negated (clockwise rotation results in positive values). Let's negate X Loc.

X Loc:    -cos(var)


Our cube revolves in the correct direction.

Next, offset the value to make our cube's center rotate around XZ (1, -1). The first center of revolution.

X Loc:    -cos(var) + 1
Z Loc:    sin(var) - 1


Currently, the cube revolves from 0° to -90°, with an input from 0° to 90°. (Marked red)
We need to offset the rotation, resulting in -45° to -135°. That is an eighth of a circle. A circle measure 2 * pi.

1/8 * 2 * pi = pi / 4


Add this to the var in both trig functions.

X Loc:    -cos(var + pi/4) + 1
Z Loc:    sin(var + pi/4) - 1


Now adjust the radius of the circle. Our distance is calculated using the pythagorean formula.

1**2 + 1**2 = c**2
c = sqrt(2)


Multiply the trig calculated offset with the distance c.

X Loc:    -cos(var + pi/4)*sqrt(2) + 1
Z Loc:    sin(var + pi/4)*sqrt(2) - 1


The cube describes the desired rotation perfectly.

For the X and Z Location it would be beneficial to have a cyclic input. The span of 0° to 90° from the empty should be repeated. I am going to use the modulo operator to do that.

var%2    ...    rest of var / 2, x in [0, 2[
var%(pi/2)     cyclic for every quarter of a circle, ranging from 0° - 90° (in radians)


The result is cyclic, albeit without an offset.

X Loc:    -cos(var%(pi/2) + pi/4)*sqrt(2) + 1
Z Loc:    sin(var%(pi/2) + pi/4)*sqrt(2) - 1


Finally, let's add the offset to the X Location. Every cycle of 90° we have to add 2 to all X Location values.

First, isolate the cycle of 90° (pi/2) using the floor() function. Then multiply the result by 2.

x = floor(var / (pi/2)) * 2   #
x = floor(2*var / pi)*2       # one step simpler/prettier


Add this to the existing X Loc expression.

X Loc:    -cos(var%(pi/2) + pi/4)*sqrt(2) + 1 + floor(2*var / pi)*2
Z Loc:    sin(var%(pi/2) + pi/4)*sqrt(2) - 1


### Getting things more production ready

Add a floor plane on (0, 0, 0). Move plane (0, 0, -1) in edit mode and scale it accordingly.

Lock the X and Z rotations on the empty.

Parent the empty and the cube to the floor plane.

Looking good!

• A very detailed and helpful answer which I very much appreciate. – BlenderBro Dec 23 '16 at 15:10