Rolling cuboïd Rig

Hello everyone!

I’m currently trying to find a way to create dynamic pivots and I was wondering what you think would be the best approach to set up a “rolling cuboid,” like in this example:

rolling_cuboïd_concept_demo

Ideally, in terms of interface, it would be great to have a single control that allows the object to roll in the desired direction.

I may have found a solution based on this video:

ezgif-636713de71fe49

It involves a hierarchy of three transforms:
We move the parent to the edge on which we want to rotate our object,
then we compensate by moving the child in the opposite direction to return it to its initial position in space,
and then we can perform our rotation.
Finally, we move the master to the new position of the object and realign the children so their pivots are aligned.

Well, I haven’t tested “automating” all this yet, but I did some tests with a cuboid.

ezgif-6e1526f1d6e920

I have a vague idea of how I could script this, but I’m not comfortable with matrices, so I have many assumptions, but practically, I don’t really know how to apply them.
So I thought the idea would be to:
Based on the controller input (the direction),
find the edge closest to the ground (if the input is → +X, retrieve all the vertices with a positive X value and then find the two with the lowest Y). Now, practically, I don’t know how I can get the coordinates of these vertices in the space of my master(red boundingBox).
Then, since we have two vectors, we can find the midpoint, but regarding orienting the yellow transform along the axis, I have a vague idea but no concrete solution.
And then, this is where it completely overwhelms me: to compensate for the movement of my yellow transform, simply reversing the values of rotation and translation isn’t enough, so I imagine it relates to the yellow pivot not being the same as the gray pivot.
And similarly, for the other steps, I have no idea how to proceed, nor even if this is a solution that works with an object that isn’t a cube, so there you go!
I would really like to know what you think, and if I should go in a completely different direction (because even from a practical standpoint, I imagined scripting a custom node; I can hardly see how I could do that with factory nodes).