Skip to content

Commit

Permalink
Add an example for projection (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz authored Sep 13, 2024
1 parent 0928147 commit 217a567
Show file tree
Hide file tree
Showing 13 changed files with 494 additions and 193 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

### Added

- `TriMesh` now implements `Shape::feature_normal_at_point` to retrieve the normal of a face, when passing a `FeatureId::Face`.
- 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

Expand Down
109 changes: 109 additions & 0 deletions crates/parry2d/examples/common_macroquad2d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use std::f32::consts::{FRAC_PI_2, FRAC_PI_4};

use macroquad::prelude::*;
use macroquad::{
color::{Color, WHITE},
math::Vec2,
shapes::draw_line,
};
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;
let b = polygon[i].1;
draw_line_2d(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 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
}

#[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() {
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);
}
}

#[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];
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,
);
}
}

#[allow(dead_code)]
pub fn draw_point(point: Point2<f32>, scale: f32, shift: Point2<f32>, 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,
);
}
44 changes: 6 additions & 38 deletions crates/parry2d/examples/point_in_poly2d.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
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 All @@ -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);
Expand Down Expand Up @@ -71,38 +74,3 @@ fn grid_points() -> Vec<Point2<f32>> {
}
pts
}

fn draw_polygon(polygon: &[Point2<f32>], scale: f32, shift: Point2<f32>, 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<f32>, scale: f32, shift: Point2<f32>, 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,
);
}
18 changes: 3 additions & 15 deletions crates/parry2d/examples/polygons_intersection2d.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
mod common_macroquad2d;

use common_macroquad2d::draw_polygon;
use macroquad::prelude::*;
use nalgebra::{Point2, UnitComplex, Vector2};
use parry2d::shape::Ball;
Expand Down Expand Up @@ -111,18 +114,3 @@ fn spikes_polygon() -> Vec<Point2<f32>> {

polygon
}

fn draw_polygon(polygon: &[Point2<f32>], scale: f32, shift: Point2<f32>, 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,
);
}
}
89 changes: 89 additions & 0 deletions crates/parry2d/examples/project_point2d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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};
use parry2d::query::PointQuery;
use parry2d::shape::{Cuboid, TriMesh, TriMeshFlags};

#[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,
);

/*
*
* 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 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 {
RED
} else {
YELLOW
};
// 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) * scale + offset,
color,
);
// Mesh is rendered in the back, so we can see the other graphics elements
draw_trimesh2(&trimesh, offset);

next_frame().await
}
}
23 changes: 3 additions & 20 deletions crates/parry2d/examples/raycasts_animated.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
mod common_macroquad2d;

use common_macroquad2d::draw_point;
use macroquad::prelude::*;
use nalgebra::{Isometry2, Point2, UnitComplex, Vector2};
use parry2d::math::Isometry;
Expand Down Expand Up @@ -99,26 +102,6 @@ fn draw_polygon(
}
}

fn draw_point(point: Point2<f32>, scale: f32, shift: Point2<f32>, 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<f32>,
to: Point2<f32>,
Expand Down
5 changes: 5 additions & 0 deletions crates/parry3d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading

0 comments on commit 217a567

Please sign in to comment.