Browse Source

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.
main
cinnaboot 6 months ago
parent
commit
a2f199c9ce
  1. 15
      src/renderer.cpp

15
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(

Loading…
Cancel
Save