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

Parry testbed exploration #281

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["crates/parry2d", "crates/parry3d", "crates/parry2d-f64", "crates/parry3d-f64"]
members = ["crates/parry2d", "crates/parry3d", "crates/parry2d-f64", "crates/parry3d-f64", "crates/common_macroquad"]
resolver = "2"

[workspace.lints]
Expand Down
17 changes: 17 additions & 0 deletions crates/common_macroquad/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "common_macroquad"
version = "0.1.0"
edition = "2021"

[features]
"dim2" = ["dep:parry2d"]
"dim3" = ["dep:parry3d"]

[dependencies]
macroquad = "0.4.12"
nalgebra = "*"
parry3d = { optional = true, version = "*", path = "../parry3d"}
parry2d = { optional = true, version = "*", path = "../parry2d"}

[lints]
workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,34 @@ use nalgebra::Point2;
use parry2d::math::Real;
use parry2d::shape::TriMesh;

/// As this file is used as a module from other examples,
/// rustc warns about dead code:
/// - `main()` is needed for this file to be included in examples
/// - For other functions, they may be "dead code" for an example, but not for others.
#[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)]
/// Converts a [`nalgebra::Point2`] to a [`Vec2`], which is used by [`macroquad`]
pub fn mquad_from_na(a: Point2<Real>) -> Vec2 {
Vec2::new(a.x, a.y)
}

#[allow(dead_code)]
/// Converts a [`Vec2`] to a [`nalgebra::Point2`], which is used by [`parry3d`]
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;
/// Uses [`macroquad`] to display the line passed as parameter.
pub fn draw_polyline(polyline: Vec<(Vec2, Vec2)>, color: Color) {
for line in polyline {
let a = line.0;
let b = line.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)]
/// Returns [lissajous curve](https://en.wikipedia.org/wiki/Lissajous_curve) coordinates for time `t`.
///
/// This uses hardcoded parameters to have an arbitrary pleasing trajectory.
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)]
/// Returns [lissajous curve](https://en.wikipedia.org/wiki/Lissajous_curve) coordinates.
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 @@ -61,12 +46,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)]
/// Uses [`macroquad`] to display the line passed as parameter.
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)]
/// Uses [`macroquad`] to display the line passed as parameter.
pub fn draw_trimesh2(trimesh: &TriMesh, offset: Vec2) {
let vertices = trimesh.vertices();
for v in trimesh.indices() {
Expand All @@ -80,7 +65,7 @@ pub fn draw_trimesh2(trimesh: &TriMesh, offset: Vec2) {
}
}

#[allow(dead_code)]
/// Uses [`macroquad`] to display a wireframe of the polygon.
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 @@ -96,7 +81,7 @@ pub fn draw_polygon(polygon: &[Point2<f32>], scale: f32, shift: Point2<f32>, col
}
}

#[allow(dead_code)]
/// Uses [`macroquad`] to display the a cross, representing a point.
pub fn draw_point(point: Point2<f32>, scale: f32, shift: Point2<f32>, color: Color) {
let edge_len = 0.15;
draw_line(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,26 @@ use macroquad::{
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)]
/// Converts a [`nalgebra::Point3`] to a [`Vec3`], which is used by [`macroquad`]
pub fn mquad_from_na(a: Point3<Real>) -> Vec3 {
Vec3::new(a.x, a.y, a.z)
}

#[allow(dead_code)]
/// Converts a [`Vec3`] to a [`nalgebra::Point3`], which is used by [`parry3d`]
pub fn na_from_mquad(a: Vec3) -> Point3<Real> {
Point3::new(a.x, a.y, a.z)
}

#[allow(dead_code)]
/// Returns [lissajous curve](https://en.wikipedia.org/wiki/Lissajous_curve) coordinates for time `t`.
///
/// This uses hardcoded parameters to have an arbitrary pleasing trajectory.

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)]
/// Returns [lissajous curve](https://en.wikipedia.org/wiki/Lissajous_curve) coordinates.
pub fn lissajous_3d_with_params(
t: f32,
a: f32,
Expand All @@ -49,21 +44,26 @@ pub fn lissajous_3d_with_params(
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;
/// Uses [`macroquad`] to display the line passed as parameter.
pub fn draw_polyline(polyline: Vec<(Vec3, Vec3)>, color: Color) {
for line in polyline {
let a = line.0;
let b = line.1;
draw_line_3d(a, b, color);
}
}

#[allow(dead_code)]
/// Draws a text in the top left corner of the screen.
///
/// This uses a hardcoded position, size, color.
pub fn easy_draw_text(text: &str) {
macroquad::text::draw_text(text, 10.0, 48.0 + 18.0, 30.0, WHITE);
}

#[allow(dead_code)]
/// Create a usable mesh for [`macroquad`].
///
/// This duplicates the trimesh vertices, computes their normals,
/// and bakes light into its vertices colors using [`mquad_compute_normals_and_bake_light`].
pub fn mquad_mesh_from_points(
trimesh: &(Vec<Point3<Real>>, Vec<[u32; 3]>),
light_pos: Vec3,
Expand Down Expand Up @@ -104,7 +104,7 @@ pub fn mquad_mesh_from_points(
mesh
}

#[allow(dead_code)]
/// Bakes light into vertices, using an hardcoded light strength.
pub fn mquad_compute_normals_and_bake_light(
points: &Vec<Vertex>,
indices: &Vec<u16>,
Expand Down
13 changes: 13 additions & 0 deletions crates/common_macroquad/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[cfg(feature = "dim2")]
pub mod dim2;
#[cfg(feature = "dim3")]
pub mod dim3;

use macroquad::color::WHITE;

/// Draws a text in the top left corner of the screen.
///
/// This uses a hardcoded position, size, color.
pub fn easy_draw_text(text: &str) {
macroquad::text::draw_text(text, 10.0, 48.0 + 18.0, 30.0, WHITE);
}
3 changes: 3 additions & 0 deletions crates/parry2d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ oorandom = "11"
ptree = "0.4.0"
rand = { version = "0.8" }
macroquad = "0.4.12"
common_macroquad = { version = "0.1", path = "../common_macroquad", features = [
"dim2",
] }

[package.metadata.docs.rs]
rustdoc-args = ["-Zunstable-options", "--generate-link-to-definition"]
Expand Down
4 changes: 1 addition & 3 deletions crates/parry2d/examples/aabb2d.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mod common_macroquad2d;

extern crate nalgebra as na;

use common_macroquad2d::{draw_polyline, lissajous_2d, mquad_from_na, na_from_mquad};
use common_macroquad::dim2::{draw_polyline, lissajous_2d, mquad_from_na, na_from_mquad};
use macroquad::prelude::*;
use na::Isometry2;
use parry2d::bounding_volume::{Aabb, BoundingVolume};
Expand Down
4 changes: 1 addition & 3 deletions crates/parry2d/examples/bounding_sphere2d.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mod common_macroquad2d;

extern crate nalgebra as na;

use common_macroquad2d::{draw_polyline, lissajous_2d, mquad_from_na, na_from_mquad};
use common_macroquad::dim2::{draw_polyline, lissajous_2d, mquad_from_na, na_from_mquad};
use macroquad::prelude::*;
use na::{Isometry2, Vector2};
use parry2d::bounding_volume::{Aabb, BoundingVolume};
Expand Down
7 changes: 2 additions & 5 deletions crates/parry2d/examples/convex_hull2d.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
mod common_macroquad2d;

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

use common_macroquad2d::{draw_point, draw_polygon, lissajous_2d_with_params, na_from_mquad};
use common_macroquad::dim2::{draw_point, draw_polygon, lissajous_2d_with_params, na_from_mquad};
use macroquad::prelude::*;
use nalgebra::Point2;
use parry2d::transformation;
use std::f32::consts::{FRAC_PI_2, FRAC_PI_4};

const RENDER_SCALE: f32 = 30.0;

Expand Down
4 changes: 1 addition & 3 deletions crates/parry2d/examples/point_in_poly2d.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
mod common_macroquad2d;

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

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

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

use common_macroquad2d::draw_point;
use common_macroquad::dim2::draw_point;
use macroquad::prelude::*;
use nalgebra::{Isometry2, Point2, UnitComplex, Vector2};
use parry2d::math::Isometry;
Expand Down
4 changes: 4 additions & 0 deletions crates/parry3d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ rand = { version = "0.8" }
macroquad = "0.4.12"
nalgebra = { version = "0.33", default-features = false, features = ["rand"] }
rand_isaac = "0.3"
common_macroquad = { version = "0.1", path = "../common_macroquad", features = [
"dim3",
] }
fork = "*"

[package.metadata.docs.rs]
rustdoc-args = ["-Zunstable-options", "--generate-link-to-definition"]
Expand Down
4 changes: 1 addition & 3 deletions crates/parry3d/examples/aabb3d.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mod common_macroquad3d;

extern crate nalgebra as na;

use common_macroquad3d::{lissajous_3d, mquad_from_na, na_from_mquad};
use common_macroquad::dim3::{lissajous_3d, mquad_from_na, na_from_mquad};
use macroquad::prelude::*;
use na::Isometry3;
use parry3d::bounding_volume::{Aabb, BoundingVolume};
Expand Down
4 changes: 1 addition & 3 deletions crates/parry3d/examples/bounding_sphere3d.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
mod common_macroquad3d;

extern crate nalgebra as na;

use std::ops::Rem;

use common_macroquad3d::{lissajous_3d, mquad_from_na, na_from_mquad};
use common_macroquad::dim3::{lissajous_3d, mquad_from_na, na_from_mquad};
use macroquad::prelude::*;
use na::{Isometry3, Vector3};
use parry3d::bounding_volume::BoundingVolume;
Expand Down
4 changes: 1 addition & 3 deletions crates/parry3d/examples/convex_hull3d.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mod common_macroquad3d;

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

use common_macroquad3d::{
use common_macroquad::dim3::{
lissajous_3d_with_params, mquad_from_na, mquad_mesh_from_points, na_from_mquad,
};
use macroquad::prelude::*;
Expand Down
5 changes: 2 additions & 3 deletions crates/parry3d/examples/plane_intersection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use nalgebra::{UnitVector3, Vector3};
use parry3d::query::IntersectResult;
use parry3d::shape::{Cuboid, TriMesh};

mod common_macroquad3d;
use common_macroquad3d::*;
use common_macroquad::dim3::*;

#[macroquad::main("parry3d::query::PlaneIntersection")]
async fn main() {
pub async fn main() {
let trimesh = Cuboid::new(Vector3::repeat(1.0)).to_trimesh();

let light_pos = Vec3::new(-1f32, 3.5f32, -3f32);
Expand Down
3 changes: 1 addition & 2 deletions crates/parry3d/examples/project_point3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use nalgebra::Vector3;
use parry3d::query::PointQuery;
use parry3d::shape::{Cuboid, TriMesh, TriMeshFlags};

mod common_macroquad3d;
use common_macroquad3d::*;
use common_macroquad::dim3::*;

#[macroquad::main("parry3d::query::PlaneIntersection")]
async fn main() {
Expand Down
24 changes: 24 additions & 0 deletions crates/parry3d/examples/testbed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
mod plane_intersection;
use fork::{fork, Fork};

fn main() {
// NOTE: we can't use threads via macroquad: it supports only 1 window ; so I'm forking it.
// This will be problematic to support wasm...
// ...but! maybe not entirely difficult to make them target a specific canvas
// This would still need a custom communication layer between both windows/canvas,
// which would be different between web and native
match fork() {
Ok(Fork::Parent(child)) => {
plane_intersection::main();
println!(
"Continuing execution in parent process, new child has pid: {}",
child
);
}
Ok(Fork::Child) => {
plane_intersection::main();
println!("I'm a new child process")
}
Err(_) => println!("Fork failed"),
}
}
Loading