Moving objects and internal force


Hello, I am trying to use nphysics to prototype my game engine and I cannot figure out how to make objects which move by their own. Imagine we have a rigid multibody (like human figure from the examples) and one of its subbodies is applying acceleration to all of the body (legs move, for example). Cannot find anything in the documentation, what is the correct way to do that?

I am reading about the motors, but they are joint feature, imagine we have a rocket that have an engine that creates thrust, this force is applied to the sub-body, not to the joint. How can I do it?

P.S. I see there is a apply_force method for RigidBody, but cannot find good examples how it could be used, any chance to have any?

P.P.S. Scratch it, now I have a real issue, I am trying to apply either force or velocity to rigid body and I am having a trouble, I am pretty new to Rust, I am sure I am doing something stupid, will appreciate some help.

The code (i am modifying example with balls):

        let &mut rigid_body = world.rigid_body(handle).unwrap().borrow_mut();
        rigid_body.set_velocity(Velocity2::new(Vector2::new(0.1 as f32 * i as f32, 0.1 as f32 * j as f32), 0.0));

I am getting an error: cannot borrow (rigid_body) as mutable. But the rigid_body is mutable borrow. I tried different approaches, but obviously I dont understand how borrow mutability works here. Can you suggest how should I do it?

P.P.P.S Pain! I found example with that code

    let platform_handle = world.add_rigid_body(pos, Inertia::zero(), Point2::origin());
    let rb = world.rigid_body_mut(platform_handle).unwrap();
    rb.set_velocity(Velocity::linear(1.0, 0.0));

I wish I have something like this somewhere in the doco. Will try this solution

P.P.P.S. Sorry for all of that. The approacj works, I didnt know the function rigid_body_mut, now I am looking into making objects move by themselves. I am thinking to provide Force that will change in time, wonder if there is some good example about that, it looks pretty common case in game engine. Wonder if I am solving it the wrong way, would be happy to hear anything regarding that.


Hi! Looks like you have made some good progress with your issue already! There are mainly two ways to control a rigid body:

  1. If your rigid body is kinematic (set by rb.set_status(BodyStatus::Kinematic);), then you have total control over its trajectory by setting its velocity as in the last example you cited. However, the kinematic body will not be affected by any extrenal force like gravity and contacts so you (or the users of your game engine) will have to add logic to your game to handle those.
  2. If your rigid body is dynamic (which is its default status), you can set its velocity with .set_velocity, or apply forces to it using .apply_force. The trick is that .apply_force will only work if called within a ForceGenerator as described there. Having an .apply_force that works outside of a force generator is planed but not yet implemented (see So to apply a force that change in time, you would have to create a struct that implements ForceGenerator and its .apply method would apply the time-dependent force to the relevant bodies. At the moment, you will also be responsible for tracking the amount of time elapsed since the beginning of the simulation, you may see for a discussion about this.

EDIT: I just realized you are the one you opened the last issue mentioned in my answer. Looks like you found the right way of doing this currently then!


Thank you for not letting me down :slight_smile: Yes, making some progress, things are working for me now. Great library, the only thing I could wish is it to have more detailed documentation, some things you have to figure out from reading the code. Do you have plans to extend the docs? I would be happy to send you some PRs if you point me to the right place to put the docs to. I am actually thinking about writing some kind of tutorial how to make simple predator/prey world with self-moving objects with nphysics.


There are plans to extend the docs but we are short on manpower right now. As far as I am concerned I currently focus on adding doc-tests to all nalgebra methods, and will then do the same for ncollide (and only then for nphysics).

That would be great! The right place depends on what kind of doc you which to contribute. You may either add doc-tests to methods on nphysics, or add more wordy documentation on the nphysics user guide ( Improving the user guide can be done by sending PRs to the repository. If you do, don’t worry to much about formatting.

That would be even better than extending the existing documentation! If you do end up writing this tutorial, we could link it from the nphysics website if you want. Tutorials are great ways of helping people getting on board.


Hi! Just so you know, using .apply_force outside of a force generator will now work. This is available in nphysics v0.9.3.