nphysics
nphysics copied to clipboard
impl Volumetric for Shape doesn't allow for your own shapes
I was implementing an Ellipsoid Shape, but when I got to the Volumetric trait, I realized that it is hardcoded for the specific Shapes that ship with nphysics:
macro_rules! dispatch(
($p: ty, $i: ty, $sself: ident.$name: ident($($argN: ident),*)) => {
{
if let Some(b) = $sself.as_shape::<Ball<N>>() {
return b.$name($($argN,)*)
}
if let Some(c) = $sself.as_shape::<Compound<N>>() {
return c.$name($($argN,)*)
}
// else if let Some(c) = $sself.as_shape::<Cone<N>>() {
// (c as &Volumetric<N, $p, $i>).$name($($argN,)*)
// }
#[cfg(feature = "dim3")]
{
if let Some(c) = $sself.as_shape::<ConvexHull<N>>() {
return c.$name($($argN,)*)
}
}
#[cfg(feature = "dim2")]
{
if let Some(c) = $sself.as_shape::<ConvexPolygon<N>>() {
return c.$name($($argN,)*)
}
}
if let Some(c) = $sself.as_shape::<Cuboid<N>>() {
return c.$name($($argN,)*)
}
// if let Some(c) = $sself.as_shape::<Cylinder<N>>() {
// return c.$name($($argN,)*)
// }
/*
* XXX: dispatch by custom type.
*/
panic!("The `Volumetric` is not implemented by the given shape.")
}
}
);
We need a way to implement Volumetric for crate-provided Shape structs.
I agree this is not extensible enough and should be improved!
Though keep in mind the Volumetric
trait is not strictly required by nphysics: all you need is a way to retrieve the center of mass and inertia from your shape if you want to use them for the creation of a rigid body or multibody link.
One solution for this would be to:
- Move the
Volumetric
trait to ncollide. - Add to the
Shape
trait the methodfn as_volumetric(&self) -> Option<&Volumetric> {}
. - Move to ncollide the relevant implementations of
Volumetric
and adapt the corresponding implementation ofShape
so it includes:fn as_volumetric(&self) -> Option<&Volumetric> { Some(self as &Volumetric) }
.