Skip to content

Commit fcae974

Browse files
authored
Fix camera tilt function to prevent orientation flipping
1 parent 389b77d commit fcae974

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/webgl/p5.Camera.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,17 +2448,34 @@ p5.Camera = class Camera {
24482448
rotatedCenter[1] += this.eyeY;
24492449
rotatedCenter[2] += this.eyeZ;
24502450

2451+
// Compute new up vector to prevent flipping
2452+
let forward = createVector(
2453+
rotatedCenter[0] - this.eyeX,
2454+
rotatedCenter[1] - this.eyeY,
2455+
rotatedCenter[2] - this.eyeZ
2456+
).normalize();
2457+
2458+
let up = createVector(this.upX, this.upY, this.upZ);
2459+
let right = p5.Vector.cross(forward, up).normalize(); // Right vector
2460+
up = p5.Vector.cross(right, forward).normalize(); // Corrected up vector
2461+
2462+
24512463
this.camera(
24522464
this.eyeX,
24532465
this.eyeY,
24542466
this.eyeZ,
24552467
rotatedCenter[0],
24562468
rotatedCenter[1],
24572469
rotatedCenter[2],
2458-
this.upX,
2459-
this.upY,
2460-
this.upZ
2470+
this.up.x,
2471+
this.up.y,
2472+
this.up.z
24612473
);
2474+
2475+
// Update up vector
2476+
this.upX = up.x;
2477+
this.upY = up.y;
2478+
this.upZ = up.z;
24622479
}
24632480

24642481
/**

0 commit comments

Comments
 (0)