nphysics icon indicating copy to clipboard operation
nphysics copied to clipboard

RigdBodyDesc can't be stored

Open AndreaCatania opened this issue 5 years ago • 8 comments

The RigidBodyDesc accept a reference to the colliders https://github.com/rustsim/nphysics/blob/master/src/object/rigid_body.rs#L727

The problem is that due to the lifetime used there is not possible to store the RigidBodyDesc in the heap and move it around.

Since the RigidBody only exist inside the world, and I need to manipulate its parameters even out of the world, would be nice to be able to store them directly inside the descriptor.

AndreaCatania avatar Jun 23 '19 09:06 AndreaCatania

Hi! I'm currently attempting to allow the user to manage itself the set of rigid bodies (outside of the physics world),. That should solve your problem here.

sebcrozet avatar Jun 23 '19 10:06 sebcrozet

Do you mean make the RigidBody available even if not assigned to the world yet?

In this case it will improve a lot the integration that I'm doing

AndreaCatania avatar Jun 23 '19 10:06 AndreaCatania

Basically the user will responsible for managing the set of bodies. A reference to this set will have to be passed to every methods like world.step because the physics world will no longer own the set of bodies.

sebcrozet avatar Jun 23 '19 10:06 sebcrozet

This seems nice but instead to pass the whole array per each call, what do you think about insert an array of weak pointers inside the world?

So the user can handle the bodies as he prefer. For example he could even store the bodies not in an Array, or he could even use a custom Array (like me)

AndreaCatania avatar Jun 23 '19 10:06 AndreaCatania

By set of bodies I did not mean an Array. specifically I meant anything (including user-defined collections) that would be able to provide references to bodies. So it would basically be anything that implements a trait like:

trait BodySet<'a, N: RealField> {
    type Body: ?Sized + Body<N>;
    type Handle: Copy;
    type Iter: Iterator<Item = (Self::Handle, &'a Self::Body)>;
    type IterMut: Iterator<Item = (Self::Handle, &'a mut Self::Body)>;

    fn get(&self, handle: Self::Handle) -> Option<&Self::Body>;
    fn get_mut(&mut self, handle: Self::Handle) -> Option<&mut Self::Body>;

    fn contains(&self, handle: Self::Handle) -> bool;

    fn iter(&'a self) -> Self::Iter;
    fn iter_mut(&'a mut self) -> Self::IterMut;
}

This trait is what I am experimenting with. The World stepping method would look like:

fn step<'a, N: RealField, Bodies: BodySet<'a, N>() {
  ....
}

Having the world store a weak pointer isn't desirable as it would require the user to rely on some form of shared pointers like Arc or Rc, which is not desirable.

sebcrozet avatar Jun 23 '19 11:06 sebcrozet

This is perfect for what I'm doing! Do you already have a PR / branch that I can keep an eye on it?

Because get rid of the builder will make my life much easier.

AndreaCatania avatar Jun 23 '19 11:06 AndreaCatania

Not yet, it's still very experimental. It will likely end up in the ccd branch within a couple of weeks if it ends up working well.

sebcrozet avatar Jun 23 '19 11:06 sebcrozet

Ok thanks!

AndreaCatania avatar Jun 23 '19 14:06 AndreaCatania