From 2da160bc6a5f74c454756be47b959c63ecb47335 Mon Sep 17 00:00:00 2001 From: yzrmn Date: Wed, 17 Apr 2024 11:23:42 +0200 Subject: [PATCH] Fix accumulating camera rotation drift --- packages/redgeometry-app/src/parts/gpu-cube.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/redgeometry-app/src/parts/gpu-cube.ts b/packages/redgeometry-app/src/parts/gpu-cube.ts index ba55477..082a427 100644 --- a/packages/redgeometry-app/src/parts/gpu-cube.ts +++ b/packages/redgeometry-app/src/parts/gpu-cube.ts @@ -300,8 +300,8 @@ function cameraMoveSystem(world: World): void { return; } - const camRot = transform.rotation; - const camPos = transform.translation; + let camRot = transform.rotation; + let camPos = transform.translation; if (mouse.isPressing(MouseButtons.Mouse3)) { let dx = 0; @@ -316,6 +316,8 @@ function cameraMoveSystem(world: World): void { camRot.rotateXPre(sens * dy); camRot.rotateY(sens * dx); + + camRot = camRot.unit(); } let vel = 0.005; @@ -337,9 +339,12 @@ function cameraMoveSystem(world: World): void { v = v.unitOrZero().mul(delta * vel); v = camRot.mulVec(v); - transform.translation = camPos.add(v); + camPos = camPos.add(v); } + transform.rotation = camRot; + transform.translation = camPos; + world.updateComponent(mainCamera, "transform"); const fovRad = fov * (Math.PI / 180);