Skip to content

Fix camera jitter when looking straight up or down#179

Merged
slimbuck merged 1 commit intomainfrom
cam-dev
Feb 27, 2026
Merged

Fix camera jitter when looking straight up or down#179
slimbuck merged 1 commit intomainfrom
cam-dev

Conversation

@slimbuck
Copy link
Member

Summary

  • Fix degenerate orbit animation in createRotateTrack when the camera-to-target offset is aligned with the Y axis (rotating a Y-aligned vector around Y is a no-op, producing identical keyframes that cause spline ringing)
  • Fix numerical instability in vecToAngles near the poles where near-zero horizontal components cause atan2 to produce wildly oscillating yaw values

Details

createRotateTrack: Decompose the camera offset into vertical and horizontal components instead of rotating the full 3D vector with a matrix. When the horizontal radius is near zero (looking straight up/down), a minimum orbit radius of 30% of the total distance is used so the camera actually traces a visible circle. For non-degenerate cases the result is mathematically identical.

vecToAngles: When the squared horizontal length is below 1e-8, yaw defaults to 0 instead of relying on atan2 with near-zero arguments. Also clamps the asin input to [-1, 1] to guard against floating-point edge cases.

Test plan

  • Load a scene with the camera looking straight down the Y axis (position and target aligned in X/Z) and verify the rotation animation orbits smoothly
  • Verify normal (non-vertical) camera setups still produce the same orbit behavior
  • Test camera transitions and orbit interactions near vertical orientations for stable yaw

@slimbuck slimbuck requested review from a team and Copilot February 27, 2026 14:15
@slimbuck slimbuck self-assigned this Feb 27, 2026
@slimbuck slimbuck added the bug Something isn't working label Feb 27, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses camera jitter/artifacts near vertical orientations by stabilizing angle extraction at the poles and by generating a non-degenerate orbit path when the camera offset is (nearly) Y-aligned.

Changes:

  • Update vecToAngles to clamp asin input and avoid unstable yaw near the poles by falling back to 0 when horizontal magnitude is tiny.
  • Rework createRotateTrack to build an orbit from horizontal/vertical decomposition (instead of rotating the full offset vector), with a minimum orbit radius for near-vertical setups.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/core/math.ts Adds clamping and a pole-case fallback in vecToAngles to prevent yaw instability.
src/animation/create-rotate-track.ts Replaces matrix-rotation orbit generation with an angle/radius approach and introduces a minimum orbit radius for near-vertical offsets.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@slimbuck slimbuck merged commit fea6093 into main Feb 27, 2026
6 checks passed
@slimbuck slimbuck deleted the cam-dev branch February 27, 2026 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants