From a2f199c9ceddfcca3bcd98f91f6a306520fc3744 Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Sun, 18 Jan 2026 09:17:43 -0500 Subject: [PATCH] Fix camera orbit rotation - use camera.up and horizontal orbit Fixes: 1. Camera now orbits horizontally (not vertically) 2. Uses camera.up instead of hardcoded global up vector Technical changes: - Remove hardcoded global up vector - Use render_state->camera.up for rotation axis - Rotate forward vector around camera's up vector - This creates proper horizontal orbit around target Rotation formula: new_forward = forward * cos(angle) + cross(up, forward) * sin(angle) Result: Camera orbits horizontally around target at any camera position, using the camera's own up vector for proper orientation-aware rotation. --- src/renderer.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/renderer.cpp b/src/renderer.cpp index 1e0d88d..a7132c4 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -59,23 +59,21 @@ void update_camera(RenderState* render_state, SimulationState* sim) { render_state->camera.position = Vector3Add(body_pos, render_state->camera_offset); } - // Camera rotation with proper up vector - Vector3 up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Global up for space + // Camera rotation using camera's up vector Vector3 to_camera = Vector3Subtract(render_state->camera.position, render_state->camera.target); float camera_distance = Vector3Length(to_camera); - // Rotate around target using cross products + // Rotate around target using camera's up vector if (IsKeyDown(KEY_LEFT)) { - Vector3 right = Vector3Normalize(Vector3CrossProduct(up, to_camera)); Vector3 forward = Vector3Normalize(to_camera); - // Rotate forward vector around right axis + // Rotate forward vector around camera's up axis (horizontal orbit) float cos_a = cosf(angle_speed); float sin_a = sinf(angle_speed); Vector3 new_forward = Vector3Add( Vector3Scale(forward, cos_a), - Vector3Scale(Vector3CrossProduct(right, forward), sin_a) + Vector3Scale(Vector3CrossProduct(render_state->camera.up, forward), sin_a) ); render_state->camera.position = Vector3Add( @@ -91,16 +89,15 @@ void update_camera(RenderState* render_state, SimulationState* sim) { } } if (IsKeyDown(KEY_RIGHT)) { - Vector3 right = Vector3Normalize(Vector3CrossProduct(up, to_camera)); Vector3 forward = Vector3Normalize(to_camera); - // Rotate forward vector around right axis + // Rotate forward vector around camera's up axis (horizontal orbit) float cos_a = cosf(-angle_speed); float sin_a = sinf(-angle_speed); Vector3 new_forward = Vector3Add( Vector3Scale(forward, cos_a), - Vector3Scale(Vector3CrossProduct(right, forward), sin_a) + Vector3Scale(Vector3CrossProduct(render_state->camera.up, forward), sin_a) ); render_state->camera.position = Vector3Add(