Note that there are some explanatory texts on larger screens.

plurals
  1. POCould someone explain the math behind how this cube-rotating script works?
    primarykey
    data
    text
    <p>If you go to the following link you'll see a really cool Javascript simulation of a cube that rotates based on your mouse position. </p> <p>Simulation: <a href="http://maettig.com/code/javascript/3d_dots.html" rel="nofollow noreferrer">here.</a></p> <p><img src="https://imgur.com/e2HTy.png" alt="alt text"></p> <p>If you view the source of the cube rotating script you'll see:</p> <pre><code>&lt;script type="text/javascript"&gt; /* I wrote this script in a few minutes just for fun. It's not made to win any competition. */ var dimension = 1, a = 0, b = 0, i = 27; while (i--) document.write('&lt;b id="l' + i + '"&gt;+&lt;/b&gt;'); function f() { i = 0; for (x = -dimension; x &lt;= dimension; x += dimension) for (y = -dimension; y &lt;= dimension; y += dimension) for (z = -dimension; z &lt;= dimension; z += dimension) { u = x; v = y; w = z; u2 = u * Math.cos(a) - v * Math.sin(a); v2 = u * Math.sin(a) + v * Math.cos(a); w2 = w; u = u2; v = v2; w = w2; u2 = u; v2 = v * Math.cos(b) - w * Math.sin(b); w2 = v * Math.sin(b) + w * Math.cos(b); u = u2; v = v2; w = w2; var c = Math.round((w + 2) * 70); if (c &lt; 0) c = 0; if (c &gt; 255) c = 255; s = document.getElementById('l' + i).style; s.left = 300 + u * (w + 2) * 50; s.top = 300 + v * (w + 2) * 50; s.color = 'rgb(' + c + ', ' + c + ', 0)'; s.fontSize = (w + 2) * 16 + 'px'; /* The Digg users missed depth sort, so here it is. */ s.zIndex = Math.round((w + 2) * 10); i++; } } /* Using a timer instead of the onmousemove handler wastes CPU time but makes the animation much smoother. */ setInterval('f()', 17); &lt;/script&gt; </code></pre> <p>I've looked over it several times, and I still don't understand how the points for the cube are calculated. Is this using "Euler Rotations"? One of the big problems I've had is the use of single letter variable names that mean nothing to me.</p> <p>Would anyone with the requisite math knowledge help explain how the math works behind rotating the cube in this simulation? I'd like to make similar things but when it comes to calculating the points positions, I'm a little lost.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload