# Rotate modelinstance node about its own centerpoint y axis in LiBGDX

I am trying to rotate the node of a modelinstance as follows:

Note: the node and modelinstance are rotating BUT not about their own center Y axis!

 Node node_ = myModelInstance.getNode("boxy", true);

BoundingBox bbox = new BoundingBox();
myModelInstance.calculateBoundingBox(bbox);
bbox.mul(node_.globalTransform/*myModelInstance.transform*/);
Vector3 centerVector_ = new Vector3();
bbox.getCenter(centerVector_);

if (true) {
try {

if (node_ != null) {

Gdx.app.log("information", "Before rotation: " + node_.localTransform.toString());

// Extract the local Y-axis and create a rotation matrix
//Vector3 yAxis = new Vector3(0, centerVector_.Y, 0);

float rotationAngle = MathUtils.degreesToRadians * 45;

Gdx.app.log("information", "Rotation angle: " + rotationAngle);

// Set rotation matrix based on the Y-axis and angle
rotationMatrix.setToRotation(centerVector_.Y/*yAxis*/, rotationAngle);

// Log the rotation matrix
Gdx.app.log("information", "Rotation matrix: " + rotationMatrix.toString());

// Apply the rotation to the node's local transform
node_.globalTransform/*localTransform*/.mulLeft(rotationMatrix);
// node_.globalTransform.getRotation(new Quaternion(),true);
node_.calculateLocalTransform();

//myModelInstance.transform.set(node.localTransform);

// myModelInstance.transform.set(node.localTransform.mulLeft(rotationMatrix));

// Log the local transform after applying rotation
Gdx.app.log("information", "After rotation: " + node_.localTransform.toString());

// Recalculate the transforms to update the hierarchy
//myModelInstance.calculateTransforms();

Gdx.app.log("error44", "Rotation applied successfully.");
} else {
}
} catch (Exception e) {
e.printStackTrace();
Gdx.app.log("error44", "Exception: " + e.toString());
}
}


I need the modelinstance and its node to rotate about the Y axis of the centerpoint of the modelinstance! How to do this??

Here is a picture of the redbox modelinstance that is supposed to rotate in place about its own y axis.. we can say the normal to the top face!

• I've now had to close two copies of this question you posted as duplicates, so I'll remind you that in future you should post each question once, and make edits or answers to that original question instead of posting copies. This avoids leaving dead ends for future readers searching for how to solve this problem, and keeps all the best answers in one place so they're easy to find. Editing or answering an existing question bumps it up in the main feed, so there's no disadvantage vs. creating a new post. Commented Aug 1 at 8:26
• @DMGregory hey I will keep that in mind! Yes, I should have modified the original post! I hope you deleted it already!
– i_o
Commented Aug 1 at 8:29
• I've closed them as duplicates with a link pointing to this version, so that readers who stumble onto those posts will be able to find their way to your answer here. Commented Aug 1 at 8:55

Ok after trying many different ways possible, I finally found the answer!

The following question post deserves all the credit: click

The idea is that you have to translate your modelinstance to the vector where you will do the rotation, Then, rotate, then translate back out of the vector! Here is the code in Libgdx:

Matrix4 m4 = new Matrix4();
m4.set(modelInstance.transform);
m4.translate(10,0,10).rotate(0,1,0,-20f*playerRotationSpeed*deltatime).translate(-10,0,-10);
//Gdx.app.log("multiply","we have: " + centerVector_.cpy().scl(-1f).toString());
modelInstance.transform.set(m4);
modelInstance.calculateTransforms();