Move your pointer to carry the light.

0:01 / 0:30
Playback speed

Shadow casting

Place the light beside a block. Trace a dark edge back toward the light and see which corner made it.

Interactive demo#Shadows#Shadow

Walls really block a light, leaving dark wedges behind them.

From the light, the game stretches each wall edge outward and fills the area behind it with darkness. Unlike the shortcuts above, this is worked out from the scene, so it’s used for torches in dungeons and for line of sight in stealth games.

Code

src/demos/shadow_casting.goose
namespace shadow_casting_lesson;
import stage;
import std;
import math;

export "shadow-casting" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
    let blocks = [[38, 22, 18, 18], [100, 16, 14, 28], [66, 62, 30, 12], [24, 64, 12, 12], [126, 62, 14, 14]];
    lit_floor(px, py);
    for b in blocks {
        cast_shadow(b[0], b[1], b[2], b[3], px, py, "rgba(14,15,38,.82)");
    }
    for b in blocks {
        draw_block(b[0], b[1], b[2], b[3]);
    }
    lamp(px, py);
}

Stage drawing helpers · All demos on GitHub

Comments

No comments yet.