Create ForceGenerator and add to ForceGeneratorSet

I want to create a rigid body that accelerates to the left when the left key is pressed. I created a ConstantAcceleration to the left and now I want to add it to the DefaultForceGeneratorSet force_generators. This is my code:

 let mut left_acc = ConstantAcceleration::new(Vector2::new(-1.0, 0.0), 0.0);
 left_acc.add_body_part(BodyPartHandle(self.rigid_body_handle, 0));

Rust gives me the following error on the last line:

error[E0277]: the trait bound nphysics2d::force_generator::constant_acceleration::ConstantAcceleration<{float}, std::option::Option<generational_arena::Index>>: nphysics2d::force_generator::force_generator::ForceGenerator<f32, generational_arena::Index> is not satisfied
Can someone explain how to solve this error?


I think the problem here is that the compiler was not able to infer that the {float} in left_acc is f32. So I believe writing this should work:

 let mut left_acc = ConstantAcceleration::new(Vector2::new(-1.0f32, 0.0), 0.0);

By the way, using a force generator for this may not be the simplest way of handling this kind of inputs. Force generators are more designed to affect multiple bodies at once, not just one. An easiest approach is to apply forces on this body at each frame (depending on user inputs) using body.apply_force (see that documentation).

Yes that fixed it. Thank you very much