From ad4f02c8753fbf2db8b9912af8b495c01c3840c4 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Tue, 19 Dec 2023 15:55:10 +0300 Subject: [PATCH] asdf --- cube.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/cube.js b/cube.js index 489bfdb..52b3d3f 100644 --- a/cube.js +++ b/cube.js @@ -97,16 +97,11 @@ const orientation = { previous: null, }; + document.addEventListener("mouseup", () => { mouse.down = false; }); - document.addEventListener("mousedown", (event) => { - // Disables link dragging that occurs when spinning. - event.preventDefault(); - mouse.down = true; - }); - document.addEventListener("mousemove", (event) => { if (!mouse.down) return; @@ -127,4 +122,50 @@ const orientation = { orientation.set(Quat.mul(rotation, orientation.get())); }); + + let angularMomentum = new Vec3(0, 0, 0); + + document.addEventListener("mousedown", (event) => { + // Disables link dragging that occurs when spinning. + event.preventDefault(); + + mouse.down = true; + + angularMomentum = new Vec3(0, 0, 0); + }); + + let lastUpdate = 0; + + const updateFrame = (timestamp) => { + if (lastUpdate == 0) lastUpdate = timestamp; + + const delta = (timestamp - lastUpdate) / 1000; + lastUpdate = timestamp; + + const axis = angularMomentum; + const omega = angularMomentum.length(); + const decay = Math.exp(-delta * friction); + + const effectiveDelta = friction > 0 ? (1 - decay) / friction : delta; + + let theta = effectiveDelta * omega; + + angularMomentum.x *= decay; + angularMomentum.y *= decay; + angularMomentum.z *= decay; + + if (friction > 0 && angularMomentum.length() < 0.00001) { + theta += angularMomentum.length() / friction; + + angularMomentum.x = 0; + angularMomentum.y = 0; + angularMomentum.z = 0; + } + + if (!mouse.down) orientation.set(Quat.mul(Quat.fromAngleAxis(theta, axis), orientation.get())); + + requestAnimationFrame(updateFrame); + }; + + updateFrame(0); })();