-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example for projection (#250)
- Loading branch information
Showing
13 changed files
with
494 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.