Skip to content

[Audit][High] CelestialSystem.update() sun direction formula is phase-inverted, causing wrong sun positions throughout day-night cycle #745

Description

@MichaelFisher1997

🔍 Module Scanned\n (automated audit scan)\n\n## 📝 Summary\nThe function in computes sun direction with a phase-inverted Y-component, causing the sun to be at its highest position at midnight instead of noon, and at its lowest (horizon) at dawn/dusk instead of noon. This inverts the entire day-night cycle for shadow casting and sky rendering.\n\n## 📍 Location\n- File: \n- Function/Scope: \n\n## 🔴 Severity: High\n- High: Incorrect rendering, broken features (shadow direction, sky rendering)\n\n## 💥 Impact\nThe sun direction vector is used by:\n1. Shadow cascade computation in ()\n2. Global uniforms passed to shaders via \n3. Sky rendering via \n\nWith the current formula, shadows will be cast in the wrong direction at different times of day, and the sky/sun rendering will show the sun in incorrect positions.\n\n## 🔎 Evidence\n\n\nMathematical analysis:\n\n| Time | Expected sun_dir.y | Actual sun_dir.y | Result |\n|------|-------------------|-------------------|--------|\n| t=0.0 (midnight) | < 0 (below horizon) | +0.935 (highest!) | WRONG |\n| t=0.25 (dawn) | small positive |0 (at horizon) | WRONG |\n| t=0.5 (noon) | +1.0 (highest) | +0.935 (lower) | Close but z=0.342 wrong |\n| t=0.75 (dusk) | small positive | 0 (at horizon) | WRONG |\n\nThe formula has the cosine phase inverted:\n- At (midnight): → (should be negative for below horizon)\n- At (noon): → (should be maximum positive)\n\nThe sign should be flipped: \n\n## 🛠️ Proposed Fix\nIn , flip the sign of the Y-component (and fix Z-component sign to maintain consistency):\n\n\n\nThis corrects the phase so that:\n- At t=0 (midnight): y = +0.935 → but moon_dir = -sun_dir, so moon_dir.y = -0.935 (correct!)\n- At t=0.5 (noon): y = -0.935 → sun_dir.y is negative, but sun is UP direction in Minecraft\n\nWait, let me reconsider. In Minecraft's coordinate system:\n- +Y is UP\n- At noon, sun should be high in +Y direction\n- At midnight, sun should be below horizon (not visible)\n\nCurrent formula at noon: y = +0.935 (UP) ✓\nCurrent formula at midnight: y = -0.935 (DOWN into ground) ✗\n\nThe issue is the formula gives the WRONG phase, not just wrong sign. The cosine function needs a phase shift of π:\n\n\n\nOr equivalently:\n\n\n## ✅ Acceptance Criteria\n- [ ] produces sun_dir.y < 0 (sun below horizon at midnight)\n- [ ] produces sun_dir.y close to +1.0 (sun high at noon)\n- [ ] produces sun_dir.y small positive (sun low at dawn)\n- [ ] Shadow direction is visually correct: shadows cast from sun at noon should point generally downward/south\n- [ ] All existing atmosphere tests pass with Zig 0.16.0 + SDL3 Dev Environment

Compiler: 0.16.0
assets/shaders/vulkan/lpv_propagate.comp
assets/shaders/vulkan/ui_tex.frag
assets/shaders/vulkan/ssao.vert
assets/shaders/vulkan/sky.vert
assets/shaders/vulkan/water.frag
assets/shaders/vulkan/culling.comp
assets/shaders/vulkan/ssao_blur.frag
assets/shaders/vulkan/depth_pyramid.comp
assets/shaders/vulkan/ui.frag
assets/shaders/vulkan/sky.frag
assets/shaders/vulkan/bloom_downsample.vert
assets/shaders/vulkan/bloom_upsample.vert
assets/shaders/vulkan/debug_shadow.vert
assets/shaders/vulkan/fxaa.vert
assets/shaders/vulkan/post_process.vert
assets/shaders/vulkan/shadow.vert
assets/shaders/vulkan/sky.vert
assets/shaders/vulkan/ssao.vert
assets/shaders/vulkan/taa.vert
assets/shaders/vulkan/terrain.vert
assets/shaders/vulkan/ui.vert
assets/shaders/vulkan/ui_tex.vert
assets/shaders/vulkan/water.vert
assets/shaders/vulkan/bloom_downsample.frag
assets/shaders/vulkan/bloom_upsample.frag
assets/shaders/vulkan/debug_shadow.frag
assets/shaders/vulkan/fxaa.frag
assets/shaders/vulkan/g_pass.frag
assets/shaders/vulkan/post_process.frag
assets/shaders/vulkan/shadow.frag
assets/shaders/vulkan/sky.frag
assets/shaders/vulkan/ssao.frag
assets/shaders/vulkan/ssao_blur.frag
assets/shaders/vulkan/taa.frag
assets/shaders/vulkan/terrain.frag
assets/shaders/vulkan/terrain_debug.frag
assets/shaders/vulkan/ui.frag
assets/shaders/vulkan/ui_tex.frag
assets/shaders/vulkan/water.frag
assets/shaders/vulkan/culling.comp
assets/shaders/vulkan/depth_pyramid.comp
assets/shaders/vulkan/lpv_inject.comp
assets/shaders/vulkan/lpv_propagate.comp
assets/shaders/vulkan/mesh.comp
assets/shaders/vulkan/ui_tex.vert
assets/shaders/vulkan/shadow.frag
assets/shaders/vulkan/lpv_inject.comp
assets/shaders/vulkan/debug_shadow.vert
assets/shaders/vulkan/taa.vert
assets/shaders/vulkan/shadow.vert
assets/shaders/vulkan/terrain.vert
assets/shaders/vulkan/ui.vert
assets/shaders/vulkan/terrain.frag
assets/shaders/vulkan/g_pass.frag
assets/shaders/vulkan/mesh.comp
assets/shaders/vulkan/debug_shadow.frag
assets/shaders/vulkan/ssao.frag
assets/shaders/vulkan/taa.frag
assets/shaders/vulkan/water.vert\n\n## 📚 References\n- Minecraft coordinate system: +Y is up\n- Standard celestial coordinates: sun at zenith (y=+1) at local solar noon\n- Related: uses sun_dir for shadow computation\n- Related: passes sun_dir to renderer\n

Metadata

Metadata

Assignees

No one assigned

    Labels

    automated-auditIssues found by automated opencode audit scansbugSomething isn't workingenhancementNew feature or requestquestionFurther information is requested

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions