Skip to content

Commit

Permalink
simpler project example
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Jul 31, 2024
1 parent 44c81cc commit 9526e75
Showing 1 changed file with 18 additions and 48 deletions.
66 changes: 18 additions & 48 deletions crates/parry3d/examples/project3d_animated.rs
Original file line number Diff line number Diff line change
@@ -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<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)
}
use parry3d::query::PointQuery;
use parry3d::shape::{Cuboid, TriMesh, TriMeshFlags};

#[macroquad::main("parry3d::query::PlaneIntersection")]
async fn main() {
Expand All @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -126,28 +104,20 @@ 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);

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)
}
Expand Down

0 comments on commit 9526e75

Please sign in to comment.