3D Rotation Along Local Axis (Not Global) Here's the Code. What's wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

3D Rotation Along Local Axis (Not Global) Here's the Code. What's wrong?

The following code rotates an object in 3 dimensions: cosx = Math.cos(radx); sinx = Math.sin(radx); cosy = Math.cos(rady); siny = Math.sin(rady); cosz = Math.cos(radz); sinz = Math.sin(radz); //x-axis tx = x - cx; ty = y - cy; tz = z - cz; y = (ty * cosx) + (tz * sinx) + cy; z = (tz * cosx) - (ty * sinx) + cz; //y-axis tx = x - cx; ty = y - cy; tz = z - cz; x = (tx * cosy) + (tz * siny) + cx; z = (tz * cosy) - (tx * siny) + cz; //z-axis tx = x - cx; ty = y - cy; tz = z - cz; x = (tx * cosz) + (ty * sinz) + cx; y = (ty * cosz) - (tx * sinz) + cy; Since I rotate X first, X turns fine along its axis, but the Y and Z rotate to the global Y and Z instead of their own axes. However, if I rotate Y first, Y turns fine but X and Z doesn't. Similarly, if I rotate Z first, Z turns fine but X and Y doesn't. Any help as to how I can make them relative to each other? See draw() function in the JS section. Approx line: 160 https://code.sololearn.com/WxtqC6Ji8ZkL/?ref=app

28th Jun 2018, 3:27 PM
Andre Daniel
Andre Daniel - avatar
2 Answers
0
At once I tried using the matrices to quaternions formula and applying it...in the end after 4 days it failed so I just moved on and now face a new issue (that I'm researching of course) Frustrum qnd occlusion culling.
2nd Jul 2018, 12:26 PM
Andre Daniel
Andre Daniel - avatar