@@ -277,6 +277,61 @@ suite('p5.Camera', function() {
277277 assert . strictEqual ( myCam . upY , orig . uy , 'up Y pos changed' ) ;
278278 assert . strictEqual ( myCam . upZ , orig . uz , 'up Z pos changed' ) ;
279279 } ) ;
280+
281+ suite ( 'Camera Tilt and Up Vector' , function ( ) {
282+ test ( 'Tilt() correctly updates the up vector' , function ( ) {
283+ var orig = getVals ( myCam ) ; // Store original camera values
284+
285+ // Apply tilt to the camera
286+ myCam . tilt ( 30 ) ; // Tilt by 30 degrees
287+
288+ // Compute expected up vector (normalized)
289+ let forward = myp5 . createVector (
290+ myCam . centerX - myCam . eyeX ,
291+ myCam . centerY - myCam . eyeY ,
292+ myCam . centerZ - myCam . eyeZ
293+ ) ;
294+ let up = myp5 . createVector ( orig . ux , orig . uy , orig . uz ) ;
295+ let right = p5 . Vector . cross ( forward , up ) ;
296+ let expectedUp = p5 . Vector . cross ( right , forward ) . normalize ( ) ;
297+
298+ // Verify that the up vector has changed
299+ assert . notStrictEqual ( myCam . upX , orig . ux , 'upX should be updated' ) ;
300+ assert . notStrictEqual ( myCam . upY , orig . uy , 'upY should be updated' ) ;
301+ assert . notStrictEqual ( myCam . upZ , orig . uz , 'upZ should be updated' ) ;
302+
303+ // Verify up vector matches expected values within a small margin of error
304+ assert . closeTo ( myCam . upX , expectedUp . x , 0.001 , 'upX mismatch' ) ;
305+ assert . closeTo ( myCam . upY , expectedUp . y , 0.001 , 'upY mismatch' ) ;
306+ assert . closeTo ( myCam . upZ , expectedUp . z , 0.001 , 'upZ mismatch' ) ;
307+ } ) ;
308+
309+ test ( 'Tilt() with negative angle correctly updates the up vector' , function ( ) {
310+ var orig = getVals ( myCam ) ; // Store original camera values
311+
312+ myCam . tilt ( - 30 ) ; // Tilt by -30 degrees
313+
314+ // Compute expected up vector (normalized)
315+ let forward = myp5 . createVector (
316+ myCam . centerX - myCam . eyeX ,
317+ myCam . centerY - myCam . eyeY ,
318+ myCam . centerZ - myCam . eyeZ
319+ ) ;
320+ let up = myp5 . createVector ( orig . ux , orig . uy , orig . uz ) ;
321+ let right = p5 . Vector . cross ( forward , up ) ;
322+ let expectedUp = p5 . Vector . cross ( right , forward ) . normalize ( ) ;
323+
324+ // Verify that the up vector has changed
325+ assert . notStrictEqual ( myCam . upX , orig . ux , 'upX should be updated' ) ;
326+ assert . notStrictEqual ( myCam . upY , orig . uy , 'upY should be updated' ) ;
327+ assert . notStrictEqual ( myCam . upZ , orig . uz , 'upZ should be updated' ) ;
328+
329+ // Verify up vector matches expected values within a small margin of error
330+ assert . closeTo ( myCam . upX , expectedUp . x , 0.001 , 'upX mismatch' ) ;
331+ assert . closeTo ( myCam . upY , expectedUp . y , 0.001 , 'upY mismatch' ) ;
332+ assert . closeTo ( myCam . upZ , expectedUp . z , 0.001 , 'upZ mismatch' ) ;
333+ } ) ;
334+ } ) ;
280335 } ) ;
281336
282337 suite ( 'Rotation with angleMode(DEGREES)' , function ( ) {
0 commit comments