Spawning rigidbodys and move them constantly

Hello, i spent a lot of time searching the forums and documentation.

I want to build an can shooter. The Cans are spawning and move along a line and i shoot with balls at them. If there was no hit, the just fall off. For Physics I use RigidBodyControl.

    Cylinder canCylinder = new Cylinder(100,100,0.08f,0.3f,true,false);;
    Geometry geomCan = new Geometry("can", canCylinder);
    geomCan.setLocalTranslation(loc);
    Material matShooter = new Material(assetManager,"Common/MatDefs/Light/Lighting.j3md");
    matShooter.setBoolean("UseMaterialColors",false); //  Configure some parameters
    matShooter.setColor("Ambient", ColorRGBA.Gray); //  Color of the object
    matShooter.setColor("Diffuse", ColorRGBA.Gray); // Color of reflected light
    geomCan.setMaterial(matShooter); 
    geomCan.rotate(FastMath.PI / 2, FastMath.PI / 2 , 0);
    rootNode.attachChild(geomCan);
    RigidBodyControl can_phy = new RigidBodyControl(1f);        
    geomCan.addControl(can_phy);
    bulletAppState.getPhysicsSpace().add(can_phy); 

I know i have to spawn the cans and move them in the simpleUpdate() method.
Regarding movement, there existed once a method named applyConstantForce() ?

Thanks

If you shoot another RigidBodyControl at them (e.g. the Ball), then the phyiscs are handled for you.
You also might not want a constant force but rather apply a force for some time (which would involve accelerating), or even better apply an impulse, where p = m * v.

I did not understand the fall off part very well but if it is like a side scroller then just make sure you move the “ground”, so they fall down when they are no longer on the ground.

You should however remove them properly afterwards due to performance reasons

You don’t want to use applyConstantForce(). The problem with this approach is that the force is constant - it will not go away when the ball hits it and the can will move according to constant force + force of impact once it is hit. The result will be some erratic behavior that doesn’t look at all convincing. I think that you could achieve what you want by enabling kinematics on the control (see the Javadocs for RigidBodyControl’s setKinematic() and setKinematicSpatial()), steadily moving the can spatial until the can is hit by the ball (use a physics listener for this), and then allowing regular physics to take over.

Edit: What @Darkchaos said. :wink:

I guess applyConstantForce is called setGravity in native bullet.

The biggest problem with your scene is that it isn’t physically possible. In the real world somebody would push the cans in the beginning and they would move until too much energy is lost due to friction. Then you have to possibilites push as hard so that they will have enough energy to pass the whole scene or you keep pushing all the time. The first route will probably not yield to the wanted behavior because the cans loose speed. Now you have two solutions: You can cheat by setting the frition to zero or you take the second route and keep pushing until it was hit by a ball. This is imaginable like a car beneath the can which constantly drives your can foreward until the wheels loose connection to the ground.

I would go the first route: set friction to zero and raise it after collision because then the whole movement looks linear. Maybe a method like set linearVeolcity comes in handy in the beginning. Constantly pushing involves some slowdown and speedup phases of the cans and it has to be fine tuned with the friction.

Setting the cans to kinematic is also possible but then the ball wouldn’t bounce of in a realsitc way from the cans. Also the collision is a bit more complicated to handle because you have to set the can to dynamic and then apply the impuls of the ball. But how do you know which impuls the ball has and were exactly it hit the can?

Just my thoughts.