variable frame rate and body.ApplyForce

Topics: Developer Forum, Project Management Forum, User Forum
Dec 19, 2011 at 7:31 AM

Hi boys,

my name is Fabio and I make a shooter with farseer.

I have two problem.

One. My game run with variable frame rate and work nice with a range between 90 - 120 fps but if for a moment the fps low around 60-70 frame my bullets from normal speed change a slow speed. this is the istruction for bullet:

Body.ApplyForce(Direction * 200000);

Two. When I create a bullet I want a bullet very speed. if I apply force 20.000 or 200.000 I dont see any change and the speed is normal.

 

Thanks



        
    
Dec 21, 2011 at 3:24 PM

To have fps independent physics you should multiply all your force/velocity updates with the time the last frame took. This you can get like this
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

so in your function where you do this Body.ApplyForce(Direction * 200000); you pass in "elapsed"
and instead do this Body.ApplyForce(Direction * 200000 * elapsed);

But since the value in elapsed will be somewhere around 1/100 = 0.01 you need to tweak all your constants to match this value.

Dec 21, 2011 at 3:49 PM

thank you, your solution work fine, but if I want apply this solution I need force applyForce each update.

before I set applyForce one time.

I make a test for performance....