# Rolling Cube along a path in C4D using math and Xpresso

A couple of days ago I came across this video of Maxon Training Team (channel that I recommend you follow):

This tutorial shows techniques for rolling a cube along a path using XPresso and the new Nodes system. The result is good and in certain situations it can be fine, but it is not precise, it is an approximation. I tried to look if there was a mathematical solution to the problem but in my short search I found only these other methods of solving the problem:

This is another fast and effective approach that leverages the internal physics of C4D.

Here, on the other hand, a solution is offered to obtain a mathematically precise rolling, but it would probably be difficult, to deny me if I am wrong, to adapt it to make the cube follow a path other than the straight line.

So I thought I’d solve the problem mathematically so that the cube can follow a path along a spline. But first I want to show you a short example video where I used my own preset.

The first step was to find a formula that described a 90° rotation.

In the previous drawing a cube whose side is equal to 𝑙 is represented on the left and to its right the same cube after having undergone a rotation of 90° clockwise along its fulcrum 𝐹. The position of a parametric cube, in C4D, coincides with the center of the square in the figure. When the cube rotates around 𝐹 by 90°, the center describes an arc of circumference whose radius is 𝑟.

My atention was focused on the search for two formulas that described respectively the position of the cube and its rotation as a function of the z-coordinate: 𝑦(𝑧), 𝛼(𝑧).

The first equation is found using the Pythagorean theorem:

Therefore:

One can extend the meaning by values of z > l  by introducing the modulo function that returns the remainder of a division. I then get:

The first formula has been found. For the solution of the second problem I use differential calculus.

Bearing in mind that with a denote the arc of circumference subtended by the angle alpha, it can be noted that:

Where da and dz denote the differentials of a and z. In addition:

So you get:

Moving on to integrate:

Solving the integrals yields:

And because:

You get:

Also in this case we can make the same consideration made previously for the first formula to extend its meaning for z>=0, obtaining:

A final term had to be added that takes into account all previous 90° rotations through division without remainder trunc(z/l).

The two solutions for z>=0 have been found.

It is possible to verify with field analysis (XPresso) that the formulas can be modified to work for any value of z. Also, I had to change the alpha sign because of the setup in C4D: moving the cube in the z direction the rotation R.P must be negative. Combining the two observations we obtain the following formulas:

In which where there is ambiguity, the upper sign for  z>0 and the lower sign for z<0 must be chosen.

Here’s how to translate the two formulas for XPresso:

y(z) = sqrt(sqr(l)/2-sqr(l/2-abs(mod(z;l))))

alpha(z) = -abs(z)/z*(acos(1/sqrt(2)-sqrt(2)/l*abs(mod(z;l)))-pi/4+abs(trunc(z/l))*pi/2)

The function abs(z)/z is used to calculate the sign of .  Because of this division it is necessary to distinguish in the XPresso setup the case in which z=0. You can find everything in the attached project file (available soon).

Xpresso setup

I just add that in order to be able to follow a path I have inserted a null object as parent of the cube. The latter will follow the null object along the path drawn by the spline, but will undergo oscillations along y and rotations according to the formulas seen.

I will soon include a demonstration video, the project file and a pdf version of the tutorial.

I hope I have been sufficiently clear and exhaustive and above all that this little work of mine can be useful to someone. In case you want to send me feedback you can do so by contacting me using the contact tab.

EDIT:

Now the preset and the pdf version of tutorial is available HERE.

This site uses Akismet to reduce spam. Learn how your comment data is processed.