Add compound shape support.

This commit is contained in:
Crozet Sébastien
2021-01-05 15:34:48 +01:00
parent 6ba5081358
commit 00da4aaa42
10 changed files with 123 additions and 50 deletions

View File

@@ -6,13 +6,13 @@ use kiss3d::window::Window;
use na::Point3;
use crate::math::Point;
use crate::math::{Isometry, Point};
use crate::objects::ball::Ball;
use crate::objects::box_node::Box as BoxNode;
use crate::objects::heightfield::HeightField;
use crate::objects::node::{GraphicsNode, Node};
use rapier::dynamics::{RigidBodyHandle, RigidBodySet};
use rapier::geometry::{Collider, ColliderHandle, ColliderSet};
use rapier::geometry::{Collider, ColliderHandle, ColliderSet, Shape};
//use crate::objects::capsule::Capsule;
use crate::objects::convex::Convex;
//#[cfg(feature = "dim3")]
@@ -237,7 +237,14 @@ impl GraphicsManager {
for collider_handle in bodies[handle].colliders() {
let color = self.c2color.get(collider_handle).copied().unwrap_or(color);
let collider = &colliders[*collider_handle];
self.do_add_collider(window, *collider_handle, collider, color, &mut new_nodes);
self.do_add_shape(
window,
*collider_handle,
collider.shape(),
&Isometry::identity(),
color,
&mut new_nodes,
);
}
new_nodes.iter_mut().for_each(|n| n.update(colliders));
@@ -267,30 +274,41 @@ impl GraphicsManager {
let color = self.c2color.get(&handle).copied().unwrap_or(color);
let mut nodes =
std::mem::replace(self.b2sn.get_mut(&collider.parent()).unwrap(), Vec::new());
self.do_add_collider(window, handle, collider, color, &mut nodes);
self.do_add_shape(
window,
handle,
collider.shape(),
&Isometry::identity(),
color,
&mut nodes,
);
self.b2sn.insert(collider.parent(), nodes);
}
fn do_add_collider(
fn do_add_shape(
&mut self,
window: &mut Window,
handle: ColliderHandle,
collider: &Collider,
shape: &dyn Shape,
delta: &Isometry<f32>,
color: Point3<f32>,
out: &mut Vec<Node>,
) {
let shape = collider.shape();
if let Some(ball) = shape.as_ball() {
out.push(Node::Ball(Ball::new(handle, ball.radius, color, window)))
if let Some(compound) = shape.as_compound() {
for (shape_pos, shape) in compound.shapes() {
self.do_add_shape(window, handle, &**shape, shape_pos, color, out)
}
}
// Shape::Polygon(poly) => out.push(Node::Convex(Convex::new(
// handle,
// poly.vertices().to_vec(),
// color,
// window,
// ))),
if let Some(ball) = shape.as_ball() {
out.push(Node::Ball(Ball::new(
handle,
*delta,
ball.radius,
color,
window,
)))
}
if let Some(cuboid) = shape
.as_cuboid()
@@ -298,6 +316,7 @@ impl GraphicsManager {
{
out.push(Node::Box(BoxNode::new(
handle,
*delta,
cuboid.half_extents,
color,
window,
@@ -305,7 +324,9 @@ impl GraphicsManager {
}
if let Some(capsule) = shape.as_capsule() {
out.push(Node::Capsule(Capsule::new(handle, capsule, color, window)))
out.push(Node::Capsule(Capsule::new(
handle, *delta, capsule, color, window,
)))
}
if let Some(triangle) = shape
@@ -350,7 +371,9 @@ impl GraphicsManager {
.or(shape.as_round_convex_polygon().map(|r| &r.base_shape))
{
let vertices = convex_polygon.points().to_vec();
out.push(Node::Convex(Convex::new(handle, vertices, color, window)))
out.push(Node::Convex(Convex::new(
handle, *delta, vertices, color, window,
)))
}
#[cfg(feature = "dim3")]
@@ -360,7 +383,7 @@ impl GraphicsManager {
{
let (vertices, indices) = convex_polyhedron.to_trimesh();
out.push(Node::Convex(Convex::new(
handle, vertices, indices, color, window,
handle, *delta, vertices, indices, color, window,
)))
}
@@ -371,6 +394,7 @@ impl GraphicsManager {
{
out.push(Node::Cylinder(Cylinder::new(
handle,
*delta,
cylinder.half_height,
cylinder.radius,
color,
@@ -385,6 +409,7 @@ impl GraphicsManager {
{
out.push(Node::Cone(Cone::new(
handle,
*delta,
cone.half_height,
cone.radius,
color,

View File

@@ -1,6 +1,6 @@
use crate::objects::node::{self, GraphicsNode};
use kiss3d::window::Window;
use na::Point3;
use na::{Isometry3, Point3};
use rapier::geometry::{ColliderHandle, ColliderSet};
use rapier::math::Isometry;
@@ -9,11 +9,13 @@ pub struct Ball {
base_color: Point3<f32>,
gfx: GraphicsNode,
collider: ColliderHandle,
delta: Isometry3<f32>,
}
impl Ball {
pub fn new(
collider: ColliderHandle,
delta: Isometry3<f32>,
radius: f32,
color: Point3<f32>,
window: &mut Window,
@@ -28,6 +30,7 @@ impl Ball {
base_color: color,
gfx: node,
collider,
delta,
};
// res.gfx.set_texture_from_file(&Path::new("media/kitten.png"), "kitten");
@@ -55,7 +58,7 @@ impl Ball {
colliders,
self.collider,
&self.color,
&Isometry::identity(),
&self.delta,
);
}

View File

@@ -1,6 +1,6 @@
use crate::objects::node::{self, GraphicsNode};
use kiss3d::window;
use na::Point3;
use na::{Isometry3, Point3};
use rapier::geometry::{ColliderHandle, ColliderSet};
use rapier::math::{Isometry, Vector};
@@ -9,11 +9,13 @@ pub struct Box {
base_color: Point3<f32>,
gfx: GraphicsNode,
collider: ColliderHandle,
delta: Isometry3<f32>,
}
impl Box {
pub fn new(
collider: ColliderHandle,
delta: Isometry3<f32>,
half_extents: Vector<f32>,
color: Point3<f32>,
window: &mut window::Window,
@@ -29,6 +31,7 @@ impl Box {
base_color: color,
gfx: node,
collider,
delta,
};
res.gfx.set_color(color.x, color.y, color.z);
@@ -55,7 +58,7 @@ impl Box {
colliders,
self.collider,
&self.color,
&Isometry::identity(),
&self.delta,
);
}

View File

@@ -1,6 +1,6 @@
use crate::objects::node::{self, GraphicsNode};
use kiss3d::window;
use na::Point3;
use na::{Isometry3, Point3};
use rapier::geometry::{self, ColliderHandle, ColliderSet};
use rapier::math::Isometry;
@@ -9,11 +9,13 @@ pub struct Capsule {
base_color: Point3<f32>,
gfx: GraphicsNode,
collider: ColliderHandle,
delta: Isometry3<f32>,
}
impl Capsule {
pub fn new(
collider: ColliderHandle,
delta: Isometry3<f32>,
capsule: &geometry::Capsule,
color: Point3<f32>,
window: &mut window::Window,
@@ -30,6 +32,7 @@ impl Capsule {
base_color: color,
gfx: node,
collider,
delta,
};
res.gfx.set_color(color.x, color.y, color.z);
@@ -50,7 +53,7 @@ impl Capsule {
colliders,
self.collider,
&self.color,
&Isometry::identity(),
&self.delta,
);
}

View File

@@ -1,6 +1,6 @@
use crate::objects::node::{self, GraphicsNode};
use kiss3d::window::Window;
use na::Point3;
use na::{Isometry3, Point3};
use rapier::geometry::{ColliderHandle, ColliderSet};
use rapier::math::Isometry;
@@ -9,11 +9,13 @@ pub struct Cone {
base_color: Point3<f32>,
gfx: GraphicsNode,
collider: ColliderHandle,
delta: Isometry3<f32>,
}
impl Cone {
pub fn new(
collider: ColliderHandle,
delta: Isometry3<f32>,
half_height: f32,
radius: f32,
color: Point3<f32>,
@@ -29,6 +31,7 @@ impl Cone {
base_color: color,
gfx: node,
collider,
delta,
};
// res.gfx.set_texture_from_file(&Path::new("media/kitten.png"), "kitten");
@@ -56,7 +59,7 @@ impl Cone {
colliders,
self.collider,
&self.color,
&Isometry::identity(),
&self.delta,
);
}

View File

@@ -5,7 +5,7 @@ use crate::math::Vector;
use crate::math::{Isometry, Point};
use crate::objects::node::{self, GraphicsNode};
use kiss3d::window::Window;
use na::Point3;
use na::{Isometry3, Point3};
use rapier::geometry::{ColliderHandle, ColliderSet};
pub struct Convex {
@@ -13,11 +13,13 @@ pub struct Convex {
base_color: Point3<f32>,
gfx: GraphicsNode,
body: ColliderHandle,
delta: Isometry3<f32>,
}
impl Convex {
pub fn new(
body: ColliderHandle,
delta: Isometry3<f32>,
vertices: Vec<Point<f32>>,
#[cfg(feature = "dim3")] indices: Vec<Point<u32>>,
color: Point3<f32>,
@@ -39,6 +41,7 @@ impl Convex {
base_color: color,
gfx: node,
body,
delta,
};
// res.gfx.set_texture_from_file(&Path::new("media/kitten.png"), "kitten");
@@ -66,7 +69,7 @@ impl Convex {
colliders,
self.body,
&self.color,
&Isometry::identity(),
&self.delta,
);
}

View File

@@ -1,6 +1,6 @@
use crate::objects::node::{self, GraphicsNode};
use kiss3d::window::Window;
use na::Point3;
use na::{Isometry3, Point3};
use rapier::geometry::{ColliderHandle, ColliderSet};
use rapier::math::Isometry;
@@ -9,11 +9,13 @@ pub struct Cylinder {
base_color: Point3<f32>,
gfx: GraphicsNode,
collider: ColliderHandle,
delta: Isometry3<f32>,
}
impl Cylinder {
pub fn new(
collider: ColliderHandle,
delta: Isometry3<f32>,
half_height: f32,
radius: f32,
color: Point3<f32>,
@@ -29,6 +31,7 @@ impl Cylinder {
base_color: color,
gfx: node,
collider,
delta,
};
// res.gfx.set_texture_from_file(&Path::new("media/kitten.png"), "kitten");
@@ -56,7 +59,7 @@ impl Cylinder {
colliders,
self.collider,
&self.color,
&Isometry::identity(),
&self.delta,
);
}

View File

@@ -17,7 +17,7 @@ use kiss3d::planar_camera::PlanarCamera;
use kiss3d::post_processing::PostProcessingEffect;
use kiss3d::text::Font;
use kiss3d::window::{State, Window};
use na::{self, Point2, Point3, Vector3};
use na::{self, Isometry3, Point2, Point3, Vector3};
use rapier::dynamics::{
ActivationStatus, IntegrationParameters, JointSet, RigidBodyHandle, RigidBodySet,
};
@@ -1525,9 +1525,13 @@ fn draw_contacts(window: &mut Window, nf: &NarrowPhase, colliders: &ColliderSet)
};
let pos1 = colliders[pair.pair.collider1].position();
let pos2 = colliders[pair.pair.collider2].position();
let start = pos1 * pt.local_p1;
let end = pos2 * pt.local_p2;
let n = pos1 * manifold.local_n1;
let start =
pos1 * manifold.subshape_pos1.unwrap_or(Isometry3::identity()) * pt.local_p1;
let end =
pos2 * manifold.subshape_pos2.unwrap_or(Isometry3::identity()) * pt.local_p2;
let n = pos1
* manifold.subshape_pos1.unwrap_or(Isometry3::identity())
* manifold.local_n1;
use crate::engine::GraphicsWindow;
window.draw_graphics_line(&start, &(start + n * 0.4), &Point3::new(0.5, 1.0, 0.5));