# Rotation difficulties

Hi guys,

I've recently started messing around with JMonkey in the hope of using it a bit more seriously later. However, I'm struggling on something quite basic at the moment that I just can't put my finger on! Basically, I'm trying to rotate an object, a cube here for example:

Vector3f axis = Vector3f.UNIT_Y;

angle+=0.01f;

Quaternion qt = acube.getLocalRotation().fromAngleAxis(angle, axis);

acube.setLocalRotation(qt);

(acube is a global variable as is angle.)

Now the above code seems to work fine, the cube rotates on its Y axis no problem. The difficulties start when I try and rotate it on its X or Z axis - it rotates, but not around its centre, it starts rotating around some point that looks like a couple of units below where it is. It almost seems like the centre of rotation needs to be set as the centre of the object and not a couple of units below it, but I've been scratching my head for ages over it and just can't work out how!

The centre of rotation needs to be in the middle of the model if you want to rotate around the middle of the model… Or what are you asking?

What model do you try to rotate? Are you loading it from a file?

Hey,

Thanks for the reply. I'm just trying to rotate a cube (Box) object, nothing flashy at all - but I can't find an option to set the centre of rotation to the middle of the object, and by default it doesn't seem to be set to that. It's probably something really simple that I'm missing, but I just can't find what it is!

So basically, I'm asking how to set the centre of rotation to the middle of an object, so when I execute the following code:

Vector3f axis = Vector3f.UNIT_Z;

angle+=0.01f;

Quaternion qt = acube.getLocalRotation().fromAngleAxis(angle, axis);

acube.setLocalRotation(qt);

…the box rotates around the z axis at the centre of the model (which it isn't doing currently, it's rotating around a point that's a bit below the box.) I've tried playing around with the setCenter method, but it doesn't seem to be having any effect.

how do you create the box?

Box("blah", new Vector3f(), 1, 1, 1);

the 2nd parameter is the center.

Ah, brilliant, that does it! I was assuming from the javadoc (obviously incorrectly) that by the center of the box it was talking about where you wanted the center of the box to be positioned in the world (i.e. muddling it up with initial translation).

Thanks

The centre of rotation needs to be in the middle of the model if you want to rotate around the middle of the model... Or what are you asking?

What model do you try to rotate? Are you loading it from a file?

I've got similar problem, but with model loaded form a file... Do You have any suggestions about that?

then your model maybe got exported with a wrong center, re-center your model before exporting.

I need help with rotating objects loaded from file!

```   protected Node load3ds(String str, float scale)    {        Node r = null;           myDebugPrint("load3ds: trying to load [" + ModelsPath + str + "]");        try        {             MaxToJme myConverter = new MaxToJme();             ByteArrayOutputStream BO = new ByteArrayOutputStream();             URL maxFile = TestMaxJmeWrite.class.getClassLoader().getResource(ModelsPath + str);                  myConverter.convert(new BufferedInputStream(maxFile.openStream()), BO);          r = (Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));             r.setLocalScale(scale);             Quaternion temp = new Quaternion();             temp.fromAngleAxis(FastMath.PI/2,new Vector3f(-1,0,0));             r.setLocalRotation(temp);                   }        catch (Exception e)        {                  ProcessException(e);        }                if (r != null)        {           myDebugPrint("Loaded!");           return r;        }        else        {           myDebugPrint("Model=NULL!");           return null;        }       }```

This ist the code I use for rotation...

```                Vector3f axis = Vector3f.UNIT_Y;                  angle+=0.05f;                  Quaternion qt = getRootNode().getChild(picked_object).getLocalRotation().fromAngleAxis(angle, axis);                  getRootNode().getChild(picked_object).setLocalRotation(qt);```

And this is how the object look like after rotation... it's rotating and facing down at the same time... how to correct this?

After loading the object, you apply a rotation to rotate it correctly.

You should add this correctly rotated object to a new node, to save the rotation.

Later you should only apply further rotations to that node, not to the object itself.

Hmm… after I load object from 3ds file I add it into "occluders" Node, then the "occluders" Node is added into RootNode, so the rotation should be saved (?). How should the code for rotation look like? What I'm doing wrong? And "occluders" Node generally contains more than one object (if it's important?..).

```Node mymodel = load3ds("filename.3ds", 1.0f); occluders.attachChild(mymodel); getRootNode().attachChild(occluders);```

you said:

This ist the code I use for rotation...

```                Vector3f axis = Vector3f.UNIT_Y;                  angle+=0.05f;                  Quaternion qt = getRootNode().getChild(picked_object).getLocalRotation().fromAngleAxis(angle, axis);                  getRootNode().getChild(picked_object).setLocalRotation(qt);```

I guess picked_object is the object you loaded.
I think with that code, you overwrite the rotation you applied after loading the model.
Make sure that picked_object is not the model you loaded, but a parent node of that object.

It works! I love You!  }:-@