Skip to content

Commit

Permalink
Merge branch 'master' into convex_hull_example
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Sep 13, 2024
2 parents 39d7f8e + 217a567 commit 1027b09
Show file tree
Hide file tree
Showing 19 changed files with 243 additions and 76 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

## Unreleased

### Added

- Implement `::to_trimesh` in 2d for `Cuboid` and `Aabb`.
- Implement `Shape::feature_normal_at_point` for `TriMesh` to retrieve the normal of a face, when passing a `FeatureId::Face`.

## v0.17.1

### Modified

- Improve convergence of epa algorithm in degenerate configurations.
- Fix bug in the mesh/mesh intersection algorithm that didn’t properly take mesh transforms into account.

## v0.17.0

Expand Down
2 changes: 1 addition & 1 deletion crates/parry2d-f64/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parry2d-f64"
version = "0.17.0"
version = "0.17.1"
authors = ["Sébastien Crozet <[email protected]>"]

description = "2 dimensional collision detection library in Rust. 64-bit precision version."
Expand Down
2 changes: 1 addition & 1 deletion crates/parry2d/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parry2d"
version = "0.17.0"
version = "0.17.1"
authors = ["Sébastien Crozet <[email protected]>"]

description = "2 dimensional collision detection library in Rust."
Expand Down
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -11,21 +10,25 @@ 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,
isolated from the rest of the examples for the sake of simplicity."
);
}

#[allow(dead_code)]
pub fn mquad_from_na(a: Point2<Real>) -> Vec2 {
Vec2::new(a.x, a.y)
}

#[allow(dead_code)]
pub fn na_from_mquad(a: Vec2) -> Point2<Real> {
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;
Expand All @@ -34,14 +37,18 @@ 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.
lissajous_2d_with_params(t, 3.0, 2.0, FRAC_PI_2, FRAC_PI_4)
}

#[allow(dead_code)]
pub fn lissajous_2d_with_params(t: f32, a: f32, b: f32, delta_x: f32, delta_y: f32) -> Vec2 {
// Some hardcoded parameters to have a pleasing lissajous trajectory.

Expand All @@ -50,10 +57,12 @@ pub fn lissajous_2d_with_params(t: f32, a: f32, b: f32, delta_x: f32, delta_y: f
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() {
Expand All @@ -67,6 +76,7 @@ pub fn draw_trimesh2(trimesh: &TriMesh, offset: Vec2) {
}
}

#[allow(dead_code)]
pub fn draw_polygon(polygon: &[Point2<f32>], scale: f32, shift: Point2<f32>, color: Color) {
for i in 0..polygon.len() {
let a = polygon[i];
Expand All @@ -82,6 +92,7 @@ pub fn draw_polygon(polygon: &[Point2<f32>], scale: f32, shift: Point2<f32>, col
}
}

#[allow(dead_code)]
pub fn draw_point(point: Point2<f32>, scale: f32, shift: Point2<f32>, color: Color) {
let edge_len = 0.15;
draw_line(
Expand Down
4 changes: 2 additions & 2 deletions crates/parry2d/examples/convex_hull2d.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod common_macroquad;
mod common_macroquad2d;

use std::f32::consts::{FRAC_PI_2, FRAC_PI_4};

use common_macroquad::{draw_point, draw_polygon, lissajous_2d_with_params, na_from_mquad};
use common_macroquad2d::{draw_point, draw_polygon, lissajous_2d_with_params, na_from_mquad};
use macroquad::prelude::*;
use nalgebra::Point2;
use parry2d::transformation;
Expand Down
5 changes: 2 additions & 3 deletions crates/parry2d/examples/point_in_poly2d.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod common_macroquad;

use common_macroquad::{draw_point, draw_polygon};
mod common_macroquad2d;
use common_macroquad2d::{draw_point, draw_polygon};
use macroquad::prelude::*;
use nalgebra::{Point2, UnitComplex, Vector2};
use parry2d::utils::point_in_poly2d;
Expand Down
5 changes: 2 additions & 3 deletions crates/parry2d/examples/polygons_intersection2d.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod common_macroquad;

use common_macroquad::draw_polygon;
mod common_macroquad2d;
use common_macroquad2d::draw_polygon;
use macroquad::prelude::*;
use nalgebra::{Point2, UnitComplex, Vector2};
use parry2d::shape::Ball;
Expand Down
5 changes: 2 additions & 3 deletions crates/parry2d/examples/project_point2d.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod common_macroquad;

use common_macroquad::{draw_line_2d, draw_trimesh2, lissajous_2d, mquad_from_na, na_from_mquad};
mod common_macroquad2d;
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};
Expand Down
5 changes: 2 additions & 3 deletions crates/parry2d/examples/raycasts_animated.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod common_macroquad;

use common_macroquad::draw_point;
mod common_macroquad2d;
use common_macroquad2d::draw_point;
use macroquad::prelude::*;
use nalgebra::{Isometry2, Point2, UnitComplex, Vector2};
use parry2d::math::Isometry;
Expand Down
2 changes: 1 addition & 1 deletion crates/parry3d-f64/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parry3d-f64"
version = "0.17.0"
version = "0.17.1"
authors = ["Sébastien Crozet <[email protected]>"]

description = "3 dimensional collision detection library in Rust. 64-bits precision version."
Expand Down
2 changes: 1 addition & 1 deletion crates/parry3d/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parry3d"
version = "0.17.0"
version = "0.17.1"
authors = ["Sébastien Crozet <[email protected]>"]

description = "3 dimensional collision detection library in Rust."
Expand Down
138 changes: 138 additions & 0 deletions crates/parry3d/examples/common_macroquad3d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6};

use macroquad::{
color::{Color, WHITE},
math::{Vec2, Vec3, Vec4},
models::{draw_line_3d, Mesh},
ui::Vertex,
};
use nalgebra::Point3;
use parry3d::math::Real;

#[allow(dead_code)]
fn main() {
println!(
"This module contains helper functions to use macroquad,
isolated from the rest of the examples for the sake of simplicity."
);
}

#[allow(dead_code)]
pub fn mquad_from_na(a: Point3<Real>) -> Vec3 {
Vec3::new(a.x, a.y, a.z)
}

#[allow(dead_code)]
pub fn na_from_mquad(a: Vec3) -> Point3<Real> {
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.
lissajous_3d_with_params(t, 3.0, 2.0, 1.0, FRAC_PI_2, FRAC_PI_4, FRAC_PI_6)
}

#[allow(dead_code)]
pub fn lissajous_3d_with_params(
t: f32,
a: f32,
b: f32,
c: f32,
delta_x: f32,
delta_y: f32,
delta_z: f32,
) -> Vec3 {
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
}

#[allow(dead_code)]
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);
}
}

#[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 mquad_mesh_from_points(
trimesh: &(Vec<Point3<Real>>, 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<Vertex> =
mquad_compute_normals_and_bake_light(&mquad_points, &mquad_indices, light_pos);
// Regenerate the index for each vertex.
let indices: Vec<u16> = (0..vertices.len() * 3)
.into_iter()
.map(|i| i as u16)
.collect();
let mesh = Mesh {
vertices,
indices,
texture: None,
};
mesh
}

#[allow(dead_code)]
pub fn mquad_compute_normals_and_bake_light(
points: &Vec<Vertex>,
indices: &Vec<u16>,
light_pos: Vec3,
) -> Vec<Vertex> {
let mut vertices: Vec<Vertex> = Vec::<Vertex>::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.4 + (0.6 / 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
}
4 changes: 2 additions & 2 deletions crates/parry3d/examples/convex_hull3d.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod common_macroquad;
mod common_macroquad3d;

use std::f32::consts::{FRAC_PI_2, FRAC_PI_4, FRAC_PI_6};

use common_macroquad::{
use common_macroquad3d::{
lissajous_3d_with_params, mquad_from_na, mquad_mesh_from_points, na_from_mquad,
};
use macroquad::prelude::*;
Expand Down
4 changes: 2 additions & 2 deletions crates/parry3d/examples/plane_intersection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
13 changes: 4 additions & 9 deletions crates/parry3d/examples/project_point3d.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use macroquad::prelude::*;
use nalgebra::Vector3;
use parry3d::math::Isometry;
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() {
Expand All @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions src/shape/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,18 @@ impl Shape for TriMesh {
Real::frac_pi_4()
}

/// Gets the normal of the triangle represented by `feature`.
fn feature_normal_at_point(
&self,
_feature: FeatureId,
_point: &Point<Real>,
) -> Option<Unit<Vector<Real>>> {
#[cfg(feature = "dim2")]
return None;
#[cfg(feature = "dim3")]
return self.feature_normal(_feature);
}

#[cfg(feature = "std")]
fn as_composite_shape(&self) -> Option<&dyn SimdCompositeShape> {
Some(self as &dyn SimdCompositeShape)
Expand Down
Loading

0 comments on commit 1027b09

Please sign in to comment.