Doing applyForce - the right way

After discussion with normen in a previous thread we came to this conclusion :

If you want to really "control" your snowboard you will have to check the current forces with getLinearVelocity and getAngularVelocity and give correct counter forces on your snowboard each tick. So if your torque around the x-axis is too high, you apply a counter-force. In this case using addForce() will work since you do it every tick.

So this is how my game currently works:

press "a"

press "d"

Now - it seems I should be calling applyForce every game tick - whereas at the moment it only gets called once per keypress - what is the best way to do this? Should I have something in the main game loop which applys a force to each model, and that force to be applied gets modified depending on the current forces and the addition of new forces because a or d has been pressed?

Please offer some wisdom!

Hey! Your problem is pretty commonly encountered when setting up the controls, because the keypress method doesn't work like you're expecting it to. I made a thread that explains how to have a boolean that you can set using keypresses, and then read to apply the action every tick:

So basically, you need to do the following (Note that the way to check if a key is pressed is slightly more complicated!):

"a" event
set boolean turningLeft to apressed

in Update method:
if apressed

Hope this helps!

Don't forget that you can use AnalogListener to receive events every frame, and do the applyForce() there.

Actually, saving the values like kidneytrader suggested but in a threadsafe manner and then applying them in simpleUpdatePhysics() would be the "proper" and multithreading-compatible way for the physics implementation right now. This way they only get applied on every physics tick (which is 60fps by default).

Right… so i do the apply force in the main game loops simpleUpdate() method ?

I think this will require some refactoring of how I am currently handling movement - because I had all of this stuff abstracted away from the main game loop and in the event handling. It seems odd to set a force variable somewhere else - and then check it inside the main game loop!

Well i have made the appropriate changes and finally I have the behaviour i want from the physics! Thank you so very much kidney trader - you can have my kidney anyday!

I want to apply an aerodynamic downforce to a vehicle that increases with the speed of the vehicle.

What’s the best way to do this in JMonkeyEngine ?