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