Welcome, Guest. Please login or register.
Did you miss your activation email?
September 08, 2010, 02:24:59 am
advanced search




Pages: [1]
  Print  
Author Topic: Doing applyForce - the right way  (Read 171 times)
renegadeandy
Full Member
***
Offline Offline

Posts: 292


View Profile
« on: July 25, 2010, 09:08:28 am »

After discussion with normen in a previous thread we came to this conclusion :
Quote
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"
  applyForce(-10,0,0);

press "d"
  applyForce(10,0,0);

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!
Logged
kidneytrader
Newbie
*
Offline Offline

Posts: 39


View Profile
« Reply #1 on: July 25, 2010, 11:41:56 am »

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:

http://www.jmonkeyengine.com/forum/index.php?topic=14318.0

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

Code:
"a" event
set boolean turningLeft to apressed

in Update method:
if apressed
applyForce(-10,0,0);

Hope this helps!
Logged
Momoko_Fan
Developer
Hero Member
*
Offline Offline

Posts: 2722



View Profile
« Reply #2 on: July 25, 2010, 11:24:14 pm »

Don't forget that you can use AnalogListener to receive events every frame, and do the applyForce() there.
Logged
normen
Moderator
Hero Member
*
Online Online

Posts: 1238


Use the source, Luke!


View Profile WWW
« Reply #3 on: July 25, 2010, 11:49:34 pm »

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).
Logged

Nothing lasts forever but the certainty of change.. Get jMonkeyEngine3 SDK Alpha2!
renegadeandy
Full Member
***
Offline Offline

Posts: 292


View Profile
« Reply #4 on: July 26, 2010, 12:06:42 am »

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!
Logged
renegadeandy
Full Member
***
Offline Offline

Posts: 292


View Profile
« Reply #5 on: July 26, 2010, 12:49:05 am »

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!
Logged
Tags:
Pages: [1]
  Print  
 
Jump to: