Lots of visibility fixes.

This commit is contained in:
Nolan Darilek 2024-09-01 16:21:26 -05:00
parent a75068f6a9
commit 3ccb3a1d80

View File

@ -65,9 +65,7 @@ impl Viewshed {
*start, *start,
0., 0.,
&shape, &shape,
QueryFilter::new() QueryFilter::new().predicate(&|e| visible_query.contains(e)),
.exclude_collider(*viewer_entity)
.predicate(&|e| visible_query.contains(e)),
|entity| { |entity| {
if let Ok((_, collider, transform)) = visible_query.get(entity) { if let Ok((_, collider, transform)) = visible_query.get(entity) {
let position = Isometry2::new( let position = Isometry2::new(
@ -86,15 +84,16 @@ impl Viewshed {
); );
let mut context: Context<u8> = Context::default(); let mut context: Context<u8> = Context::default();
let vision_distance = vision_distance::Circle::new(self.range); let vision_distance = vision_distance::Circle::new(self.range);
let shape = Collider::cuboid(0.49, 0.49); let shape = Collider::cuboid(0.5, 0.5);
let mut new_visible_entities = HashSet::new(); let mut new_visible_entities = HashSet::new();
let size = ( let size = (
(start.x.abs() + self.range as f32) as u32, (start.x.abs() + self.range as f32 * 2.) as u32,
(start.y.abs() + self.range as f32) as u32, (start.y.abs() + self.range as f32 * 2.) as u32,
); );
let visibility_grid = VisibilityGrid( let visibility_grid = VisibilityGrid(
size, size,
RefCell::new(Box::new(|coord: Coord| { RefCell::new(Box::new(|coord: Coord| {
// println!("Checking {coord:?}");
let shape_pos = Vec2::new(coord.x as f32 + 0.5, coord.y as f32 + 0.5); let shape_pos = Vec2::new(coord.x as f32 + 0.5, coord.y as f32 + 0.5);
// println!("Checking {:?}", shape_pos); // println!("Checking {:?}", shape_pos);
if start.distance(&shape_pos) > self.range as f32 { if start.distance(&shape_pos) > self.range as f32 {
@ -106,7 +105,7 @@ impl Viewshed {
Some((*opacity, coord_entities.clone())) Some((*opacity, coord_entities.clone()))
} else { } else {
let position = Isometry2::new(Vector2::new(shape_pos.x, shape_pos.y), 0.); let position = Isometry2::new(Vector2::new(shape_pos.x, shape_pos.y), 0.);
let aabb = shape.raw.compute_aabb(&position); let aabb = shape.raw.compute_aabb(&position).tightened(0.01);
let mut hit = false; let mut hit = false;
for b in &boxes { for b in &boxes {
if b.intersects(&aabb) { if b.intersects(&aabb) {
@ -123,13 +122,11 @@ impl Viewshed {
shape_pos, shape_pos,
0., 0.,
&shape, &shape,
QueryFilter::new() QueryFilter::new().predicate(&|v| visible_query.contains(v)),
.exclude_collider(*viewer_entity)
.predicate(&|v| visible_query.contains(v)),
|entity| { |entity| {
// println!("{:?}", entity); // println!("{:?}", entity);
coord_entities.insert(entity);
if let Ok((visible, _, _)) = visible_query.get(entity) { if let Ok((visible, _, _)) = visible_query.get(entity) {
coord_entities.insert(entity);
opacity = opacity.max(**visible); opacity = opacity.max(**visible);
} }
true true
@ -144,12 +141,18 @@ impl Viewshed {
Some((0, coord_entities)) Some((0, coord_entities))
} }
}; };
if let Some((opacity, coord_entities)) = result { if let Some((mut opacity, coord_entities)) = result {
// println!("Opacity: {opacity}, coord_entities: {coord_entities:?}");
for e in &coord_entities { for e in &coord_entities {
let mut should_insert = true; let mut should_insert = true;
if coord_entities.len() > 1 { if e == viewer_entity {
opacity = 0;
} else {
let dest = Vec2::new(coord.x as f32, coord.y as f32); let dest = Vec2::new(coord.x as f32, coord.y as f32);
let dir = dest - *start; let dir = dest - *start;
// println!(
// "Checking visibility of {e} by casting from {start} to {dest}, {dir}"
// );
rapier_context.intersections_with_ray( rapier_context.intersections_with_ray(
*start, *start,
dir, dir,
@ -157,12 +160,14 @@ impl Viewshed {
true, true,
QueryFilter::new() QueryFilter::new()
.exclude_sensors() .exclude_sensors()
.exclude_collider(*viewer_entity)
.exclude_collider(*e) .exclude_collider(*e)
.predicate(&|e| visible_query.contains(e)), .predicate(&|e| visible_query.contains(e)),
|_, _| { |entity, hit| {
should_insert = false; if entity != *viewer_entity {
false // println!("Hit {entity} at {hit:?}");
should_insert = false;
}
true
}, },
); );
} }
@ -170,6 +175,7 @@ impl Viewshed {
new_visible_entities.insert(*e); new_visible_entities.insert(*e);
} }
} }
// println!("New opacity: {opacity}");
opacity opacity
} else { } else {
0 0
@ -177,6 +183,7 @@ impl Viewshed {
})), })),
); );
let mut new_visible = HashSet::new(); let mut new_visible = HashSet::new();
// println!("Start: {viewer_entity}");
context.for_each_visible( context.for_each_visible(
Coord::new(start.x_i32(), start.y_i32()), Coord::new(start.x_i32(), start.y_i32()),
&visibility_grid, &visibility_grid,