Add a simple filter to hide out-of-range walls

This commit is contained in:
Emi Simpson 2021-11-18 20:44:39 -05:00
parent 66093071ac
commit 07ddec501a
Signed by: Emi
GPG Key ID: A12F2C2FFDC3D847
2 changed files with 22 additions and 7 deletions

View File

@ -87,12 +87,11 @@ async fn main() {
);
for (w1, w2) in dd.get_walls() {
shadows::draw_wall(
w1.as_f32(),
w2.as_f32(),
mouse_position,
LIGHT_RADIUS,
);
let w1 = w1.as_f32();
let w2 = w2.as_f32();
if shadows::wall_needs_to_be_drawn(w1, w2, mouse_position, LIGHT_RADIUS) {
shadows::draw_wall(w1, w2, mouse_position, LIGHT_RADIUS);
}
}
dd.update();

View File

@ -1,3 +1,4 @@
use macroquad::prelude::Mat2;
use macroquad::prelude::draw_triangle;
use macroquad::prelude::DVec2;
use macroquad::prelude::draw_mesh;
@ -52,7 +53,22 @@ pub fn draw_wall(a: Vec2, b: Vec2, light_source: Vec2, radius: f32) {
draw_triangle(b_prime, a_prime, opposite_src, BLACK);
}
pub fn draw_polygon(points: &[Vec2], color: Color) {
pub fn wall_needs_to_be_drawn(a: Vec2, b: Vec2, light_source: Vec2, radius: f32) -> bool {
let a = a - light_source;
let b = b - light_source;
let delta = a - b;
let unit = delta / delta.length();
let a_dot = unit.dot(a);
let b_dot = unit.dot(b);
let d = Mat2::from_cols(unit, a).determinant();
(d < radius && d > -radius) && (
a.length() < radius ||
b.length() < radius ||
(a_dot < 0. && b_dot > 0.) || (a_dot > 0. && b_dot < 0.)
)
}
/*/// Code adapted from https://github.com/not-fl3/macroquad/issues/174