Add convex polygons support.

This commit is contained in:
Crozet Sébastien
2020-12-27 18:14:22 +01:00
parent 27ebb541f8
commit 8d925a02ef
8 changed files with 208 additions and 21 deletions

View File

@@ -174,6 +174,14 @@ impl Box2dWorld {
b2_shape.set_radius(b.radius);
b2_shape.set_position(center);
body.create_fixture(&b2_shape, &mut fixture_def);
} else if let Some(p) = shape.as_convex_polygon() {
let vertices: Vec<_> = p
.points()
.iter()
.map(|p| na_vec_to_b2_vec(p.coords))
.collect();
let b2_shape = b2::PolygonShape::new_with(&vertices);
body.create_fixture(&b2_shape, &mut fixture_def);
} else if let Some(c) = shape.as_cuboid() {
let b2_shape = b2::PolygonShape::new_box(c.half_extents.x, c.half_extents.y);
body.create_fixture(&b2_shape, &mut fixture_def);

View File

@@ -344,6 +344,15 @@ impl GraphicsManager {
)))
}
#[cfg(feature = "dim2")]
if let Some(convex_polygon) = shape
.as_convex_polygon()
.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)))
}
#[cfg(feature = "dim3")]
if let Some(convex_polyhedron) = shape
.as_convex_polyhedron()

View File

@@ -1,3 +1,5 @@
#[cfg(feature = "dim2")]
use ncollide::shape::ConvexPolygon;
use ncollide::shape::{Ball, Capsule, Cuboid, HeightField, ShapeHandle};
use nphysics::force_generator::DefaultForceGeneratorSet;
use nphysics::joint::{
@@ -144,6 +146,7 @@ impl NPhysicsWorld {
self.mechanical_world
.integration_parameters
.set_dt(params.dt());
self.mechanical_world.integration_parameters.warmstart_coeff = params.warmstart_coeff;
counters.step_started();
self.mechanical_world.step(
@@ -175,12 +178,15 @@ fn nphysics_collider_from_rapier_collider(
collider: &Collider,
is_dynamic: bool,
) -> Option<ColliderDesc<f32>> {
let margin = ColliderDesc::<f32>::default_margin();
let mut margin = ColliderDesc::<f32>::default_margin();
let mut pos = *collider.position_wrt_parent();
let shape = collider.shape();
let shape = if let Some(cuboid) = shape.as_cuboid() {
ShapeHandle::new(Cuboid::new(cuboid.half_extents.map(|e| e - margin)))
} else if let Some(cuboid) = shape.as_round_cuboid() {
margin = cuboid.border_radius;
ShapeHandle::new(Cuboid::new(cuboid.base_shape.half_extents))
} else if let Some(ball) = shape.as_ball() {
ShapeHandle::new(Ball::new(ball.radius - margin))
} else if let Some(capsule) = shape.as_capsule() {
@@ -208,7 +214,12 @@ fn nphysics_collider_from_rapier_collider(
}
#[cfg(feature = "dim2")]
{
if let Some(polygon) = shape.as_round_convex_polygon() {
margin = polygon.border_radius;
ShapeHandle::new(ConvexPolygon::try_from_points(polygon.base_shape.points()).unwrap())
} else if let Some(polygon) = shape.as_convex_polygon() {
ShapeHandle::new(ConvexPolygon::try_from_points(polygon.points()).unwrap())
} else {
return None;
}
};
@@ -219,6 +230,7 @@ fn nphysics_collider_from_rapier_collider(
ColliderDesc::new(shape)
.position(pos)
.density(density)
.sensor(collider.is_sensor()),
.sensor(collider.is_sensor())
.margin(margin),
)
}

View File

@@ -10,7 +10,6 @@ use rapier::math::Point;
#[cfg(feature = "dim3")]
use rapier::math::Vector;
use std::cell::RefCell;
use std::rc::Rc;
pub struct HeightField {
color: Point3<f32>,
@@ -52,6 +51,8 @@ impl HeightField {
color: Point3<f32>,
window: &mut Window,
) -> HeightField {
use std::rc::Rc;
let (vertices, indices) = heightfield.to_trimesh();
let indices = indices.into_iter().map(|i| na::convert(i)).collect();
let mesh = Mesh::new(vertices, indices, None, None, false);