I can't apply force to a RigidBodyControl

Hi.



I’m trying to apply a force to my RigidBodyControl element.



[java]Node node = new Node();

RigidBodyControl auxRBC = new RigidBodyControl(0);

node.addControl(auxRBC);

node.getControl(RigidBodyControl.class).applyCentralForce(Vector3f.UNIT_X);[/java]



My node has a spatial. I didn’t put it here because the code would be too large.

This doesn’t work. Nothing happens.

I’m new on JME, so maybe my problem is very easy. =p

Does anyone know why this is not working?



Thanks!

You have to apply forces on the physics tick.

Thanks normen.

I’ll try this.

Have you tried buying it dinner? Maybe a good zombie flick before applying force? Oh… and if the RigidBodyControl yells “NO MEANS NO!”… just walk away. Pretty much, at this point… you’ll wanna try a FarFromRigidBodyForFiveAmericanDollarControl.

You made a rigid body with mass 0. Seems applying a force to it would cause weird results (or none at all)?

1 Like
@androlo said:
You made a rigid body with mass 0. Seems applying a force to it would cause weird results (or none at all)?

That too :)
@androlo said:
You made a rigid body with mass 0. Seems applying a force to it would cause weird results (or none at all)?


Thanks for the replys.
I did this because when I use a mass = 1, for example, the object keeps bouncing, and rolling. So I wanted to apply the force to an object that wouldn't move randomly. Now I tried this:
[java]auxRBC.setMass(.5f);
auxRBC.setRestitution(0);[/java]
In this case, the object barely moves. And the bouncing continues.=p

Please read the manual, kinematic mode is what you want.

@carlafcf said:
Thanks for the replys.
I did this because when I use a mass = 1, for example, the object keeps bouncing, and rolling. So I wanted to apply the force to an object that wouldn't move randomly. Now I tried this:
[java]auxRBC.setMass(.5f);
auxRBC.setRestitution(0);[/java]
In this case, the object barely moves. And the bouncing continues.=p



you can also set linear damping, angular damping, friction for some cases...

Could you make it specific? How to use apply force on physics tick?

Which part don’t you understand exactly? Adding a physics tick listener or apply forces? (Both are covered in the tutorials I think… you just have to put them together.)

This is what I have done;
public class RollingBall extends SimpleApplication implements PhysicsTickListener{

public TestSimplePhysics() {
    this.actionListener = new ActionListener() {
        public void onAction(String name, boolean keyPressed, float tpf) {
            if (name.equals("down") && !keyPressed) {
                prePhysicsTick();
            }      
        }
    };
}

public void prePhysicsTick(PhysicsSpace space, float tpf) {
Ball.getControl(RigidBodyControl.class).applyCentralForce(new Vector3f(2,0,0));
}

I was trying to make it that when I press Space the rigid ball would get a force to roll. But it doesnt work.

It seems you are very new to java development as your code barely makes sense. I highly recommend getting more experienced with Java development before attempting advanced topics like game development.

As it is, you have your application that is also a PhysicsTickListener but you never seem to actually register that listener. Instead, you do the hugely bad wrong-so-very-wrong-it-hurts mistake of calling the listener method yourself.

You probably won’t take my advice to get more experienced with Java. Prediction: you will then just give up in frustration soon.

If not, I suggest going through the tutorials as they cover all of this stuff in one way or another. And it’s pretty clear you’ve skipped them.

Thank you for your suggestions! After reading tutorial carefully I knew why I was wrong!

getPhysicsSpace().addTickListener(this);

1 Like