|
| 1 | +//! Builds on top of `point_in_poly2d` example but uses `QueryPoint`` methods and different shapes. |
| 2 | +
|
| 3 | +mod common_macroquad2d; |
| 4 | + |
| 5 | +use common_macroquad2d::{draw_point, draw_polygon}; |
| 6 | +use macroquad::prelude::*; |
| 7 | +use nalgebra::{Isometry, Point2, Rotation, Translation, UnitComplex, Vector2}; |
| 8 | +use parry2d::query::PointQuery; |
| 9 | +use parry2d::shape::{ConvexPolygon, Shape, SharedShape}; |
| 10 | +use parry2d::{shape::Compound, utils::point_in_poly2d}; |
| 11 | + |
| 12 | +const RENDER_SCALE: f32 = 30.0; |
| 13 | + |
| 14 | +#[macroquad::main("points_in_poly2d")] |
| 15 | +async fn main() { |
| 16 | + env_logger::init(); |
| 17 | + let mut compound_simple = simple_compound(); |
| 18 | + let mut compound_simple2 = simple_compound(); |
| 19 | + let test_points = grid_points(); |
| 20 | + |
| 21 | + let animation_rotation = UnitComplex::new(0.02); |
| 22 | + let polygon_render_pos = Point2::new(screen_width() / 2.0, screen_height() / 2.0); |
| 23 | + |
| 24 | + for i in 0.. { |
| 25 | + let shape_to_query = if (i / 350) % 2 == 0 { |
| 26 | + &mut compound_simple |
| 27 | + } else { |
| 28 | + &mut compound_simple2 |
| 29 | + }; |
| 30 | + |
| 31 | + clear_background(BLACK); |
| 32 | + |
| 33 | + // TODO: Display the shape |
| 34 | + /* polygon |
| 35 | + .iter_mut() |
| 36 | + .for_each(|pt| *pt = animation_rotation * *pt); |
| 37 | +
|
| 38 | + draw_polygon(&polygon, RENDER_SCALE, polygon_render_pos, BLUE); |
| 39 | + */ |
| 40 | + /* |
| 41 | + * Compute polygon intersections. |
| 42 | + */ |
| 43 | + for point in &test_points { |
| 44 | + let pos12 = Isometry::default().inv_mul(&Isometry::from_parts( |
| 45 | + Translation::from(point.coords), |
| 46 | + Rotation::identity(), |
| 47 | + )); |
| 48 | + if shape_to_query.contains_local_point( |
| 49 | + &(pos12 * point), |
| 50 | + // FIXME: Thierry (pr 298): options should contain everything necessary to be dispatched. |
| 51 | + &(), |
| 52 | + ) { |
| 53 | + draw_point(*point, RENDER_SCALE, polygon_render_pos, RED); |
| 54 | + } else { |
| 55 | + draw_point(*point, RENDER_SCALE, polygon_render_pos, GREEN); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + next_frame().await |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +fn simple_compound() -> Box<dyn Shape> { |
| 64 | + let to_cast_against = ConvexPolygon::from_convex_polyline( |
| 65 | + [ |
| 66 | + [-24.0, 0.0].into(), |
| 67 | + [0.0, -24.0].into(), |
| 68 | + [24.0, 0.0].into(), |
| 69 | + [0.0, 24.0].into(), |
| 70 | + ] |
| 71 | + .into(), |
| 72 | + ) |
| 73 | + .unwrap(); |
| 74 | + Box::new(Compound::new(vec![( |
| 75 | + Isometry::default(), |
| 76 | + SharedShape::new(to_cast_against), |
| 77 | + )])) |
| 78 | +} |
| 79 | + |
| 80 | +fn grid_points() -> Vec<Point2<f32>> { |
| 81 | + let count = 80 * 5; |
| 82 | + let spacing = 0.6 / 5.0; |
| 83 | + let mut pts = vec![]; |
| 84 | + for i in 0..count { |
| 85 | + for j in 0..count { |
| 86 | + pts.push(Point2::new( |
| 87 | + (i as f32 - count as f32 / 2.0) * spacing, |
| 88 | + (j as f32 - count as f32 / 2.0) * spacing, |
| 89 | + )); |
| 90 | + } |
| 91 | + } |
| 92 | + pts |
| 93 | +} |
0 commit comments