From 9a842d7e54f2addaa8e44bcaf8738126e4ce1dec Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Thu, 18 Jul 2024 14:53:03 +0200 Subject: [PATCH] more pleasing result: display a point if within solid ; code cleaning --- crates/parry2d/examples/raycasts_animated.rs | 115 +++++++++---------- 1 file changed, 52 insertions(+), 63 deletions(-) diff --git a/crates/parry2d/examples/raycasts_animated.rs b/crates/parry2d/examples/raycasts_animated.rs index 091b84e7..a81a63f3 100644 --- a/crates/parry2d/examples/raycasts_animated.rs +++ b/crates/parry2d/examples/raycasts_animated.rs @@ -1,63 +1,67 @@ use macroquad::prelude::*; use nalgebra::{Isometry2, Point2, UnitComplex, Vector2}; use parry2d::query::{Ray, RayCast}; -use parry2d::shape::{Ball, Cuboid, TriMesh}; +use parry2d::shape::{Cuboid, TriMesh}; const RENDER_SCALE: f32 = 30.0; #[macroquad::main("parry2d::utils::polygons_intersection_points")] async fn main() { - let mut animated_spikes = spikes_polygon(); - - let mut animated_star = star_polygon(); - let animation_scale = 2.0; let animation_rotation = UnitComplex::new(0.008); let spikes_render_pos = Point2::new(300.0, 300.0); - for i in 0.. { + for i in 1.. { clear_background(BLACK); /* * - * Compute the rotated/scaled polygons, and compute their intersection with the original - * polygon. + * Compute the rotated/scaled cuboid. * */ - animated_spikes - .iter_mut() - .for_each(|pt| *pt = animation_rotation * *pt); - - animated_star.iter_mut().for_each(|pt| { + let mut animated_cube = Cuboid::new(Vector2::new(2.0, 2.0)).to_polyline(); + animated_cube.iter_mut().for_each(|pt| { *pt = animation_rotation.powf(i as f32) * *pt - * ((i as f32 / 100.0).sin().abs() * animation_scale) + * ((i as f32 / 100.0).sin().abs() * animation_scale); }); - - //let trimesh = TriMesh::from_polygon(animated_spikes.clone()).unwrap(); - let trimesh = Cuboid::new(Vector2::new(2.0, 2.0)); + let trimesh = TriMesh::from_polygon(animated_cube).unwrap(); + /* + * + * Prepare a Raycast and compute its result against the shape. + * + */ let ray = Ray::new( - (Vector2::new(12.0, 0.0)).into(), + (Vector2::new(2.0, 1.0)).into(), animation_rotation.powf(i as f32 * 2f32) * Vector2::y(), ); let toi = trimesh.cast_ray(&Isometry2::identity(), &ray, std::f32::MAX, true); + /* + * + * Render the raycast's result. + * + */ if let Some(toi) = toi { - drawline_from_to( - ray.origin, - ray.origin + ray.dir * toi, - GREEN, - RENDER_SCALE, - spikes_render_pos, - ); + if toi == 0f32 { + draw_point(ray.origin, RENDER_SCALE, spikes_render_pos, YELLOW); + } else { + drawline_from_to( + ray.origin, + ray.origin + ray.dir * toi, + RENDER_SCALE, + spikes_render_pos, + GREEN, + ); + } } else { drawline_from_to( ray.origin, ray.origin + ray.dir * 1000f32, - RED, RENDER_SCALE, spikes_render_pos, + RED, ); } @@ -66,47 +70,12 @@ async fn main() { * Render the polygons and their intersections. * */ - draw_polygon( - &trimesh.to_polyline(), - RENDER_SCALE, - spikes_render_pos, - GREEN, - ); - - draw_polygon(&animated_star, RENDER_SCALE, spikes_render_pos, GREEN); + draw_polygon(&trimesh.vertices(), RENDER_SCALE, spikes_render_pos, GREEN); next_frame().await } } -fn star_polygon() -> Vec> { - let mut star = Ball::new(1.5).to_polyline(10); - star.iter_mut().step_by(2).for_each(|pt| *pt = *pt * 0.6); - star -} - -fn spikes_polygon() -> Vec> { - let teeths = 5; - let width = 10.0; - let height = 5.0; - let tooth_width = width / (teeths as f32); - let center = Vector2::new(width / 2.0, height / 2.0); - - let mut polygon = vec![ - Point2::new(width, 0.0) - center, - Point2::new(width, height) - center, - Point2::new(0.0, height) - center, - ]; - - for i in 0..teeths { - let x = i as f32 * tooth_width; - polygon.push(Point2::new(x, 0.0) - center); - polygon.push(Point2::new(x + tooth_width / 2.0, height * 0.8) - center); - } - - polygon -} - fn draw_polygon(polygon: &[Point2], scale: f32, shift: Point2, color: Color) { for i in 0..polygon.len() { let a = polygon[i]; @@ -122,12 +91,32 @@ fn draw_polygon(polygon: &[Point2], scale: f32, shift: Point2, color: } } +fn draw_point(point: Point2, scale: f32, shift: Point2, color: Color) { + let edge_len = 0.15; + draw_line( + (point.x - edge_len) * scale + shift.x, + point.y * scale + shift.y, + (point.x + edge_len) * scale + shift.x, + point.y * scale + shift.y, + 2.0, + color, + ); + draw_line( + point.x * scale + shift.x, + (point.y - edge_len) * scale + shift.y, + point.x * scale + shift.x, + (point.y + edge_len) * scale + shift.y, + 2.0, + color, + ); +} + fn drawline_from_to( from: Point2, to: Point2, - color: Color, scale: f32, shift: Point2, + color: Color, ) { let from = from * scale + shift.coords; let to = to * scale + shift.coords;