Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (739) Games in Android Showcase (224) games submitted by our members Games in WIP (820) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Math rotation boroken  (Read 750 times) 0 Members and 1 Guest are viewing this topic.
ZeLoD
 « Posted 2017-08-11 13:16:49 »

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:
 1  2  3  4  5  6  7 `   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);   `
KaiHH

JGO Kernel

Medals: 445

 « Reply #1 - Posted 2017-08-11 13:49:12 »

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:
 `V * T * Rx * Ry * Rz * S`

but your current code in fact gives you:
 `V * Rz * Ry * Rx * T * S`

Long story short, you need `rotateX/Y/Z` which post-multiplies:
 1  2  3  4  5  6  7 `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:
 1  2  3  4  5  6  7 `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());`
ZeLoD
 « Reply #2 - Posted 2017-08-11 13:56:10 »

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.
KaiHH

JGO Kernel

Medals: 445

 « Reply #3 - Posted 2017-08-11 14:11:45 »

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:
 `V * To * Re * To^-1 * Ro * So`

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'
ZeLoD
 « Reply #4 - Posted 2017-08-11 14:13:47 »

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
KaiHH

JGO Kernel

Medals: 445

 « Reply #5 - Posted 2017-08-11 15:50:48 »

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

 `R = Rz * Ry`

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
 `Rz * Ry`
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.
KaiHH

JGO Kernel

Medals: 445

 « Reply #6 - Posted 2017-08-11 16:07:37 »

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

 `M = Rsy * Te * Rsy^-1 * Rz * Ry * Se`

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:
 1  2  3  4  5  6  7 `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`
Pages: [1]
 ignore  |  Print

 Ecumene (50 views) 2017-09-30 02:57:34 theagentd (76 views) 2017-09-26 18:23:31 cybrmynd (183 views) 2017-08-02 12:28:51 cybrmynd (181 views) 2017-08-02 12:19:43 cybrmynd (188 views) 2017-08-02 12:18:09 Sralse (195 views) 2017-07-25 17:13:48 Archive (745 views) 2017-04-27 17:45:51 buddyBro (879 views) 2017-04-05 03:38:00 CopyableCougar4 (1428 views) 2017-03-24 15:39:42 theagentd (1318 views) 2017-03-24 15:32:08
 List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05SF/X Librariesby SkyAphid2017-03-02 06:38:56SF/X Librariesby SkyAphid2017-03-02 06:38:32SF/X Librariesby SkyAphid2017-03-02 06:38:05SF/X Librariesby SkyAphid2017-03-02 06:37:51
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org