Skip to content

Commit

Permalink
common macroquad code in their example
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Aug 14, 2024
1 parent 065f6b6 commit 174f012
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 51 deletions.
46 changes: 46 additions & 0 deletions crates/parry3d/examples/common_macroquad.rs
Original file line number Diff line number Diff line change
@@ -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<Real>) -> Vec3 {
Vec3::new(a.x, a.y, a.z)
}

pub fn na_from_mquad(a: Vec3) -> Point3<Real> {
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);
}
28 changes: 8 additions & 20 deletions crates/parry3d/examples/plane_intersection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -95,7 +98,8 @@ fn mquad_mesh_from_points(trimesh: &(Vec<Point3<Real>>, 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
Expand Down Expand Up @@ -140,19 +144,3 @@ fn mquad_compute_normals(points: &Vec<Vertex>, indices: &Vec<u16>, 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<Real>) -> 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);
}
35 changes: 4 additions & 31 deletions crates/parry3d/examples/project_point3d.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -116,11 +97,3 @@ async fn main() {
next_frame().await
}
}

fn mquad_from_na(a: Point3<Real>) -> Vec3 {
Vec3::new(a.x, a.y, a.z)
}

fn na_from_mquad(a: Vec3) -> Point3<Real> {
Point3::new(a.x, a.y, a.z)
}

0 comments on commit 174f012

Please sign in to comment.