Math rotation boroken

hell I use lwjgl with jom and tryed to make to rotate my object in it’s local y axes but insted of rotation it’s start to fly around the local axes.
can anyone help?
code is here:


   Vector3f rotation = gameItem.getRotation();
            modelViewMatrix.identity().translate(gameItem.getPosition()).
                    rotateLocalX((float)Math.toRadians(-rotation.x)/2).
                    rotateLocalY((float)Math.toRadians(-rotation.y)/2).
                    rotateLocalZ((float)Math.toRadians(-rotation.z)/2).scale(gameItem.getScale());
            Matrix4f viewCurr = new Matrix4f(viewMatrix);
            return viewCurr.mul(modelViewMatrix);   

M.rotateLocal*() is equivalent to R * M (pre-multiplication) and not M * R (post-multiplication), where R is the intermediary rotation matrix computed by the method, as is also mentioned in the JavaDocs.
Hence, the “Local” is not meant as: “local to the coordinate system represented by the given matrix”, but “local to the coordinate system which that matrix transforms [any vector into]”.

Essentially, you want: [icode]V * T * Rx * Ry * Rz * S[/icode]
but your current code in fact gives you: [icode]V * Rz * Ry * Rx * T * S[/icode]

Long story short, you need rotateX/Y/Z which post-multiplies:


Vector3f rotation = gameItem.getRotation();
modelViewMatrix.identity().translate(gameItem.getPosition()).
		rotateX((float)Math.toRadians(-rotation.x)/2).
		rotateY((float)Math.toRadians(-rotation.y)/2).
		rotateZ((float)Math.toRadians(-rotation.z)/2).scale(gameItem.getScale());
Matrix4f viewCurr = new Matrix4f(viewMatrix);
return viewCurr.mul(modelViewMatrix);

or equivalently and more efficiently:


Vector3f rotation = gameItem.getRotation();
return new Matrix4f(viewMatrix)
		.translate(gameItem.getPosition())
		.rotateX((float)Math.toRadians(-rotation.x)/2)
		.rotateY((float)Math.toRadians(-rotation.y)/2)
		.rotateZ((float)Math.toRadians(-rotation.z)/2)
		.scale(gameItem.getScale());

thank you for the fast asnwer I tryed your code but the goal was to transfer the base vector as well so I could achive an earth like rotation.

What do you mean by “base vector” and what exactly are you trying to achieve?

If you want an object ‘o’ positioned on the Earth’s surface to stay put at the same place on the Earth’s surface when you rotate the Earth (i.e. ‘o’ should rotate along with Earth), then you must make sure that when you rotate ‘o’ that the rotation pivot is the Earth’s center and not the center of ‘o’.

So, given that you already placed ‘o’ at the correct position on the yet unrotated Earth, and the Earth’s center is the origin, then you want this: [icode]V * To * Re * To^-1 * Ro * So[/icode]
where:

  • V is your view matrix (as above)
  • Re is Earth’s rotation
  • To is the translation from Earth’s center (origin) to the position of ‘o’ on the unrotated Earth
  • To^-1 is the inverse of To (so for a translation this means negating all components)
  • Ro is the rotation of ‘o’ as it should be for the unrotated Earth
  • So is the scaling of ‘o’

in the x,y,z axes cordinate system earth hase a 30 aligned rotation around it’s own axes that’s what i try to achive sorry for the bad english

If you want to have an “Axial tilt” or “Obliquity” applied to Earth’s +Y rotation axis, then the rotation part of Earth is:

[icode]R = Rz * Ry[/icode]

where:

  • Rz is a rotation around the +Z axis to achieve the Axial tilt or Obliquity (~ 23.437 degrees according to Wikipedia)
  • Ry is the rotation around Earth’s rotation axis (+Y in this case)

You have to understand that every successive (post-multiplied) transformation step transforms the whole coordinate system and all transformations afterwards are relative to this new coordinate system.
So, the idea with [icode]Rz * Ry[/icode] is to first tilt Earth’s coordinate system (in this case on the XY-plane). Now, that we got the coordinate system tilted, we just rotate around its +Y axis normally.

Btw. here is the formula for letting the Earth rotate around the Sun including the axial tilt:

[icode]M = Rsy * Te * Rsy^-1 * Rz * Ry * Se[/icode]

where:

  • Rsy is the rotation of Earth around the Sun
  • Te is the translation of Earth relative to Sun (e.g. (5, 0, 0))
  • Rz is the axial tilt rotation
  • Ry is Earth’s own rotation around its own +Y axis
  • Se is Earth’s scaling factor (given that Sun has factor = 1.0)

Just post-multiply that to your view matrix and you’re golden.

Some notes: Rsy^-1 is used to cancel the orbital rotation of Earth around the Sun for the later applied axial tilt to not rotate along with Earth around the Sun but stay fixed.

Now lets translate this to JOML-speak:


Matrix4f M = new Matrix4f();
M.rotationY(orbitAngle)      // <- Rsy
 .translate(distToSun, 0, 0) // <- * Te
 .rotateY(-orbitAngle)       // <- * Rsy^-1
 .rotateZ(tiltAngle)         // <- * Rz
 .rotateY(earthRotation)     // <- * Ry
 .scale(earthScale);         // <- * Se