From 44c81cc34f6fe8b09cb67b0ec2e8e661d6cd3c89 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 31 Jul 2024 12:14:04 +0200 Subject: [PATCH 01/16] example for projection (+ bug detected) --- crates/parry3d/Cargo.toml | 17 +- crates/parry3d/examples/project3d_animated.rs | 157 ++++++++++++++++++ 2 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 crates/parry3d/examples/project3d_animated.rs diff --git a/crates/parry3d/Cargo.toml b/crates/parry3d/Cargo.toml index 62545af3..99c037f1 100644 --- a/crates/parry3d/Cargo.toml +++ b/crates/parry3d/Cargo.toml @@ -22,11 +22,23 @@ workspace = true [features] default = ["required-features", "std"] required-features = ["dim3", "f32"] -std = ["nalgebra/std", "slab", "rustc-hash", "simba/std", "arrayvec/std", "spade", "thiserror"] +std = [ + "nalgebra/std", + "slab", + "rustc-hash", + "simba/std", + "arrayvec/std", + "spade", + "thiserror", +] dim3 = [] f32 = [] serde-serialize = ["serde", "nalgebra/serde-serialize", "bitflags/serde"] -rkyv-serialize = ["rkyv/validation", "nalgebra/rkyv-serialize", "simba/rkyv-serialize"] +rkyv-serialize = [ + "rkyv/validation", + "nalgebra/rkyv-serialize", + "simba/rkyv-serialize", +] bytemuck-serialize = ["bytemuck", "nalgebra/convert-bytemuck"] simd-stable = ["simba/wide", "simd-is-enabled"] @@ -76,3 +88,4 @@ obj = { version = "0.10.2", optional = true } oorandom = "11" ptree = "0.4.0" rand = { version = "0.8" } +macroquad = "*" diff --git a/crates/parry3d/examples/project3d_animated.rs b/crates/parry3d/examples/project3d_animated.rs new file mode 100644 index 00000000..6c64486c --- /dev/null +++ b/crates/parry3d/examples/project3d_animated.rs @@ -0,0 +1,157 @@ +use macroquad::models::Vertex; +use macroquad::prelude::*; +use nalgebra::{Point3, UnitVector3, Vector3}; +use parry3d::math::{Isometry, Real}; +use parry3d::query::{IntersectResult, PointQuery}; +use parry3d::shape::TriMesh; + +fn build_diamond(position: &Isometry) -> (Vec>, Vec<[u32; 3]>) { + // Two tetrahedrons sharing a face + let points = vec![ + position * Point3::new(0.0, 2.0, 0.0), + position * Point3::new(-2.0, -1.0, 0.0), + position * Point3::new(0.0, 0.0, 2.0), + position * Point3::new(2.0, -1.0, 0.0), + position * Point3::new(0.0, 0.0, -2.0), + ]; + + let indices = vec![ + [0u32, 1, 2], + [0, 2, 3], + [1, 2, 3], + [0, 1, 4], + [0, 4, 3], + [1, 4, 3], + ]; + + (points, indices) +} + +#[macroquad::main("parry3d::query::PlaneIntersection")] +async fn main() { + // + // This is useful to test for https://github.com/dimforge/parry/pull/248 + let _points = vec![ + Point3::from([0.0, 0.0, 0.0]), + Point3::from([0.0, 0.0, 1.0]), + Point3::from([1.0, 0.0, 0.0]), + Point3::from([1.0, 0.0, 1.0]), + ]; + let _indices: Vec<[u32; 3]> = vec![[0, 1, 2], [1, 3, 2]]; + // + // + + let (points, indices) = build_diamond(&Isometry::identity()); + + let mesh = Mesh { + vertices: points + .iter() + .map(|p| Vertex { + position: mquad_from_na(*p), + uv: Vec2::new(p.x, p.y), + color: Color::new(0.9, 0.9, 0.9, 0.7), + }) + .collect(), + indices: indices.iter().flatten().map(|v| *v as u16).collect(), + texture: None, + }; + let trimesh = TriMesh::new(points, indices); + + for _i in 1.. { + clear_background(BLACK); + + let elapsed_time = get_time() as f32; + let slow_elapsed_time = elapsed_time / 3.0; + + let sin = (elapsed_time / 3f32).sin(); + let bias = 1.5 * sin.abs(); + let rotation = Quat::from_axis_angle( + Vec3::new(slow_elapsed_time.sin(), slow_elapsed_time.cos(), 1f32), + (elapsed_time * 50f32).to_radians(), + ); + let up_plane_vector = rotation * Vec3::Y; + let point_to_project = up_plane_vector * bias; + let projected_point = trimesh.project_point( + &Isometry::identity(), + &na_from_mquad(point_to_project), + true, + ); + + let slow_elapsed_time = slow_elapsed_time / 2.0; + // Going 3d! + set_camera(&Camera3D { + position: Vec3::new( + slow_elapsed_time.sin() * 5.0, + slow_elapsed_time.sin(), + slow_elapsed_time.cos() * 5.0, + ), + up: Vec3::new(0f32, 1f32, 0f32), + target: Vec3::new(0.5f32, 0f32, 0.5f32), + ..Default::default() + }); + + /* + * + * Render the projection + * + */ + let color = if projected_point.is_inside { + RED + } else { + YELLOW + }; + + draw_line_3d( + point_to_project, + mquad_from_na(projected_point.point), + color, + ); + draw_sphere(point_to_project, 0.1, None, color); + + draw_line_3d( + point_to_project, + mquad_from_na(projected_point.point), + color, + ); + + // fixed point inside + let point_to_project = Vec3::ZERO; + let projected_point = trimesh.project_point( + &Isometry::identity(), + &na_from_mquad(point_to_project), + true, + ); + let color = if projected_point.is_inside { + RED + } else { + YELLOW + }; + draw_line_3d( + point_to_project, + mquad_from_na(projected_point.point), + color, + ); + draw_sphere(point_to_project, 0.1, None, color); + + // Mesh is rendered in the back. + draw_mesh(&mesh); + + next_frame().await + } +} + +fn draw_polyline(polygon: Vec<(Vec3, Vec3)>, color: Color) { + for i in 0..polygon.len() { + let a = polygon[i].0; + let b = polygon[i].1; + draw_line_3d(a, b, color); + } +} + +fn mquad_from_na(a: Point3) -> Vec3 { + Vec3::new(a.x, a.y, a.z) +} + +fn na_from_mquad(a: Vec3) -> Point3 { + Point3::new(a.x, a.y, a.z) +} From 9526e753eac701448961756acc152cb14523d6ef Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 31 Jul 2024 20:47:24 +0200 Subject: [PATCH 02/16] simpler project example --- crates/parry3d/examples/project3d_animated.rs | 66 +++++-------------- 1 file changed, 18 insertions(+), 48 deletions(-) diff --git a/crates/parry3d/examples/project3d_animated.rs b/crates/parry3d/examples/project3d_animated.rs index 6c64486c..f3e9e27b 100644 --- a/crates/parry3d/examples/project3d_animated.rs +++ b/crates/parry3d/examples/project3d_animated.rs @@ -1,31 +1,9 @@ use macroquad::models::Vertex; use macroquad::prelude::*; -use nalgebra::{Point3, UnitVector3, Vector3}; +use nalgebra::{Point3, Vector3}; use parry3d::math::{Isometry, Real}; -use parry3d::query::{IntersectResult, PointQuery}; -use parry3d::shape::TriMesh; - -fn build_diamond(position: &Isometry) -> (Vec>, Vec<[u32; 3]>) { - // Two tetrahedrons sharing a face - let points = vec![ - position * Point3::new(0.0, 2.0, 0.0), - position * Point3::new(-2.0, -1.0, 0.0), - position * Point3::new(0.0, 0.0, 2.0), - position * Point3::new(2.0, -1.0, 0.0), - position * Point3::new(0.0, 0.0, -2.0), - ]; - - let indices = vec![ - [0u32, 1, 2], - [0, 2, 3], - [1, 2, 3], - [0, 1, 4], - [0, 4, 3], - [1, 4, 3], - ]; - - (points, indices) -} +use parry3d::query::PointQuery; +use parry3d::shape::{Cuboid, TriMesh, TriMeshFlags}; #[macroquad::main("parry3d::query::PlaneIntersection")] async fn main() { @@ -38,10 +16,8 @@ async fn main() { Point3::from([1.0, 0.0, 1.0]), ]; let _indices: Vec<[u32; 3]> = vec![[0, 1, 2], [1, 3, 2]]; - // - // - let (points, indices) = build_diamond(&Isometry::identity()); + let (points, indices) = Cuboid::new(Vector3::new(0.2, 0.5, 1.0)).to_trimesh(); let mesh = Mesh { vertices: points @@ -55,22 +31,24 @@ async fn main() { indices: indices.iter().flatten().map(|v| *v as u16).collect(), texture: None, }; - let trimesh = TriMesh::new(points, indices); - + let trimesh = TriMesh::with_flags( + points.clone(), + indices.clone(), + TriMeshFlags::ORIENTED + | TriMeshFlags::DELETE_BAD_TOPOLOGY_TRIANGLES + | TriMeshFlags::FIX_INTERNAL_EDGES, + ); for _i in 1.. { clear_background(BLACK); let elapsed_time = get_time() as f32; let slow_elapsed_time = elapsed_time / 3.0; - let sin = (elapsed_time / 3f32).sin(); - let bias = 1.5 * sin.abs(); - let rotation = Quat::from_axis_angle( - Vec3::new(slow_elapsed_time.sin(), slow_elapsed_time.cos(), 1f32), - (elapsed_time * 50f32).to_radians(), - ); - let up_plane_vector = rotation * Vec3::Y; - let point_to_project = up_plane_vector * bias; + let point_to_project = Vec3::new( + slow_elapsed_time.sin(), + slow_elapsed_time.cos() * 1.5, + (elapsed_time - slow_elapsed_time).cos(), + ) * slow_elapsed_time.sin().abs(); let projected_point = trimesh.project_point( &Isometry::identity(), &na_from_mquad(point_to_project), @@ -126,13 +104,13 @@ async fn main() { } else { YELLOW }; + draw_sphere(point_to_project, 0.1, None, color); + draw_line_3d( point_to_project, mquad_from_na(projected_point.point), color, ); - draw_sphere(point_to_project, 0.1, None, color); - // Mesh is rendered in the back. draw_mesh(&mesh); @@ -140,14 +118,6 @@ async fn main() { } } -fn draw_polyline(polygon: Vec<(Vec3, Vec3)>, color: Color) { - for i in 0..polygon.len() { - let a = polygon[i].0; - let b = polygon[i].1; - draw_line_3d(a, b, color); - } -} - fn mquad_from_na(a: Point3) -> Vec3 { Vec3::new(a.x, a.y, a.z) } From 113046d3637974b2419e8e75abb69a5d40be8acb Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 31 Jul 2024 21:08:45 +0200 Subject: [PATCH 03/16] use lissajous trajectory + small polish pass --- crates/parry3d/examples/project3d_animated.rs | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/crates/parry3d/examples/project3d_animated.rs b/crates/parry3d/examples/project3d_animated.rs index f3e9e27b..be8260fd 100644 --- a/crates/parry3d/examples/project3d_animated.rs +++ b/crates/parry3d/examples/project3d_animated.rs @@ -1,3 +1,5 @@ +use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6}; + use macroquad::models::Vertex; use macroquad::prelude::*; use nalgebra::{Point3, Vector3}; @@ -5,6 +7,16 @@ use parry3d::math::{Isometry, Real}; use parry3d::query::PointQuery; use parry3d::shape::{Cuboid, TriMesh, TriMeshFlags}; +fn lissajous_3d(t: f32) -> Vec3 { + // Some hardcoded parameters to have a pleasing lissajous trajectory. + let (a, b, c, delta_x, delta_y, delta_z) = (3.0, 2.0, 1.0, FRAC_PI_2, FRAC_PI_4, FRAC_PI_6); + + let x = (a * t + delta_x).sin(); + let y = (b * t + delta_y).sin(); + let z = (c * t + delta_z).sin(); + Vec3::new(x, y, z) * 0.75f32 +} + #[macroquad::main("parry3d::query::PlaneIntersection")] async fn main() { // @@ -31,31 +43,20 @@ async fn main() { indices: indices.iter().flatten().map(|v| *v as u16).collect(), texture: None, }; - let trimesh = TriMesh::with_flags( - points.clone(), - indices.clone(), - TriMeshFlags::ORIENTED - | TriMeshFlags::DELETE_BAD_TOPOLOGY_TRIANGLES - | TriMeshFlags::FIX_INTERNAL_EDGES, - ); + let trimesh = TriMesh::with_flags(points, indices, TriMeshFlags::ORIENTED); for _i in 1.. { clear_background(BLACK); let elapsed_time = get_time() as f32; let slow_elapsed_time = elapsed_time / 3.0; - let point_to_project = Vec3::new( - slow_elapsed_time.sin(), - slow_elapsed_time.cos() * 1.5, - (elapsed_time - slow_elapsed_time).cos(), - ) * slow_elapsed_time.sin().abs(); + let point_to_project = lissajous_3d(slow_elapsed_time); let projected_point = trimesh.project_point( &Isometry::identity(), &na_from_mquad(point_to_project), true, ); - let slow_elapsed_time = slow_elapsed_time / 2.0; // Going 3d! set_camera(&Camera3D { position: Vec3::new( @@ -63,8 +64,8 @@ async fn main() { slow_elapsed_time.sin(), slow_elapsed_time.cos() * 5.0, ), - up: Vec3::new(0f32, 1f32, 0f32), - target: Vec3::new(0.5f32, 0f32, 0.5f32), + up: Vec3::Y, + target: Vec3::ZERO, ..Default::default() }); @@ -111,7 +112,7 @@ async fn main() { mquad_from_na(projected_point.point), color, ); - // Mesh is rendered in the back. + // Mesh is rendered in the back, so we can see the other graphics elements draw_mesh(&mesh); next_frame().await From ed6a11299769ad4224ed52e769e9bd79ee87fcfd Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 14 Aug 2024 09:47:07 +0200 Subject: [PATCH 04/16] zoom in --- crates/parry3d/examples/project3d_animated.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/parry3d/examples/project3d_animated.rs b/crates/parry3d/examples/project3d_animated.rs index be8260fd..4c967596 100644 --- a/crates/parry3d/examples/project3d_animated.rs +++ b/crates/parry3d/examples/project3d_animated.rs @@ -37,7 +37,8 @@ async fn main() { .map(|p| Vertex { position: mquad_from_na(*p), uv: Vec2::new(p.x, p.y), - color: Color::new(0.9, 0.9, 0.9, 0.7), + color: [210, 210, 210, 150], + normal: vec4(0.0, 0.0, 0.0, 0.0), }) .collect(), indices: indices.iter().flatten().map(|v| *v as u16).collect(), @@ -57,12 +58,13 @@ async fn main() { true, ); + let slow_elapsed_time = slow_elapsed_time * 0.7; // Going 3d! set_camera(&Camera3D { position: Vec3::new( - slow_elapsed_time.sin() * 5.0, + slow_elapsed_time.sin() * 3.0, slow_elapsed_time.sin(), - slow_elapsed_time.cos() * 5.0, + slow_elapsed_time.cos() * 3.0, ), up: Vec3::Y, target: Vec3::ZERO, From b181c652df157b2713e32b112991a3086423332a Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 14 Aug 2024 09:49:08 +0200 Subject: [PATCH 05/16] scrape plane intersection --- crates/parry3d/Cargo.toml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/parry3d/Cargo.toml b/crates/parry3d/Cargo.toml index c4afd5c0..9685da66 100644 --- a/crates/parry3d/Cargo.toml +++ b/crates/parry3d/Cargo.toml @@ -173,6 +173,11 @@ name = "plane3d" path = "examples/plane3d.rs" doc-scrape-examples = true +[[example]] +name = "plane_intersection" +path = "examples/plane_intersection.rs" +doc-scrape-examples = true + [[example]] name = "polyline3d" path = "examples/polyline3d.rs" From 2a685142803212d17607d57e26aae491f5797fd5 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 14 Aug 2024 10:44:49 +0200 Subject: [PATCH 06/16] expose cuboid::to_trimesh for parry2d + add 2d example for projectpoint --- crates/parry2d/examples/project_point2d.rs | 126 ++++++++++++++++++ ...oject3d_animated.rs => project_point3d.rs} | 8 +- src/transformation/mod.rs | 1 - .../to_trimesh/cuboid_to_trimesh.rs | 79 +++++++---- src/transformation/to_trimesh/mod.rs | 6 + 5 files changed, 187 insertions(+), 33 deletions(-) create mode 100644 crates/parry2d/examples/project_point2d.rs rename crates/parry3d/examples/{project3d_animated.rs => project_point3d.rs} (94%) diff --git a/crates/parry2d/examples/project_point2d.rs b/crates/parry2d/examples/project_point2d.rs new file mode 100644 index 00000000..06db2c4c --- /dev/null +++ b/crates/parry2d/examples/project_point2d.rs @@ -0,0 +1,126 @@ +use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6}; + +use macroquad::models::Vertex; +use macroquad::prelude::*; +use nalgebra::{Point2, Point3, UnitComplex, UnitQuaternion, Vector2}; +use parry2d::math::{Isometry, Real, Translation}; +use parry2d::query::PointQuery; +use parry2d::shape::{Cuboid, TriMesh, TriMeshFlags}; + +fn lissajous_2d(t: f32) -> Vec2 { + // Some hardcoded parameters to have a pleasing lissajous trajectory. + let (a, b, delta_x, delta_y) = (3.0, 2.0, FRAC_PI_2, FRAC_PI_4); + + let x = (a * t + delta_x).sin(); + let y = (b * t + delta_y).sin(); + Vec2::new(x, y) * 0.75f32 +} + +const VIRTUAL_WIDTH: f32 = 1280.0; +const VIRTUAL_HEIGHT: f32 = 720.0; + +#[macroquad::main("parry3d::query::PlaneIntersection")] +async fn main() { + // + // This is useful to test for https://github.com/dimforge/parry/pull/248 + let _points = vec![ + Point3::from([0.0, 0.0, 0.0]), + Point3::from([0.0, 0.0, 1.0]), + Point3::from([1.0, 0.0, 0.0]), + Point3::from([1.0, 0.0, 1.0]), + ]; + let _indices: Vec<[u32; 3]> = vec![[0, 1, 2], [1, 3, 2]]; + + let scale = 200f32; + let (points, indices) = Cuboid::new(Vector2::new(0.2 * scale, 0.5 * scale)).to_trimesh(); + + let trimesh = TriMesh::with_flags(points, indices, TriMeshFlags::ORIENTED); + for _i in 1.. { + clear_background(BLACK); + + let elapsed_time = get_time() as f32; + let slow_elapsed_time = elapsed_time / 3.0; + + let offset = Vec2::new(screen_width() / 2f32, screen_height() / 2f32); + + let point_to_project = lissajous_2d(slow_elapsed_time) * scale + offset; + let translation = Translation::new(offset.x, offset.y); + let rot = UnitComplex::identity(); + let projected_point = trimesh.project_point( + &Isometry::from_parts(translation, rot), + &na_from_mquad(point_to_project), + true, + ); + + let slow_elapsed_time = slow_elapsed_time * 0.7; + + /* + * + * Render the projection + * + */ + let color = if projected_point.is_inside { + RED + } else { + YELLOW + }; + + draw_line_2d( + point_to_project, + mquad_from_na(projected_point.point), + color, + ); + draw_circle(point_to_project.x, point_to_project.y, 10f32, color); + + draw_line_2d( + point_to_project, + mquad_from_na(projected_point.point), + color, + ); + + // fixed point inside the shape + let point_to_project = Vec2::ZERO; + let projected_point = trimesh.project_local_point(&na_from_mquad(point_to_project), true); + let color = if projected_point.is_inside { + RED + } else { + YELLOW + }; + draw_circle(point_to_project.x, point_to_project.y, 2f32, color); + + draw_line_2d( + point_to_project, + mquad_from_na(projected_point.point), + color, + ); + // Mesh is rendered in the back, so we can see the other graphics elements + draw_trimesh2(&trimesh, offset); + + next_frame().await + } +} + +fn mquad_from_na(a: Point2) -> Vec2 { + Vec2::new(a.x, a.y) +} + +fn na_from_mquad(a: Vec2) -> Point2 { + Point2::new(a.x, a.y) +} + +fn draw_line_2d(a: Vec2, b: Vec2, color: Color) { + draw_line(a.x, a.y, b.x, b.y, 2f32, color); +} + +fn draw_trimesh2(trimesh: &TriMesh, offset: Vec2) { + let vertices = trimesh.vertices(); + for v in trimesh.indices() { + let v0 = mquad_from_na(vertices[v[0] as usize]) + offset; + let v1 = mquad_from_na(vertices[v[1] as usize]) + offset; + let v2 = mquad_from_na(vertices[v[2] as usize]) + offset; + + draw_line(v0.x, v0.y, v1.x, v1.y, 2f32, WHITE); + draw_line(v0.x, v0.y, v2.x, v2.y, 2f32, WHITE); + draw_line(v2.x, v2.y, v1.x, v1.y, 2f32, WHITE); + } +} diff --git a/crates/parry3d/examples/project3d_animated.rs b/crates/parry3d/examples/project_point3d.rs similarity index 94% rename from crates/parry3d/examples/project3d_animated.rs rename to crates/parry3d/examples/project_point3d.rs index 4c967596..efa849c9 100644 --- a/crates/parry3d/examples/project3d_animated.rs +++ b/crates/parry3d/examples/project_point3d.rs @@ -95,13 +95,9 @@ async fn main() { color, ); - // fixed point inside + // fixed point inside the shape let point_to_project = Vec3::ZERO; - let projected_point = trimesh.project_point( - &Isometry::identity(), - &na_from_mquad(point_to_project), - true, - ); + let projected_point = trimesh.project_local_point(&na_from_mquad(point_to_project), true); let color = if projected_point.is_inside { RED } else { diff --git a/src/transformation/mod.rs b/src/transformation/mod.rs index be7e8348..7b29e427 100644 --- a/src/transformation/mod.rs +++ b/src/transformation/mod.rs @@ -36,7 +36,6 @@ mod mesh_intersection; mod to_outline; #[cfg(feature = "dim2")] mod to_polyline; -#[cfg(feature = "dim3")] mod to_trimesh; pub mod utils; diff --git a/src/transformation/to_trimesh/cuboid_to_trimesh.rs b/src/transformation/to_trimesh/cuboid_to_trimesh.rs index 1c763a22..3e1dcd35 100644 --- a/src/transformation/to_trimesh/cuboid_to_trimesh.rs +++ b/src/transformation/to_trimesh/cuboid_to_trimesh.rs @@ -2,11 +2,20 @@ use crate::bounding_volume::Aabb; use crate::math::Real; use crate::shape::Cuboid; use crate::transformation::utils; + +#[cfg(feature = "dim3")] +use na::Point3 as PointDim; +#[cfg(feature = "dim3")] use na::{self, Point3}; +#[cfg(feature = "dim2")] +use na::Point2 as PointDim; +#[cfg(feature = "dim2")] +use na::{self, Point2}; + impl Aabb { /// Discretize the boundary of this Aabb as a triangle-mesh. - pub fn to_trimesh(&self) -> (Vec>, Vec<[u32; 3]>) { + pub fn to_trimesh(&self) -> (Vec>, Vec<[u32; 3]>) { let center = self.center(); let half_extents = self.half_extents(); let mut cube_mesh = Cuboid::new(half_extents).to_trimesh(); @@ -17,7 +26,7 @@ impl Aabb { impl Cuboid { /// Discretize the boundary of this cuboid as a triangle-mesh. - pub fn to_trimesh(&self) -> (Vec>, Vec<[u32; 3]>) { + pub fn to_trimesh(&self) -> (Vec>, Vec<[u32; 3]>) { let (vtx, idx) = unit_cuboid(); (utils::scaled(vtx, self.half_extents * 2.0), idx) } @@ -28,36 +37,54 @@ impl Cuboid { * * The cuboid is centered at the origin, and has its half extents set to 0.5. */ -fn unit_cuboid() -> (Vec>, Vec<[u32; 3]>) { - let mut coords = Vec::with_capacity(8); - let mut faces = Vec::with_capacity(12); +fn unit_cuboid() -> (Vec>, Vec<[u32; 3]>) { + #[cfg(feature = "dim3")] + return { + let mut coords = Vec::with_capacity(8); + let mut faces = Vec::with_capacity(12); + + coords.push(Point3::new(-0.5, -0.5, 0.5)); + coords.push(Point3::new(-0.5, -0.5, -0.5)); + coords.push(Point3::new(0.5, -0.5, -0.5)); + coords.push(Point3::new(0.5, -0.5, 0.5)); + coords.push(Point3::new(-0.5, 0.5, 0.5)); + coords.push(Point3::new(-0.5, 0.5, -0.5)); + coords.push(Point3::new(0.5, 0.5, -0.5)); + coords.push(Point3::new(0.5, 0.5, 0.5)); + + faces.push([4, 5, 0]); + faces.push([5, 1, 0]); + + faces.push([5, 6, 1]); + faces.push([6, 2, 1]); - coords.push(Point3::new(-0.5, -0.5, 0.5)); - coords.push(Point3::new(-0.5, -0.5, -0.5)); - coords.push(Point3::new(0.5, -0.5, -0.5)); - coords.push(Point3::new(0.5, -0.5, 0.5)); - coords.push(Point3::new(-0.5, 0.5, 0.5)); - coords.push(Point3::new(-0.5, 0.5, -0.5)); - coords.push(Point3::new(0.5, 0.5, -0.5)); - coords.push(Point3::new(0.5, 0.5, 0.5)); + faces.push([6, 7, 3]); + faces.push([2, 6, 3]); - faces.push([4, 5, 0]); - faces.push([5, 1, 0]); + faces.push([7, 4, 0]); + faces.push([3, 7, 0]); - faces.push([5, 6, 1]); - faces.push([6, 2, 1]); + faces.push([0, 1, 2]); + faces.push([3, 0, 2]); - faces.push([6, 7, 3]); - faces.push([2, 6, 3]); + faces.push([7, 6, 5]); + faces.push([4, 7, 5]); - faces.push([7, 4, 0]); - faces.push([3, 7, 0]); + (coords, faces) + }; + #[cfg(feature = "dim2")] + return { + let mut coords = Vec::with_capacity(8); + let mut faces = Vec::with_capacity(12); - faces.push([0, 1, 2]); - faces.push([3, 0, 2]); + coords.push(Point2::new(-0.5, -0.5)); + coords.push(Point2::new(0.5, -0.5)); + coords.push(Point2::new(-0.5, 0.5)); + coords.push(Point2::new(0.5, 0.5)); - faces.push([7, 6, 5]); - faces.push([4, 7, 5]); + faces.push([0, 1, 2]); + faces.push([2, 1, 3]); - (coords, faces) + (coords, faces) + }; } diff --git a/src/transformation/to_trimesh/mod.rs b/src/transformation/to_trimesh/mod.rs index 6c730f12..60dc33a7 100644 --- a/src/transformation/to_trimesh/mod.rs +++ b/src/transformation/to_trimesh/mod.rs @@ -1,7 +1,13 @@ +#[cfg(feature = "dim3")] mod ball_to_trimesh; +#[cfg(feature = "dim3")] mod capsule_to_trimesh; +#[cfg(feature = "dim3")] mod cone_to_trimesh; +#[cfg(feature = "dim3")] mod convex_polyhedron_to_trimesh; mod cuboid_to_trimesh; +#[cfg(feature = "dim3")] mod cylinder_to_trimesh; +#[cfg(feature = "dim3")] mod heightfield_to_trimesh; From 065f6b69c6e6567451eba5937ead3816c936aab3 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 14 Aug 2024 10:48:14 +0200 Subject: [PATCH 07/16] fix local point inside the shape --- crates/parry2d/examples/project_point2d.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/parry2d/examples/project_point2d.rs b/crates/parry2d/examples/project_point2d.rs index 06db2c4c..1e205aa1 100644 --- a/crates/parry2d/examples/project_point2d.rs +++ b/crates/parry2d/examples/project_point2d.rs @@ -78,7 +78,7 @@ async fn main() { color, ); - // fixed point inside the shape + // fixed local point inside the shape let point_to_project = Vec2::ZERO; let projected_point = trimesh.project_local_point(&na_from_mquad(point_to_project), true); let color = if projected_point.is_inside { @@ -86,11 +86,13 @@ async fn main() { } else { YELLOW }; - draw_circle(point_to_project.x, point_to_project.y, 2f32, color); + // convert to "world" space + let point_to_project = point_to_project * scale + offset; + draw_circle(point_to_project.x, point_to_project.y, 10f32, color); draw_line_2d( point_to_project, - mquad_from_na(projected_point.point), + mquad_from_na(projected_point.point) * scale + offset, color, ); // Mesh is rendered in the back, so we can see the other graphics elements From 174f0121f5a848e89df7c12e4b25ea86cf58a73e Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 14 Aug 2024 11:05:38 +0200 Subject: [PATCH 08/16] common macroquad code in their example --- crates/parry3d/examples/common_macroquad.rs | 46 +++++++++++++++++++ crates/parry3d/examples/plane_intersection.rs | 28 ++++------- crates/parry3d/examples/project_point3d.rs | 35 ++------------ 3 files changed, 58 insertions(+), 51 deletions(-) create mode 100644 crates/parry3d/examples/common_macroquad.rs diff --git a/crates/parry3d/examples/common_macroquad.rs b/crates/parry3d/examples/common_macroquad.rs new file mode 100644 index 00000000..1371127d --- /dev/null +++ b/crates/parry3d/examples/common_macroquad.rs @@ -0,0 +1,46 @@ +use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6}; + +use macroquad::{ + color::{Color, WHITE}, + math::Vec3, + models::draw_line_3d, +}; +use nalgebra::Point3; +use parry3d::math::Real; + +fn main() { + println!( + "This module contains helper fubnctions to use macroquad, + isolated from the rest of the examples for the sake of simplicity." + ); +} + +pub fn mquad_from_na(a: Point3) -> Vec3 { + Vec3::new(a.x, a.y, a.z) +} + +pub fn na_from_mquad(a: Vec3) -> Point3 { + Point3::new(a.x, a.y, a.z) +} + +pub fn lissajous_3d(t: f32) -> Vec3 { + // Some hardcoded parameters to have a pleasing lissajous trajectory. + let (a, b, c, delta_x, delta_y, delta_z) = (3.0, 2.0, 1.0, FRAC_PI_2, FRAC_PI_4, FRAC_PI_6); + + let x = (a * t + delta_x).sin(); + let y = (b * t + delta_y).sin(); + let z = (c * t + delta_z).sin(); + Vec3::new(x, y, z) * 0.75f32 +} + +pub fn draw_polyline(polygon: Vec<(Vec3, Vec3)>, color: Color) { + for i in 0..polygon.len() { + let a = polygon[i].0; + let b = polygon[i].1; + draw_line_3d(a, b, color); + } +} + +pub fn easy_draw_text(text: &str) { + macroquad::text::draw_text(text, 10.0, 48.0 + 18.0, 30.0, WHITE); +} diff --git a/crates/parry3d/examples/plane_intersection.rs b/crates/parry3d/examples/plane_intersection.rs index c80fc0e5..97d1d3b5 100644 --- a/crates/parry3d/examples/plane_intersection.rs +++ b/crates/parry3d/examples/plane_intersection.rs @@ -5,6 +5,9 @@ use parry3d::math::Real; use parry3d::query::IntersectResult; use parry3d::shape::{Cuboid, TriMesh}; +mod common_macroquad; +use common_macroquad::*; + #[macroquad::main("parry3d::query::PlaneIntersection")] async fn main() { let trimesh = Cuboid::new(Vector3::repeat(1.0)).to_trimesh(); @@ -64,15 +67,15 @@ async fn main() { Color::new(0f32, 1f32, 0f32, 1f32), ); set_default_camera(); - draw_text("Intersection found!"); + easy_draw_text("Intersection found!"); } IntersectResult::Negative => { set_default_camera(); - draw_text("No intersection found, the shape is below the plane."); + easy_draw_text("No intersection found, the shape is below the plane."); } IntersectResult::Positive => { set_default_camera(); - draw_text("No intersection found, the shape is above the plane."); + easy_draw_text("No intersection found, the shape is above the plane."); } } next_frame().await @@ -95,7 +98,8 @@ fn mquad_mesh_from_points(trimesh: &(Vec>, Vec<[u32; 3]>), camera_p indices.iter().flatten().map(|v| *v as u16).collect(), ); - // Macroquad doesn´t support adding normals to vertices, so we'll bake a color into these vertices. + // Macroquad does support adding normals to vertices, but we´d have to provide shaders for them. + // so we're baking a color into these vertices. // See https://github.com/not-fl3/macroquad/issues/321. // Compute the normal of each vertex, making them unique @@ -140,19 +144,3 @@ fn mquad_compute_normals(points: &Vec, indices: &Vec, cam_pos: Vec3 } vertices } - -fn draw_polyline(polygon: Vec<(Vec3, Vec3)>, color: Color) { - for i in 0..polygon.len() { - let a = polygon[i].0; - let b = polygon[i].1; - draw_line_3d(a, b, color); - } -} - -fn mquad_from_na(a: Point3) -> Vec3 { - Vec3::new(a.x, a.y, a.z) -} - -fn draw_text(text: &str) { - macroquad::text::draw_text(text, 10.0, 48.0 + 18.0, 30.0, WHITE); -} diff --git a/crates/parry3d/examples/project_point3d.rs b/crates/parry3d/examples/project_point3d.rs index efa849c9..f08b1eb5 100644 --- a/crates/parry3d/examples/project_point3d.rs +++ b/crates/parry3d/examples/project_point3d.rs @@ -1,34 +1,15 @@ -use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6}; - use macroquad::models::Vertex; use macroquad::prelude::*; -use nalgebra::{Point3, Vector3}; -use parry3d::math::{Isometry, Real}; +use nalgebra::Vector3; +use parry3d::math::Isometry; use parry3d::query::PointQuery; use parry3d::shape::{Cuboid, TriMesh, TriMeshFlags}; -fn lissajous_3d(t: f32) -> Vec3 { - // Some hardcoded parameters to have a pleasing lissajous trajectory. - let (a, b, c, delta_x, delta_y, delta_z) = (3.0, 2.0, 1.0, FRAC_PI_2, FRAC_PI_4, FRAC_PI_6); - - let x = (a * t + delta_x).sin(); - let y = (b * t + delta_y).sin(); - let z = (c * t + delta_z).sin(); - Vec3::new(x, y, z) * 0.75f32 -} +mod common_macroquad; +use common_macroquad::*; #[macroquad::main("parry3d::query::PlaneIntersection")] async fn main() { - // - // This is useful to test for https://github.com/dimforge/parry/pull/248 - let _points = vec![ - Point3::from([0.0, 0.0, 0.0]), - Point3::from([0.0, 0.0, 1.0]), - Point3::from([1.0, 0.0, 0.0]), - Point3::from([1.0, 0.0, 1.0]), - ]; - let _indices: Vec<[u32; 3]> = vec![[0, 1, 2], [1, 3, 2]]; - let (points, indices) = Cuboid::new(Vector3::new(0.2, 0.5, 1.0)).to_trimesh(); let mesh = Mesh { @@ -116,11 +97,3 @@ async fn main() { next_frame().await } } - -fn mquad_from_na(a: Point3) -> Vec3 { - Vec3::new(a.x, a.y, a.z) -} - -fn na_from_mquad(a: Vec3) -> Point3 { - Point3::new(a.x, a.y, a.z) -} From 3955d443f2b78626be4d480d65c7ec26285728bb Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Wed, 14 Aug 2024 11:13:14 +0200 Subject: [PATCH 09/16] use a shaded cube for project_point2d too. --- crates/parry3d/examples/common_macroquad.rs | 79 ++++++++++++++++++- crates/parry3d/examples/plane_intersection.rs | 67 +--------------- crates/parry3d/examples/project_point3d.rs | 21 ++--- 3 files changed, 84 insertions(+), 83 deletions(-) diff --git a/crates/parry3d/examples/common_macroquad.rs b/crates/parry3d/examples/common_macroquad.rs index 1371127d..a326e65c 100644 --- a/crates/parry3d/examples/common_macroquad.rs +++ b/crates/parry3d/examples/common_macroquad.rs @@ -1,9 +1,10 @@ use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6}; use macroquad::{ - color::{Color, WHITE}, - math::Vec3, - models::draw_line_3d, + color::{Color, DARKGRAY, WHITE}, + math::{Vec2, Vec3, Vec4}, + models::{draw_line_3d, Mesh}, + ui::Vertex, }; use nalgebra::Point3; use parry3d::math::Real; @@ -44,3 +45,75 @@ pub fn draw_polyline(polygon: Vec<(Vec3, Vec3)>, color: Color) { pub fn easy_draw_text(text: &str) { macroquad::text::draw_text(text, 10.0, 48.0 + 18.0, 30.0, WHITE); } + +pub fn mquad_mesh_from_points( + trimesh: &(Vec>, Vec<[u32; 3]>), + light_pos: Vec3, + color: Color, +) -> Mesh { + let (points, indices) = trimesh; + // Transform the parry mesh into a mquad Mesh + let (mquad_points, mquad_indices) = ( + points + .iter() + .map(|p| Vertex { + position: mquad_from_na(*p), + uv: Vec2::new(p.x, p.y), + color: color.into(), + normal: Vec4::ZERO, + }) + .collect(), + indices.iter().flatten().map(|v| *v as u16).collect(), + ); + + // Macroquad does support adding normals to vertices, but we´d have to provide shaders for them. + // so we're baking a color into these vertices. + // See https://github.com/not-fl3/macroquad/issues/321. + + // Compute the normal of each vertex, making them unique + let vertices: Vec = + mquad_compute_normals_and_bake_light(&mquad_points, &mquad_indices, light_pos); + // Regenerate the index for each vertex. + let indices: Vec = (0..vertices.len() * 3) + .into_iter() + .map(|i| i as u16) + .collect(); + let mesh = Mesh { + vertices, + indices, + texture: None, + }; + mesh +} + +pub fn mquad_compute_normals_and_bake_light( + points: &Vec, + indices: &Vec, + light_pos: Vec3, +) -> Vec { + let mut vertices: Vec = Vec::::new(); + for indices in indices.chunks(3) { + let v0 = &points[indices[0] as usize]; + let v1 = &points[indices[1] as usize]; + let v2 = &points[indices[2] as usize]; + let normal = (v0.position - v2.position) + .cross(v1.position - v2.position) + .normalize(); + let brightness_mod = 0.2 + (0.8 / 2.) * (normal.dot(light_pos) + 1.); + + for &i in indices.iter() { + let mut color = points[i as usize].color; + color[0] = (color[0] as f32 * brightness_mod) as u8; + color[1] = (color[1] as f32 * brightness_mod) as u8; + color[2] = (color[2] as f32 * brightness_mod) as u8; + + vertices.push(Vertex { + position: points[i as usize].position, + uv: Vec2::ZERO, + color: color, + normal: Vec4::ZERO, + }); + } + } + vertices +} diff --git a/crates/parry3d/examples/plane_intersection.rs b/crates/parry3d/examples/plane_intersection.rs index 97d1d3b5..05ef54ef 100644 --- a/crates/parry3d/examples/plane_intersection.rs +++ b/crates/parry3d/examples/plane_intersection.rs @@ -1,7 +1,5 @@ -use macroquad::models::Vertex; use macroquad::prelude::*; -use nalgebra::{Point3, UnitVector3, Vector3}; -use parry3d::math::Real; +use nalgebra::{UnitVector3, Vector3}; use parry3d::query::IntersectResult; use parry3d::shape::{Cuboid, TriMesh}; @@ -81,66 +79,3 @@ async fn main() { next_frame().await } } - -fn mquad_mesh_from_points(trimesh: &(Vec>, Vec<[u32; 3]>), camera_pos: Vec3) -> Mesh { - let (points, indices) = trimesh; - // Transform the parry mesh into a mquad Mesh - let (mquad_points, mquad_indices) = ( - points - .iter() - .map(|p| Vertex { - position: mquad_from_na(*p), - uv: Vec2::new(p.x, p.y), - color: DARKGRAY.into(), - normal: Vec4::ZERO, - }) - .collect(), - indices.iter().flatten().map(|v| *v as u16).collect(), - ); - - // Macroquad does support adding normals to vertices, but we´d have to provide shaders for them. - // so we're baking a color into these vertices. - // See https://github.com/not-fl3/macroquad/issues/321. - - // Compute the normal of each vertex, making them unique - let vertices: Vec = mquad_compute_normals(&mquad_points, &mquad_indices, camera_pos); - // Regenerate the index for each vertex. - let indices: Vec = (0..vertices.len() * 3) - .into_iter() - .map(|i| i as u16) - .collect(); - let mesh = Mesh { - vertices, - indices, - texture: None, - }; - mesh -} - -fn mquad_compute_normals(points: &Vec, indices: &Vec, cam_pos: Vec3) -> Vec { - let mut vertices: Vec = Vec::::new(); - for indices in indices.chunks(3) { - let v0 = &points[indices[0] as usize]; - let v1 = &points[indices[1] as usize]; - let v2 = &points[indices[2] as usize]; - let normal = (v0.position - v2.position) - .cross(v1.position - v2.position) - .normalize(); - let brightness_mod = 0.2 + (0.8 / 2.) * (normal.dot(cam_pos) + 1.); - - for &i in indices.iter() { - let mut color = points[i as usize].color; - color[0] = (color[0] as f32 * brightness_mod) as u8; - color[1] = (color[1] as f32 * brightness_mod) as u8; - color[2] = (color[2] as f32 * brightness_mod) as u8; - - vertices.push(Vertex { - position: points[i as usize].position, - uv: Vec2::ZERO, - color: color, - normal: Vec4::ZERO, - }); - } - } - vertices -} diff --git a/crates/parry3d/examples/project_point3d.rs b/crates/parry3d/examples/project_point3d.rs index f08b1eb5..b1b56e94 100644 --- a/crates/parry3d/examples/project_point3d.rs +++ b/crates/parry3d/examples/project_point3d.rs @@ -10,21 +10,14 @@ use common_macroquad::*; #[macroquad::main("parry3d::query::PlaneIntersection")] async fn main() { - let (points, indices) = Cuboid::new(Vector3::new(0.2, 0.5, 1.0)).to_trimesh(); + let trimesh = Cuboid::new(Vector3::new(0.2, 0.5, 1.0)).to_trimesh(); - let mesh = Mesh { - vertices: points - .iter() - .map(|p| Vertex { - position: mquad_from_na(*p), - uv: Vec2::new(p.x, p.y), - color: [210, 210, 210, 150], - normal: vec4(0.0, 0.0, 0.0, 0.0), - }) - .collect(), - indices: indices.iter().flatten().map(|v| *v as u16).collect(), - texture: None, - }; + let mesh = mquad_mesh_from_points( + &trimesh, + Vec3::new(1f32, 3f32, 3f32), + Color::from_rgba(200, 200, 200, 150), + ); + let (points, indices) = trimesh; let trimesh = TriMesh::with_flags(points, indices, TriMeshFlags::ORIENTED); for _i in 1.. { clear_background(BLACK); From 4bbe2984d2f59f591a727f75e75d98bc457840e3 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Tue, 3 Sep 2024 11:37:11 +0200 Subject: [PATCH 10/16] lighter cube --- crates/parry3d/examples/common_macroquad.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/parry3d/examples/common_macroquad.rs b/crates/parry3d/examples/common_macroquad.rs index a326e65c..0bd37202 100644 --- a/crates/parry3d/examples/common_macroquad.rs +++ b/crates/parry3d/examples/common_macroquad.rs @@ -1,7 +1,8 @@ +#[allow(unused, dead_code)] use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6}; use macroquad::{ - color::{Color, DARKGRAY, WHITE}, + color::{Color, WHITE}, math::{Vec2, Vec3, Vec4}, models::{draw_line_3d, Mesh}, ui::Vertex, @@ -99,7 +100,7 @@ pub fn mquad_compute_normals_and_bake_light( let normal = (v0.position - v2.position) .cross(v1.position - v2.position) .normalize(); - let brightness_mod = 0.2 + (0.8 / 2.) * (normal.dot(light_pos) + 1.); + let brightness_mod = 0.4 + (0.6 / 2.) * (normal.dot(light_pos) + 1.); for &i in indices.iter() { let mut color = points[i as usize].color; From 2dcadc92eca2bc328d779d9d830a0dd3120aec3f Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Tue, 3 Sep 2024 12:09:16 +0200 Subject: [PATCH 11/16] fix plane intersection code with common code --- crates/parry3d/examples/plane_intersection.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/parry3d/examples/plane_intersection.rs b/crates/parry3d/examples/plane_intersection.rs index 05ef54ef..567d4dfc 100644 --- a/crates/parry3d/examples/plane_intersection.rs +++ b/crates/parry3d/examples/plane_intersection.rs @@ -10,9 +10,10 @@ use common_macroquad::*; async fn main() { let trimesh = Cuboid::new(Vector3::repeat(1.0)).to_trimesh(); + let light_pos = Vec3::new(-1f32, 3.5f32, -3f32); let camera_pos = Vec3::new(-1.5f32, 2.5f32, -3f32); - let mesh = mquad_mesh_from_points(&trimesh, camera_pos); + let mesh = mquad_mesh_from_points(&trimesh, light_pos, DARKGRAY); let trimesh = TriMesh::new(trimesh.0, trimesh.1); for _ in 1.. { From d61b05b76c3e613fea36ef6d5e74bd8c213ae47f Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Tue, 3 Sep 2024 12:13:21 +0200 Subject: [PATCH 12/16] removed a few unused variables --- crates/parry2d/examples/project_point2d.rs | 10 ++-------- crates/parry3d/examples/project_point3d.rs | 1 - 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/crates/parry2d/examples/project_point2d.rs b/crates/parry2d/examples/project_point2d.rs index 1e205aa1..d5cc8b90 100644 --- a/crates/parry2d/examples/project_point2d.rs +++ b/crates/parry2d/examples/project_point2d.rs @@ -1,8 +1,7 @@ -use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6}; +use std::f32::consts::{FRAC_PI_2, FRAC_PI_4}; -use macroquad::models::Vertex; use macroquad::prelude::*; -use nalgebra::{Point2, Point3, UnitComplex, UnitQuaternion, Vector2}; +use nalgebra::{Point2, Point3, UnitComplex, Vector2}; use parry2d::math::{Isometry, Real, Translation}; use parry2d::query::PointQuery; use parry2d::shape::{Cuboid, TriMesh, TriMeshFlags}; @@ -16,9 +15,6 @@ fn lissajous_2d(t: f32) -> Vec2 { Vec2::new(x, y) * 0.75f32 } -const VIRTUAL_WIDTH: f32 = 1280.0; -const VIRTUAL_HEIGHT: f32 = 720.0; - #[macroquad::main("parry3d::query::PlaneIntersection")] async fn main() { // @@ -52,8 +48,6 @@ async fn main() { true, ); - let slow_elapsed_time = slow_elapsed_time * 0.7; - /* * * Render the projection diff --git a/crates/parry3d/examples/project_point3d.rs b/crates/parry3d/examples/project_point3d.rs index b1b56e94..408ac3fb 100644 --- a/crates/parry3d/examples/project_point3d.rs +++ b/crates/parry3d/examples/project_point3d.rs @@ -1,4 +1,3 @@ -use macroquad::models::Vertex; use macroquad::prelude::*; use nalgebra::Vector3; use parry3d::math::Isometry; From 8ab7b5c995d6c4eb50db59391771f714c29a0d5c Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Tue, 3 Sep 2024 14:42:22 +0200 Subject: [PATCH 13/16] 2d examples use common macroquad code --- crates/parry2d/examples/common_macroquad.rs | 100 ++++++++++++++++++ crates/parry2d/examples/point_in_poly2d.rs | 44 ++------ .../examples/polygons_intersection2d.rs | 18 +--- crates/parry2d/examples/project_point2d.rs | 41 +------ crates/parry2d/examples/raycasts_animated.rs | 23 +--- crates/parry3d/examples/common_macroquad.rs | 2 +- 6 files changed, 117 insertions(+), 111 deletions(-) create mode 100644 crates/parry2d/examples/common_macroquad.rs diff --git a/crates/parry2d/examples/common_macroquad.rs b/crates/parry2d/examples/common_macroquad.rs new file mode 100644 index 00000000..6326afe2 --- /dev/null +++ b/crates/parry2d/examples/common_macroquad.rs @@ -0,0 +1,100 @@ +#[allow(unused, dead_code)] +use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6}; + +use macroquad::prelude::*; +use macroquad::{ + color::{Color, WHITE}, + math::Vec2, + shapes::draw_line, +}; +use nalgebra::Point2; +use parry2d::math::Real; +use parry2d::shape::TriMesh; + +fn main() { + println!( + "This module contains helper functions to use macroquad, + isolated from the rest of the examples for the sake of simplicity." + ); +} + +pub fn mquad_from_na(a: Point2) -> Vec2 { + Vec2::new(a.x, a.y) +} + +pub fn na_from_mquad(a: Vec2) -> Point2 { + Point2::new(a.x, a.y) +} + +pub fn draw_polyline(polygon: Vec<(Vec2, Vec2)>, color: Color) { + for i in 0..polygon.len() { + let a = polygon[i].0; + let b = polygon[i].1; + draw_line_2d(a, b, color); + } +} + +pub fn easy_draw_text(text: &str) { + macroquad::text::draw_text(text, 10.0, 48.0 + 18.0, 30.0, WHITE); +} + +pub fn lissajous_2d(t: f32) -> Vec2 { + // Some hardcoded parameters to have a pleasing lissajous trajectory. + let (a, b, delta_x, delta_y) = (3.0, 2.0, FRAC_PI_2, FRAC_PI_4); + + let x = (a * t + delta_x).sin(); + let y = (b * t + delta_y).sin(); + Vec2::new(x, y) * 0.75f32 +} + +pub fn draw_line_2d(a: Vec2, b: Vec2, color: Color) { + draw_line(a.x, a.y, b.x, b.y, 2f32, color); +} + +pub fn draw_trimesh2(trimesh: &TriMesh, offset: Vec2) { + let vertices = trimesh.vertices(); + for v in trimesh.indices() { + let v0 = mquad_from_na(vertices[v[0] as usize]) + offset; + let v1 = mquad_from_na(vertices[v[1] as usize]) + offset; + let v2 = mquad_from_na(vertices[v[2] as usize]) + offset; + + draw_line(v0.x, v0.y, v1.x, v1.y, 2f32, WHITE); + draw_line(v0.x, v0.y, v2.x, v2.y, 2f32, WHITE); + draw_line(v2.x, v2.y, v1.x, v1.y, 2f32, WHITE); + } +} + +pub fn draw_polygon(polygon: &[Point2], scale: f32, shift: Point2, color: Color) { + for i in 0..polygon.len() { + let a = polygon[i]; + let b = polygon[(i + 1) % polygon.len()]; + draw_line( + a.x * scale + shift.x, + a.y * scale + shift.y, + b.x * scale + shift.x, + b.y * scale + shift.y, + 2.0, + color, + ); + } +} + +pub 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, + ); +} diff --git a/crates/parry2d/examples/point_in_poly2d.rs b/crates/parry2d/examples/point_in_poly2d.rs index 6933c21e..fd85e046 100644 --- a/crates/parry2d/examples/point_in_poly2d.rs +++ b/crates/parry2d/examples/point_in_poly2d.rs @@ -1,3 +1,6 @@ +mod common_macroquad; + +use common_macroquad::{draw_point, draw_polygon}; use macroquad::prelude::*; use nalgebra::{Point2, UnitComplex, Vector2}; use parry2d::utils::point_in_poly2d; @@ -15,14 +18,14 @@ async fn main() { loop { clear_background(BLACK); - /* - * Compute polygon intersections. - */ spikes .iter_mut() .for_each(|pt| *pt = animation_rotation * *pt); draw_polygon(&spikes, RENDER_SCALE, spikes_render_pos, BLUE); + /* + * Compute polygon intersections. + */ for point in &test_points { if point_in_poly2d(point, &spikes) { draw_point(*point, RENDER_SCALE, spikes_render_pos, RED); @@ -71,38 +74,3 @@ fn grid_points() -> Vec> { } pts } - -fn draw_polygon(polygon: &[Point2], scale: f32, shift: Point2, color: Color) { - for i in 0..polygon.len() { - let a = polygon[i]; - let b = polygon[(i + 1) % polygon.len()]; - draw_line( - a.x * scale + shift.x, - a.y * scale + shift.y, - b.x * scale + shift.x, - b.y * scale + shift.y, - 2.0, - 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, - ); -} diff --git a/crates/parry2d/examples/polygons_intersection2d.rs b/crates/parry2d/examples/polygons_intersection2d.rs index 51d03079..6c3164b3 100644 --- a/crates/parry2d/examples/polygons_intersection2d.rs +++ b/crates/parry2d/examples/polygons_intersection2d.rs @@ -1,3 +1,6 @@ +mod common_macroquad; + +use common_macroquad::draw_polygon; use macroquad::prelude::*; use nalgebra::{Point2, UnitComplex, Vector2}; use parry2d::shape::Ball; @@ -111,18 +114,3 @@ fn spikes_polygon() -> Vec> { polygon } - -fn draw_polygon(polygon: &[Point2], scale: f32, shift: Point2, color: Color) { - for i in 0..polygon.len() { - let a = polygon[i]; - let b = polygon[(i + 1) % polygon.len()]; - draw_line( - a.x * scale + shift.x, - a.y * scale + shift.y, - b.x * scale + shift.x, - b.y * scale + shift.y, - 2.0, - color, - ); - } -} diff --git a/crates/parry2d/examples/project_point2d.rs b/crates/parry2d/examples/project_point2d.rs index d5cc8b90..ca484eef 100644 --- a/crates/parry2d/examples/project_point2d.rs +++ b/crates/parry2d/examples/project_point2d.rs @@ -1,20 +1,12 @@ -use std::f32::consts::{FRAC_PI_2, FRAC_PI_4}; +mod common_macroquad; +use common_macroquad::{draw_line_2d, draw_trimesh2, lissajous_2d, mquad_from_na, na_from_mquad}; use macroquad::prelude::*; -use nalgebra::{Point2, Point3, UnitComplex, Vector2}; -use parry2d::math::{Isometry, Real, Translation}; +use nalgebra::{Point3, UnitComplex, Vector2}; +use parry2d::math::{Isometry, Translation}; use parry2d::query::PointQuery; use parry2d::shape::{Cuboid, TriMesh, TriMeshFlags}; -fn lissajous_2d(t: f32) -> Vec2 { - // Some hardcoded parameters to have a pleasing lissajous trajectory. - let (a, b, delta_x, delta_y) = (3.0, 2.0, FRAC_PI_2, FRAC_PI_4); - - let x = (a * t + delta_x).sin(); - let y = (b * t + delta_y).sin(); - Vec2::new(x, y) * 0.75f32 -} - #[macroquad::main("parry3d::query::PlaneIntersection")] async fn main() { // @@ -95,28 +87,3 @@ async fn main() { next_frame().await } } - -fn mquad_from_na(a: Point2) -> Vec2 { - Vec2::new(a.x, a.y) -} - -fn na_from_mquad(a: Vec2) -> Point2 { - Point2::new(a.x, a.y) -} - -fn draw_line_2d(a: Vec2, b: Vec2, color: Color) { - draw_line(a.x, a.y, b.x, b.y, 2f32, color); -} - -fn draw_trimesh2(trimesh: &TriMesh, offset: Vec2) { - let vertices = trimesh.vertices(); - for v in trimesh.indices() { - let v0 = mquad_from_na(vertices[v[0] as usize]) + offset; - let v1 = mquad_from_na(vertices[v[1] as usize]) + offset; - let v2 = mquad_from_na(vertices[v[2] as usize]) + offset; - - draw_line(v0.x, v0.y, v1.x, v1.y, 2f32, WHITE); - draw_line(v0.x, v0.y, v2.x, v2.y, 2f32, WHITE); - draw_line(v2.x, v2.y, v1.x, v1.y, 2f32, WHITE); - } -} diff --git a/crates/parry2d/examples/raycasts_animated.rs b/crates/parry2d/examples/raycasts_animated.rs index 09aa4bd4..76ed094f 100644 --- a/crates/parry2d/examples/raycasts_animated.rs +++ b/crates/parry2d/examples/raycasts_animated.rs @@ -1,3 +1,6 @@ +mod common_macroquad; + +use common_macroquad::draw_point; use macroquad::prelude::*; use nalgebra::{Isometry2, Point2, UnitComplex, Vector2}; use parry2d::math::Isometry; @@ -99,26 +102,6 @@ fn draw_polygon( } } -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, diff --git a/crates/parry3d/examples/common_macroquad.rs b/crates/parry3d/examples/common_macroquad.rs index 0bd37202..0e97a1c2 100644 --- a/crates/parry3d/examples/common_macroquad.rs +++ b/crates/parry3d/examples/common_macroquad.rs @@ -12,7 +12,7 @@ use parry3d::math::Real; fn main() { println!( - "This module contains helper fubnctions to use macroquad, + "This module contains helper functions to use macroquad, isolated from the rest of the examples for the sake of simplicity." ); } From f6c1b0ec578b9a1c3bec2ed44519e576ab85f344 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Fri, 13 Sep 2024 10:06:03 +0200 Subject: [PATCH 14/16] address PR feebacks --- crates/parry2d/examples/common_macroquad.rs | 13 ++++- crates/parry3d/examples/common_macroquad.rs | 3 ++ crates/parry3d/examples/project_point3d.rs | 9 +--- .../to_trimesh/cuboid_to_trimesh.rs | 50 ++++++++----------- 4 files changed, 36 insertions(+), 39 deletions(-) diff --git a/crates/parry2d/examples/common_macroquad.rs b/crates/parry2d/examples/common_macroquad.rs index 6326afe2..da74dd59 100644 --- a/crates/parry2d/examples/common_macroquad.rs +++ b/crates/parry2d/examples/common_macroquad.rs @@ -1,5 +1,4 @@ -#[allow(unused, dead_code)] -use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6}; +use std::f32::consts::{FRAC_PI_2, FRAC_PI_4}; use macroquad::prelude::*; use macroquad::{ @@ -11,6 +10,7 @@ use nalgebra::Point2; use parry2d::math::Real; use parry2d::shape::TriMesh; +#[allow(dead_code)] fn main() { println!( "This module contains helper functions to use macroquad, @@ -18,14 +18,17 @@ fn main() { ); } +#[allow(dead_code)] pub fn mquad_from_na(a: Point2) -> Vec2 { Vec2::new(a.x, a.y) } +#[allow(dead_code)] pub fn na_from_mquad(a: Vec2) -> Point2 { Point2::new(a.x, a.y) } +#[allow(dead_code)] pub fn draw_polyline(polygon: Vec<(Vec2, Vec2)>, color: Color) { for i in 0..polygon.len() { let a = polygon[i].0; @@ -34,10 +37,12 @@ pub fn draw_polyline(polygon: Vec<(Vec2, Vec2)>, color: Color) { } } +#[allow(dead_code)] pub fn easy_draw_text(text: &str) { macroquad::text::draw_text(text, 10.0, 48.0 + 18.0, 30.0, WHITE); } +#[allow(dead_code)] pub fn lissajous_2d(t: f32) -> Vec2 { // Some hardcoded parameters to have a pleasing lissajous trajectory. let (a, b, delta_x, delta_y) = (3.0, 2.0, FRAC_PI_2, FRAC_PI_4); @@ -47,10 +52,12 @@ pub fn lissajous_2d(t: f32) -> Vec2 { Vec2::new(x, y) * 0.75f32 } +#[allow(dead_code)] pub fn draw_line_2d(a: Vec2, b: Vec2, color: Color) { draw_line(a.x, a.y, b.x, b.y, 2f32, color); } +#[allow(dead_code)] pub fn draw_trimesh2(trimesh: &TriMesh, offset: Vec2) { let vertices = trimesh.vertices(); for v in trimesh.indices() { @@ -64,6 +71,7 @@ pub fn draw_trimesh2(trimesh: &TriMesh, offset: Vec2) { } } +#[allow(dead_code)] pub fn draw_polygon(polygon: &[Point2], scale: f32, shift: Point2, color: Color) { for i in 0..polygon.len() { let a = polygon[i]; @@ -79,6 +87,7 @@ pub fn draw_polygon(polygon: &[Point2], scale: f32, shift: Point2, col } } +#[allow(dead_code)] pub fn draw_point(point: Point2, scale: f32, shift: Point2, color: Color) { let edge_len = 0.15; draw_line( diff --git a/crates/parry3d/examples/common_macroquad.rs b/crates/parry3d/examples/common_macroquad.rs index 0e97a1c2..a554e789 100644 --- a/crates/parry3d/examples/common_macroquad.rs +++ b/crates/parry3d/examples/common_macroquad.rs @@ -10,6 +10,7 @@ use macroquad::{ use nalgebra::Point3; use parry3d::math::Real; +#[allow(dead_code)] fn main() { println!( "This module contains helper functions to use macroquad, @@ -35,6 +36,7 @@ pub fn lissajous_3d(t: f32) -> Vec3 { Vec3::new(x, y, z) * 0.75f32 } +#[allow(dead_code)] pub fn draw_polyline(polygon: Vec<(Vec3, Vec3)>, color: Color) { for i in 0..polygon.len() { let a = polygon[i].0; @@ -43,6 +45,7 @@ pub fn draw_polyline(polygon: Vec<(Vec3, Vec3)>, color: Color) { } } +#[allow(dead_code)] pub fn easy_draw_text(text: &str) { macroquad::text::draw_text(text, 10.0, 48.0 + 18.0, 30.0, WHITE); } diff --git a/crates/parry3d/examples/project_point3d.rs b/crates/parry3d/examples/project_point3d.rs index 408ac3fb..1cbec816 100644 --- a/crates/parry3d/examples/project_point3d.rs +++ b/crates/parry3d/examples/project_point3d.rs @@ -1,6 +1,5 @@ use macroquad::prelude::*; use nalgebra::Vector3; -use parry3d::math::Isometry; use parry3d::query::PointQuery; use parry3d::shape::{Cuboid, TriMesh, TriMeshFlags}; @@ -25,14 +24,10 @@ async fn main() { let slow_elapsed_time = elapsed_time / 3.0; let point_to_project = lissajous_3d(slow_elapsed_time); - let projected_point = trimesh.project_point( - &Isometry::identity(), - &na_from_mquad(point_to_project), - true, - ); + let projected_point = trimesh.project_local_point(&na_from_mquad(point_to_project), true); let slow_elapsed_time = slow_elapsed_time * 0.7; - // Going 3d! + // Setup 3D camera. set_camera(&Camera3D { position: Vec3::new( slow_elapsed_time.sin() * 3.0, diff --git a/src/transformation/to_trimesh/cuboid_to_trimesh.rs b/src/transformation/to_trimesh/cuboid_to_trimesh.rs index 3e1dcd35..30d2086b 100644 --- a/src/transformation/to_trimesh/cuboid_to_trimesh.rs +++ b/src/transformation/to_trimesh/cuboid_to_trimesh.rs @@ -1,21 +1,11 @@ use crate::bounding_volume::Aabb; -use crate::math::Real; +use crate::math::{Point, Real}; use crate::shape::Cuboid; use crate::transformation::utils; -#[cfg(feature = "dim3")] -use na::Point3 as PointDim; -#[cfg(feature = "dim3")] -use na::{self, Point3}; - -#[cfg(feature = "dim2")] -use na::Point2 as PointDim; -#[cfg(feature = "dim2")] -use na::{self, Point2}; - impl Aabb { /// Discretize the boundary of this Aabb as a triangle-mesh. - pub fn to_trimesh(&self) -> (Vec>, Vec<[u32; 3]>) { + pub fn to_trimesh(&self) -> (Vec>, Vec<[u32; 3]>) { let center = self.center(); let half_extents = self.half_extents(); let mut cube_mesh = Cuboid::new(half_extents).to_trimesh(); @@ -26,7 +16,7 @@ impl Aabb { impl Cuboid { /// Discretize the boundary of this cuboid as a triangle-mesh. - pub fn to_trimesh(&self) -> (Vec>, Vec<[u32; 3]>) { + pub fn to_trimesh(&self) -> (Vec>, Vec<[u32; 3]>) { let (vtx, idx) = unit_cuboid(); (utils::scaled(vtx, self.half_extents * 2.0), idx) } @@ -37,20 +27,20 @@ impl Cuboid { * * The cuboid is centered at the origin, and has its half extents set to 0.5. */ -fn unit_cuboid() -> (Vec>, Vec<[u32; 3]>) { +fn unit_cuboid() -> (Vec>, Vec<[u32; 3]>) { #[cfg(feature = "dim3")] - return { + { let mut coords = Vec::with_capacity(8); let mut faces = Vec::with_capacity(12); - coords.push(Point3::new(-0.5, -0.5, 0.5)); - coords.push(Point3::new(-0.5, -0.5, -0.5)); - coords.push(Point3::new(0.5, -0.5, -0.5)); - coords.push(Point3::new(0.5, -0.5, 0.5)); - coords.push(Point3::new(-0.5, 0.5, 0.5)); - coords.push(Point3::new(-0.5, 0.5, -0.5)); - coords.push(Point3::new(0.5, 0.5, -0.5)); - coords.push(Point3::new(0.5, 0.5, 0.5)); + coords.push(Point::new(-0.5, -0.5, 0.5)); + coords.push(Point::new(-0.5, -0.5, -0.5)); + coords.push(Point::new(0.5, -0.5, -0.5)); + coords.push(Point::new(0.5, -0.5, 0.5)); + coords.push(Point::new(-0.5, 0.5, 0.5)); + coords.push(Point::new(-0.5, 0.5, -0.5)); + coords.push(Point::new(0.5, 0.5, -0.5)); + coords.push(Point::new(0.5, 0.5, 0.5)); faces.push([4, 5, 0]); faces.push([5, 1, 0]); @@ -71,20 +61,20 @@ fn unit_cuboid() -> (Vec>, Vec<[u32; 3]>) { faces.push([4, 7, 5]); (coords, faces) - }; + } #[cfg(feature = "dim2")] - return { + { let mut coords = Vec::with_capacity(8); let mut faces = Vec::with_capacity(12); - coords.push(Point2::new(-0.5, -0.5)); - coords.push(Point2::new(0.5, -0.5)); - coords.push(Point2::new(-0.5, 0.5)); - coords.push(Point2::new(0.5, 0.5)); + coords.push(Point::new(-0.5, -0.5)); + coords.push(Point::new(0.5, -0.5)); + coords.push(Point::new(-0.5, 0.5)); + coords.push(Point::new(0.5, 0.5)); faces.push([0, 1, 2]); faces.push([2, 1, 3]); (coords, faces) - }; + } } From 48d99a4edc80afae2c6b5881eec42abcabfff0b0 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Fri, 13 Sep 2024 10:10:15 +0200 Subject: [PATCH 15/16] fix warnings --- .../examples/{common_macroquad.rs => common_macroquad2d.rs} | 0 crates/parry2d/examples/point_in_poly2d.rs | 4 ++-- crates/parry2d/examples/polygons_intersection2d.rs | 4 ++-- crates/parry2d/examples/project_point2d.rs | 4 ++-- crates/parry2d/examples/raycasts_animated.rs | 4 ++-- .../examples/{common_macroquad.rs => common_macroquad3d.rs} | 5 +++++ crates/parry3d/examples/plane_intersection.rs | 4 ++-- crates/parry3d/examples/project_point3d.rs | 4 ++-- 8 files changed, 17 insertions(+), 12 deletions(-) rename crates/parry2d/examples/{common_macroquad.rs => common_macroquad2d.rs} (100%) rename crates/parry3d/examples/{common_macroquad.rs => common_macroquad3d.rs} (97%) diff --git a/crates/parry2d/examples/common_macroquad.rs b/crates/parry2d/examples/common_macroquad2d.rs similarity index 100% rename from crates/parry2d/examples/common_macroquad.rs rename to crates/parry2d/examples/common_macroquad2d.rs diff --git a/crates/parry2d/examples/point_in_poly2d.rs b/crates/parry2d/examples/point_in_poly2d.rs index fd85e046..99f28770 100644 --- a/crates/parry2d/examples/point_in_poly2d.rs +++ b/crates/parry2d/examples/point_in_poly2d.rs @@ -1,6 +1,6 @@ -mod common_macroquad; +mod common_macroquad2d; -use common_macroquad::{draw_point, draw_polygon}; +use common_macroquad2d::{draw_point, draw_polygon}; use macroquad::prelude::*; use nalgebra::{Point2, UnitComplex, Vector2}; use parry2d::utils::point_in_poly2d; diff --git a/crates/parry2d/examples/polygons_intersection2d.rs b/crates/parry2d/examples/polygons_intersection2d.rs index 6c3164b3..604f86c5 100644 --- a/crates/parry2d/examples/polygons_intersection2d.rs +++ b/crates/parry2d/examples/polygons_intersection2d.rs @@ -1,6 +1,6 @@ -mod common_macroquad; +mod common_macroquad2d; -use common_macroquad::draw_polygon; +use common_macroquad2d::draw_polygon; use macroquad::prelude::*; use nalgebra::{Point2, UnitComplex, Vector2}; use parry2d::shape::Ball; diff --git a/crates/parry2d/examples/project_point2d.rs b/crates/parry2d/examples/project_point2d.rs index ca484eef..fc2a9884 100644 --- a/crates/parry2d/examples/project_point2d.rs +++ b/crates/parry2d/examples/project_point2d.rs @@ -1,6 +1,6 @@ -mod common_macroquad; +mod common_macroquad2d; -use common_macroquad::{draw_line_2d, draw_trimesh2, lissajous_2d, mquad_from_na, na_from_mquad}; +use common_macroquad2d::{draw_line_2d, draw_trimesh2, lissajous_2d, mquad_from_na, na_from_mquad}; use macroquad::prelude::*; use nalgebra::{Point3, UnitComplex, Vector2}; use parry2d::math::{Isometry, Translation}; diff --git a/crates/parry2d/examples/raycasts_animated.rs b/crates/parry2d/examples/raycasts_animated.rs index 76ed094f..ddd8fcb4 100644 --- a/crates/parry2d/examples/raycasts_animated.rs +++ b/crates/parry2d/examples/raycasts_animated.rs @@ -1,6 +1,6 @@ -mod common_macroquad; +mod common_macroquad2d; -use common_macroquad::draw_point; +use common_macroquad2d::draw_point; use macroquad::prelude::*; use nalgebra::{Isometry2, Point2, UnitComplex, Vector2}; use parry2d::math::Isometry; diff --git a/crates/parry3d/examples/common_macroquad.rs b/crates/parry3d/examples/common_macroquad3d.rs similarity index 97% rename from crates/parry3d/examples/common_macroquad.rs rename to crates/parry3d/examples/common_macroquad3d.rs index a554e789..45aeb50e 100644 --- a/crates/parry3d/examples/common_macroquad.rs +++ b/crates/parry3d/examples/common_macroquad3d.rs @@ -18,14 +18,17 @@ fn main() { ); } +#[allow(dead_code)] pub fn mquad_from_na(a: Point3) -> Vec3 { Vec3::new(a.x, a.y, a.z) } +#[allow(dead_code)] pub fn na_from_mquad(a: Vec3) -> Point3 { Point3::new(a.x, a.y, a.z) } +#[allow(dead_code)] pub fn lissajous_3d(t: f32) -> Vec3 { // Some hardcoded parameters to have a pleasing lissajous trajectory. let (a, b, c, delta_x, delta_y, delta_z) = (3.0, 2.0, 1.0, FRAC_PI_2, FRAC_PI_4, FRAC_PI_6); @@ -50,6 +53,7 @@ pub fn easy_draw_text(text: &str) { macroquad::text::draw_text(text, 10.0, 48.0 + 18.0, 30.0, WHITE); } +#[allow(dead_code)] pub fn mquad_mesh_from_points( trimesh: &(Vec>, Vec<[u32; 3]>), light_pos: Vec3, @@ -90,6 +94,7 @@ pub fn mquad_mesh_from_points( mesh } +#[allow(dead_code)] pub fn mquad_compute_normals_and_bake_light( points: &Vec, indices: &Vec, diff --git a/crates/parry3d/examples/plane_intersection.rs b/crates/parry3d/examples/plane_intersection.rs index 567d4dfc..47f45e69 100644 --- a/crates/parry3d/examples/plane_intersection.rs +++ b/crates/parry3d/examples/plane_intersection.rs @@ -3,8 +3,8 @@ use nalgebra::{UnitVector3, Vector3}; use parry3d::query::IntersectResult; use parry3d::shape::{Cuboid, TriMesh}; -mod common_macroquad; -use common_macroquad::*; +mod common_macroquad3d; +use common_macroquad3d::*; #[macroquad::main("parry3d::query::PlaneIntersection")] async fn main() { diff --git a/crates/parry3d/examples/project_point3d.rs b/crates/parry3d/examples/project_point3d.rs index 1cbec816..31e745a6 100644 --- a/crates/parry3d/examples/project_point3d.rs +++ b/crates/parry3d/examples/project_point3d.rs @@ -3,8 +3,8 @@ use nalgebra::Vector3; use parry3d::query::PointQuery; use parry3d::shape::{Cuboid, TriMesh, TriMeshFlags}; -mod common_macroquad; -use common_macroquad::*; +mod common_macroquad3d; +use common_macroquad3d::*; #[macroquad::main("parry3d::query::PlaneIntersection")] async fn main() { From 0e41c84b3318c8950171dd8f2dc49ce341246ff1 Mon Sep 17 00:00:00 2001 From: Thierry Berger Date: Fri, 13 Sep 2024 10:32:06 +0200 Subject: [PATCH 16/16] add changelog line for added features --- CHANGELOG.md | 4 ++++ src/transformation/to_trimesh/cuboid_to_trimesh.rs | 8 +++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 970a072f..bbe8fa0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Added + +- Implement `::to_trimesh` in 2d for `Cuboid` and `Aabb`. + ### Modified - Improve convergence of epa algorithm in degenerate configurations. diff --git a/src/transformation/to_trimesh/cuboid_to_trimesh.rs b/src/transformation/to_trimesh/cuboid_to_trimesh.rs index 30d2086b..5132cf17 100644 --- a/src/transformation/to_trimesh/cuboid_to_trimesh.rs +++ b/src/transformation/to_trimesh/cuboid_to_trimesh.rs @@ -22,11 +22,9 @@ impl Cuboid { } } -/** - * Generates a cuboid shape with a split index buffer. - * - * The cuboid is centered at the origin, and has its half extents set to 0.5. - */ +/// Generates a cuboid shape with a split index buffer. +/// +/// The cuboid is centered at the origin, and has its half extents set to 0.5. fn unit_cuboid() -> (Vec>, Vec<[u32; 3]>) { #[cfg(feature = "dim3")] {