# SpatialTransform pivot

Hi, I have a simple Box and I want to rotate it by the middle axis (that is, an axis that is in the middle of the box), so the box will almost stay in place and rotate.

Using SpatialTransformer and the code from HelloAnimation the box rotates well, but always from one of the edge axis.

How do I set the axis in the middle of the box?

Here is a code snippet:

```                 SpatialTransformer st = new SpatialTransformer(1);                 // I tell my spatial controller to change pivot                 st.setObject(g, 0, -1);                 st.setSpeed(2);                 // Assign a rotation for object 0 at time 0 to rotate 0 degrees                 // around the z axis                 Quaternion x0 = new Quaternion();                 x0.fromAngleAxis(0, new Vector3f(0, 1, 0));                 st.setRotation(0, 0, x0);                 // Assign a rotation for object 0 at time 1 to rotate 180                 // degrees around the z axis                 Quaternion x180 = new Quaternion();                 x180.fromAngleAxis(FastMath.DEG_TO_RAD * 180, new Vector3f(0,                         1, 0));                 st.setRotation(0, 1, x180);                 // Assign a rotation for object 0 at time 2 to rotate 360                 // degrees around the z axis                 Quaternion x360 = new Quaternion();                 x360.fromAngleAxis(FastMath.DEG_TO_RAD * 360, new Vector3f(0,                         1, 0));                 st.setRotation(0, 2, x360);                 st.interpolateMissing();                 g.addController(st); ```

Changing the Vector3f from

``` x0.fromAngleAxis(0, new Vector3f(0, 1, 0)); ```

Doesn't alter the axis...

Any ideas?

Thanks

you need to understand transformations.

rotation is always performed first, secondly the LocalTranslation is concatenated (in one Spatial).

if you construct a Box with one corner to be at (0, 0, 0) and the other corners in positive directions,

then you will need to translate it first by (-0.5width,-0.5height,-0.5*depth).

You can do so by either of these 2 ways:

1)

Use two Nodes/Spatials - the first with the LocalTranslation, the second with LocalRotation and attached to the first Node. The second Node needs then to be controled by the SpatialTransformer, to rotate correctly.

2)

For a simple Box, use the alternative constructor Box(String name, Vector3f center, float xExtent, float yExtent, float zExtent), e.g. in the following way:

Box("my box", new Vector3f(0,0,0), 1.0f, 1.0f, 1.0f) - that will create a box with the center being at (0,0,0) and each edge having length 1.0

I've tried the second approach (creating boxed by setting center and exents) and it doesn't work.

The code for the SpatialTransform is the same.

Going to do some voodoo programming on the first approach… Nope… clearly I have no idea  :oops:

Anyone can give me a hand?

Thanks

Suggest you look at some concepts on world space and local space

It's working now, but frankly I don't know why it's working!

After looking at some code on TestSpatialTransformer, this is the working code:

```         b = new Box("b0", new Vector3f(-1, -1, -1),                 new Vector3f(1, 1, 1));         b.setLocalTranslation(new Vector3f(dist, dist, 0));         b.setModelBound(new BoundingBox());         b.updateModelBound();         ...                 SpatialTransformer st = new SpatialTransformer(1);                 st.setObject(b, 0, -1);                 Quaternion x0 = new Quaternion();                 x0.fromAngleAxis(0, new Vector3f(0, 1, 0));                 Quaternion x90 = new Quaternion();                 x90.fromAngleAxis((float) (Math.PI / 2), new Vector3f(0, 1, 0));                 Quaternion x180 = new Quaternion();                 x180.fromAngleAxis((float) (Math.PI), new Vector3f(0, 1, 0));                 Quaternion x270 = new Quaternion();                 x270.fromAngleAxis((float) (3 * Math.PI / 2), new Vector3f(0,                         1, 0));                 st.setRotation(0, 0, x0);                 st.setRotation(0, 1, x90);                 st.setRotation(0, 2, x180);                 st.setRotation(0, 3, x270);                 st.setRotation(0, 4, x0);                 st.interpolateMissing();                 b.addController(st); ```