Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an example for intersection plane #249

Merged
merged 8 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions crates/parry3d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -76,3 +88,4 @@ obj = { version = "0.10.2", optional = true }
oorandom = "11"
ptree = "0.4.0"
rand = { version = "0.8" }
macroquad = "0.4"
141 changes: 141 additions & 0 deletions crates/parry3d/examples/plane_intersection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
use macroquad::models::Vertex;
use macroquad::prelude::*;
use nalgebra::{Point3, UnitVector3, Vector3};
use parry3d::math::{Isometry, Real};
use parry3d::query::IntersectResult;
use parry3d::shape::TriMesh;

fn build_diamond(position: &Isometry<Real>) -> (Vec<Point3<Real>>, 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: WHITE,
})
.collect(),
indices: indices.iter().flatten().map(|v| *v as u16).collect(),
texture: None,
};
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved
let trimesh = TriMesh::new(points, indices);

for _i in 1.. {
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved
clear_background(BLACK);

let elapsed_time = get_time();

let bias = -2.0 * (elapsed_time as f32 / 3f32).sin();
let rotation = Quat::from_axis_angle(Vec3::Z, (elapsed_time as f32 * 50f32).to_radians());
let up_plane_vector = rotation * Vec3::Y;
let intersection_result = trimesh.intersection_with_local_plane(
&UnitVector3::new_normalize(Vector3::<Real>::new(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the type parameter annotation really necessary?

Suggested change
&UnitVector3::new_normalize(Vector3::<Real>::new(
&UnitVector3::new_normalize(Vector3::new(

up_plane_vector.x,
up_plane_vector.y,
up_plane_vector.z,
)),
bias,
0.0005,
);

// Going 3d!
Vrixyz marked this conversation as resolved.
Show resolved Hide resolved
set_camera(&Camera3D {
position: Vec3::new(0f32, 3f32, -3f32),
up: Vec3::new(0f32, 1f32, 0f32),
target: Vec3::new(0.5f32, 0f32, 0.5f32),
..Default::default()
});

let plane_center = up_plane_vector * bias;
draw_line_3d(plane_center, plane_center + up_plane_vector, GREEN);
draw_mesh(&mesh);
draw_grid_ex(10, 0.333, BLUE, RED, plane_center, rotation);

/*
*
* Render the intersection
*
*/
match intersection_result {
IntersectResult::Intersect(points) => {
draw_polyline(
points
.segments()
.map(|s| (mquad_from_na(s.a), mquad_from_na(s.b)))
.collect(),
Color::new(0f32, 1f32, 0f32, 1f32),
);
}
IntersectResult::Negative => {
set_default_camera();
draw_text(
format!("No intersection found, the shape is below the plane.").as_str(),
10.0,
48.0 + 18.0,
30.0,
WHITE,
);
}
IntersectResult::Positive => {
set_default_camera();
draw_text(
format!("No intersection found, the shape is above the plane.").as_str(),
10.0,
48.0 + 18.0,
30.0,
WHITE,
);
}
}
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<Real>) -> Vec3 {
Vec3::new(a.x, a.y, a.z)
}