
Rotating 3D Shapes - WoodenChair
https://www.khanacademy.org/computing/computer-programming/programming-games-visualizations/programming-3d-shapes/a/rotating-3d-shapes
======
daenz
If you're wondering why the 3d cube doesn't quite look 3d, it's because the
perspective transform hasn't been applied. Imagine taking a camera's view
frustum (the rectangular "cone" from a camera's POV) and then re-forming the
frustum and to be a unit square, while non-linearly squishing all of the
geometry inside of the frustum as it turns into that square. The result would
be that vertices of geometry further away from the camera would shrink towards
the camera's vanishing point. Rendering this result would make geometry appear
to be in perspective.

~~~
romwell
Also if you are wondering why the UI feels so horrible, it is because it _is_
horrible.

In particular, it does not feel as if you were spinning a globe with your
finger, because you need better math to do that: quaternions.

Play with this[1] and just _feel_ how much better it is.

I tried to make the source understandable, but there are many, many, many
other implementation of the Arcball interface out there. Find an explanation
and code that you like and roll with that.

[1][http://romankogan.net/math/arcball_js/index.html](http://romankogan.net/math/arcball_js/index.html)

------
th0ma5
While a related but more advanced topic, I still can't quite intuit
quaternions. But of course we're all fortunate to not need to understand them
completely to use them.

~~~
colinthompson
3Blue1Brown has a great video on quaternions:

[https://www.youtube.com/watch?v=d4EgbgTm0Bg](https://www.youtube.com/watch?v=d4EgbgTm0Bg)

~~~
th0ma5
Yeah, I have seen this... Still pretty mind boggling though :P

------
Amboto2205
Woah, all this is so complicated! I would rather have them individually
explain what each variable/math stuff does here to know its importance. Can
anyone explain it?

~~~
romwell
It's needlessly complicated, and a horrible way both to understand and code
rotations in 2D and 3D.

------
Stay_frostJebel
Why is pmouseX subtracted from mouseX? And why is pmouseY subtracted from
mouseY?

~~~
ghusbands
They're using the difference in mouseX (or mouseY) to dictate the amount to
rotate the object. To do that, you subtract it from the previous one (mouseX-
pmouseX).

------
romwell
TL;DR: don't read this article, it is amazingly bad. Avoid trig. Look up
"arcball interface"[0].

\-------------------------------------------------------

Strong opinion from a math PhD/SWE:

You'll be better off not reading this at all than learning about the subject
from this page. You'll have to un-learn it afterwards.

Most importantly, whatever you do, please NEVER write a 3D rotation function
and UI this way. It is horrible for many reasons I won't go into here. DO
write and use the so-called _arcball interaface_. Here's an implementation you
can play with in a browser[0]. Try it now!

The article is is amazingly bad: not only it is not the right way to think
about rotations, it's _also_ bad computationally, features bad code, and
nothing is said of the _right_ way to do these things (i.e. how it's actually
done by anyone who deals with it).

It also features some "magic math", i.e. unexplained trig formulas. Feel
confused? You should be, unless you know this stuff already!

Rotations aren'ta reason to _learn trigonometry_ ; if anything, it's a reason
to _avoid_ trig. In fact, you should avoid trig when you can. (People smarter
than me say so too[1]).

Here are the subjects one need to learn to actually deal with rotations:

* Linear algebra. Rotations are orthogonal transformations. And by using projective coordinates, translations are linear maps. OpenGL stack is all about 4 x 4 matrices!

* Complex analysis and quaternions. _All_ complex number _are_ is rotations & scaling in 2D (the sqrt(-1) stuff is not even necessary to define them). Quaternions generalize complex numbers, and provide an efficent way to interpolate rotations in 3D. Again, see[0].

* Lean about Euler Angles so that you know to _not_ use them.

As a bonus, the unexplained trig identities (like the formula for _sin(a+b)_ )
come off naturally from either matrices or complex numbers.

Note that this is not all there is to it; I still used trigonometry in that
example. This is altogether not necessary, see Inigo Quilez's article [1].

 _Most importantly_ , understand that the operation "rotate an object so that
it points in _that direction_ " doesn't require neither angles nor
trigonometry. You just need a way to encode the _direction_ (as a vector,
quaternion, etc).

[0][http://romankogan.net/math/arcball_js/index.html](http://romankogan.net/math/arcball_js/index.html)

[1][http://www.iquilezles.org/www/articles/noacos/noacos.htm](http://www.iquilezles.org/www/articles/noacos/noacos.htm)

~~~
ghusbands
You're pushing arcball really quite hard across this discussion, but it's just
one of many ways of handling interactive rotation of an object, and the one
used in the article (give or take accumulating inaccuracy) is fairly normal,
too. Certainly, though, the article will not really help anyone aspiring to
write a 3d engine of most any kind.

