Skip to content

Commit

Permalink
Intersection testing works \o/
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatekii committed Aug 18, 2019
1 parent af5e66a commit 5817156
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/drawing/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ impl Painter {
}
}
hud.paint(
app_state,
&self.window,
app_state.screen.width as f64,
app_state.screen.height as f64,
Expand Down
6 changes: 4 additions & 2 deletions src/drawing/ui.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::app_state::AppState;
use imgui::*;

pub struct HUD {
Expand Down Expand Up @@ -30,7 +31,7 @@ impl HUD {
})
}
]);

let renderer = imgui_wgpu::Renderer::new(
&mut imgui,
device,
Expand All @@ -48,6 +49,7 @@ impl HUD {

pub fn paint(
&mut self,
app_state: &mut AppState,
window: &wgpu::winit::Window,
width: f64,
height: f64,
Expand All @@ -65,7 +67,7 @@ impl HUD {
window
.size([300.0, 100.0], imgui::Condition::FirstUseEver)
.build(&ui, || {
ui.text(im_str!("Hello world!"));
ui.text(im_str!("{:#?}", app_state.hovered_objects.iter().map(|o| o.tags.clone()).collect::<Vec<_>>()));
ui.text(im_str!("This...is...imgui-rs on WGPU!"));
ui.separator();
let mouse_pos = ui.io().mouse_pos;
Expand Down
6 changes: 2 additions & 4 deletions src/interaction/collider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ impl Collider {

let tile_cache = &app_state.tile_cache;

println!("START =================");
for tile_id in tile_field.iter() {
if let Some(tile) = tile_cache.try_get_tile(&tile_id) {
let matrix = app_state.screen.tile_to_global_space(
Expand All @@ -28,8 +27,8 @@ impl Collider {
);
let matrix = nalgebra_glm::inverse(&matrix);
let screen_point = Point::new(
point.0 / (app_state.screen.width / 2) as f32,
point.1 / (app_state.screen.height / 2) as f32
point.0 / (app_state.screen.width / 2) as f32 - 1.0,
point.1 / (app_state.screen.height / 2) as f32 - 1.0
);
let global_point = matrix * Vector4::new(screen_point.x, screen_point.y, 0.0, 1.0);
let tile_point = Point::new(global_point.x, global_point.y) * tile.extent as f32;
Expand All @@ -40,7 +39,6 @@ impl Collider {
for object_id in object_ids {
objects.push(&tile.objects[object_id])
}
println!("FOUND");
return objects
}
} else {
Expand Down
40 changes: 33 additions & 7 deletions src/interaction/tile_collider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ use ncollide2d::{
Isometry,
Point,
Vector,
Rotation,
Translation
},
query::{
Ray,
},
shape::{
ShapeHandle,
Polyline,
Segment,
},
};
use crate::vector_tile::object::Object;
Expand Down Expand Up @@ -54,12 +56,36 @@ impl TileCollider {
}

pub fn get_hovered_objects(&self, point: &Point<f32>) -> Vec<usize> {
self.world
let mut object_ids = vec![];
let mut interferences = vec![];
self.world.broad_phase
.interferences_with_point(
point,
&CollisionGroups::new()
)
.map(|co| *co.1.data())
.collect()
&mut interferences
);

let ray = Ray::new(point.clone(), Vector::x());
for handle in interferences {
if let Some(co) = self.world.collision_object(*handle) {
if let Some(polyline) = co.shape().downcast_ref::<Polyline<f32>>() {
let mut winding_number = 0;
let points = polyline.points();
for edge in polyline.edges() {
let segment = Segment::new(points[edge.indices.x], points[edge.indices.y]);
use ncollide2d::query::RayCast;
if segment.intersects_ray(&Isometry::identity(), &ray) {
winding_number += 1;
}
}

if winding_number % 2 == 1 {
// We found a general polygon that contains our mouse pointer.
object_ids.push(*co.data());
}
}
}
}

object_ids
}
}
2 changes: 1 addition & 1 deletion src/vector_tile/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ impl Tile {
let mut collider = crate::interaction::tile_collider::TileCollider::new();

for object_id in 0..objects.len() {
println!("{}/{}", object_id, objects.len());
if objects[object_id].points.len() >= 2 {
collider.add_object(object_id, &objects[object_id]);
}
}
collider.update();

Self {
tile_id: tile_id.clone(),
Expand Down

0 comments on commit 5817156

Please sign in to comment.