# Rolling a box around its edges

Hi,

second post ever, with a more respectable question…

I am trying to make a box roll around its edges based on user input i.e. the box is on flat surface and it should rotate at 90 degs (+ or -) aroud on of its "lower" edges.

having read some of the discussions on the forum, I think I should be able to achieve this by attaching the box to a pivot node, set a local translation on the box and rotate the pivot node. The idea is: translate the pivot node so that it is coinciding with the north-east lower vertex when I want to rotate "north" or "east" and is

coinciding with the south-west lower vertex when I want to rotate "south" or "west".

for example when I want to rotate north:

find the north-east lower vertex with getWorldCoords()

get the world displacement as the difference between the "future" pivot translation and its current translation

transform the world displacement into local according to the rotations of the pivot node.

subtract the transformed displacement from the current local translation of the box

move the pivot node

updating the pivot node position:

```pivotTranslation.set(minX,minY,maxZ); pivotTranslation.subtract(pivot.getWorldTranslation(),worldDisplacement); localDisplacement = (pivot.getLocalRotation().mult(worldDisplacement)); <---??? currentTranslation = cube.getLocalTranslation(); cube.setLocalTranslation(currentTranslation.subtractLocal(localDisplacement)); pivot.setLocalTranslation(pivotTransaltion); ```

How should I convert the worldDisplacement to localDisplacement to be subtracted from the current local translation of the box?
I also tried localDisplacement = (pivot.getLocalRotation().toRotationMatrix.mult(worldDisplacement));

I am able to rotate only north and south or only west and east, but when I try a combo of say north and then west,
things get ugly.

illustation:

http://www.postyourimage.com/view_image.php?img_id=6JC0FIb7RJWQCrQ1191862479
worldDisplacement (0, 0, -2)      localDisplacement (0, 0, -2)  cool

http://www.postyourimage.com/view_image.php?img_id=jjZXbqyqk50QJ7j1191862517
worldDisplacement(-6, 0, 0)      localDisplacement(-6, 0, 0)  cool

http://www.postyourimage.com/view_image.php?img_id=lBqQ3jTrMJMU04s1191860109
worldDisplacement(-2, 0, 0) localDisplacement(0,-2 , 0)    not cool, I want (0, 0, -2)

I am also having the problem of updating the axis of rotation. Obsiously after some rotations of the pivot node,
the (1, 0, 0) is no longer the x axis as seen by the viewer. Right now I am undoing the rotation made on the pivot
node to the (1, 0, 0) but I am not sure this is the right approach.

thanks

Hi,

just for the record, I got it working by using matrices. In my case (since all the rotations were on 90 degs) this was in fact quite simple; I keep track of all the rotations by multiplying by the corresponding matrix each time I make a move; in the end the current rotation is represented by the product.

I still don't know why: localDisplacement = (pivot.getLocalRotation().mult(worldDisplacement)); did not work though.

anyway…

cheers

Glad to hear it… I don't know why it would not work, but I am always happy when someone uses a direct math solution in contrast to a API usage…