Allow using polylines as a collider shape.

This commit is contained in:
Crozet Sébastien
2021-01-26 16:41:21 +01:00
parent e1f50eb6e8
commit 23a86c294e
10 changed files with 294 additions and 53 deletions

View File

@@ -1,38 +1,32 @@
use kiss3d::window::Window;
use na::{Isometry2, Point2, Point3};
use nphysics2d::object::{ColliderAnchor, DefaultColliderHandle, DefaultColliderSet};
use parry2d::shape;
use na::Point3;
use rapier::geometry::{ColliderHandle, ColliderSet};
use rapier::math::{Isometry, Point};
pub struct Polyline {
color: Point3<f32>,
base_color: Point3<f32>,
vertices: Vec<Point2<f32>>,
indices: Vec<Point2<usize>>,
collider: DefaultColliderHandle,
pos: Isometry2<f32>,
vertices: Vec<Point<f32>>,
indices: Vec<[u32; 2]>,
collider: ColliderHandle,
pos: Isometry<f32>,
}
impl Polyline {
pub fn new(
collider: DefaultColliderHandle,
colliders: &DefaultColliderSet<f32>,
_: Isometry2<f32>,
vertices: Vec<Point2<f32>>,
indices: Vec<Point2<usize>>,
collider: ColliderHandle,
vertices: Vec<Point<f32>>,
indices: Vec<[u32; 2]>,
color: Point3<f32>,
_: &mut Window,
) -> Polyline {
let mut res = Polyline {
Polyline {
color,
pos: Isometry2::identity(),
pos: Isometry::identity(),
base_color: color,
vertices,
indices,
collider,
};
res.update(colliders);
res
}
}
pub fn select(&mut self) {
@@ -48,32 +42,26 @@ impl Polyline {
self.base_color = color;
}
pub fn update(&mut self, colliders: &DefaultColliderSet<f32>) {
// Update if some deformation occurred.
// FIXME: don't update if it did not move.
if let Some(c) = colliders.get(self.collider) {
self.pos = *c.position();
if let ColliderAnchor::OnDeformableBody { .. } = c.anchor() {
let shape = c.shape().as_shape::<shape::Polyline<f32>>().unwrap();
self.vertices = shape.points().to_vec();
self.indices.clear();
for e in shape.edges() {
self.indices.push(e.indices);
}
}
}
pub fn update(&mut self, colliders: &ColliderSet) {
self.pos = colliders
.get(self.collider)
.map(|c| *c.position())
.unwrap_or(Isometry::identity());
}
pub fn object(&self) -> DefaultColliderHandle {
pub fn object(&self) -> ColliderHandle {
self.collider
}
pub fn draw(&mut self, window: &mut Window) {
for idx in &self.indices {
let p1 = self.pos * self.vertices[idx.x];
let p2 = self.pos * self.vertices[idx.y];
window.draw_planar_line(&p1, &p2, &self.color)
let p1 = self.pos * self.vertices[idx[0] as usize];
let p2 = self.pos * self.vertices[idx[1] as usize];
#[cfg(feature = "dim2")]
window.draw_planar_line(&p1, &p2, &self.color);
#[cfg(feature = "dim3")]
window.draw_line(&p1, &p2, &self.color);
}
}
}