Physics based controls and delta time frustration

0 favourites
  • 13 posts
From the Asset Store
Supports keyboard, mouse and gamepads. Supports several gamepads at once.
  • Physics based controls and delta time frustration...

    I have a physics object, which is affected by gravity. I accellerate it using a physics impulse triggered by pressing a button.

    Now I have the very frustrating problem, that accelleration is varying extremely on different frame rates (different devices) even though I switched the physics behaviour to "framerate independent"

    Same goes for rotation(when applying torque to the object)

    My theory on this problem is, that the button press triggers the impulse every tick... So when running at 60FPS I have 60 impulses added to my object per second and on 30FPS I have 30 impulses added to my object. I tried to fix this using the dt-expression on the value for my accelleration ( acc * dt ) adding the same amount of thrust over time... but it didn't work. Accelleration still varies extremely...

    So what can I do to fix this?

  • If you use

    Button: "On clicked" => Sprite : "Apply Physics Impulse towards position"

    it will only trigger once per click.

  • I've tried a bunch of different stuff to make the physics movement behave similarly with different frame rates but it's pretty awkward

    a lower frame rate means the resulting velocity from apply force/impulse is much bigger, so you jump higher for example

    something I experimented with and ended up keeping was making instance variables on each physics object, 'forceX' and 'forceY' and instead of 'apply force..' i use 'add to forceX'/add to forceY instead. and at the very end of the events sheet, it sets the velocity X to it's current velocity X plus the forceX value.. and then sets the forceX variable back to 0 for the next tick. Same for ForceY

    now I dont know for sure whats going on under the hood, but i guess this means it's not doing a bunch of force iterations each tick, just one. The effect this has is that a lower frame rate means you jump slightly lower instead of higher (the difference is lessened too). Which is arguably better.. just try your best to make the game run at a consistent 60 fps. and maybe let the player know that 60 fps is really how it should be played.

  • If you use

    Button: "On clicked" => Sprite : "Apply Physics Impulse towards position"

    it will only trigger once per click.

    Thanks, but this one won't help in my case, because I don't need a single impulse. I want the sum of the impulses applied over time to be the same on any framerate.

  • keepee: Thanks a lot for your reply, this one sounds quite promising and I will give it a shot.

    In the meantime I kept on experimenting some more and it looks like my impulses are working out so far...

    Framerate independent movement activated AND dt on my impulse gives me satisfying results... must have overlooked this earlier because rotation still won't work as it should:

    I'm rotating my object by applying torque

    without dt rotation on lower framerates is much too slow and with dt rotation on lower framerates is much too fast.

    When measuring the angular velocity of my Sprite using "Sprite.Physics.AngularVelocity" WITHOUT dt I get the exact same values on all framerates, while still on lower framerates my sprite is rotating much much slower than on 60FPS.

    This seems odd, because the manual says AngularVelocity outputs "The current angular velocity (rate of rotation) of the physics object, in degrees per second." and not degrees per tick or something.

    So I'm still struggling^^

  • If you want, upload your project file so i can take a look at it. My current project use a lot physics too, maybe i can find your mistake.

  • This seems odd, because the manual says AngularVelocity outputs "The current angular velocity (rate of rotation) of the physics object, in degrees per second." and not degrees per tick or something.

    The difference is that you are directly setting the angular velocity... like forcing it to to turn at this speed on every single tick...

    whereas applying torque every tick will 'add on' to the ang veloc every tick, and this is where the torque force will be affected by delta time and subsequently affect the outcome ang veloc

    the same is true for using 'set velocity' instead of 'apply force'

  • If you want, upload your project file so i can take a look at it. My current project use a lot physics too, maybe i can find your mistake.

    The only mistake I made was to test on low end computers ;) so far everything worked perfectly, but anything below 30FPS is a complete mess.

  • keepee: sorry, I think you got me wrong...

    I used "apply torque" to spin my object and Sprite.Physics.Angular Velocity to measure the max rotation speed (got angular damping). And I used (or used not) the dt in the apply torque action...

    apply torque with dt > spinning way too fast on low framerate

    apply torque without dt > spinning way too slow on low framerates, but displaying the same angular velocity like on 60FPS

  • ah i see, my bad.

    although you could actually try what I said and see if it works for you.. depending on your context i guess...

    I've set my characters collision object as a circle, and to move left and right it sets angular velocity clockwise or anticlockwise. It actually works pretty good.. Changing the friction adjusts the inertia.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I've set my characters collision object as a circle, and to move left and right it sets angular velocity clockwise or anticlockwise. It actually works pretty good.. Changing the friction adjusts the inertia.

    Sounds fun, especially when riding across other physics objects.

    In my Case it's flying with two button controls.

    left button: turn left(and accellerate a little);

    right button: turn right(and accellerate a little);

    both buttons: accellerate

    For today I've spent enough time on this issue, tomorrow I'll test your approach from above ;)

    Thanks again

  • || Sounds fun, especially when riding across other physics objects.

    yup! like in old cartoons where a character is riding a barrel, and has to run backwards to make it go forwards

    || In my Case it's flying with two button controls.

    ah ... it might be a bit crappy in this case, it'll just make it turn very stiff.. like in asteroids.

    Best do it by applying torque...

    relevant protip: set the angular damping very high to stop it from spinning too much when you stop turning

  • elevant protip: set the angular damping very high to stop it from spinning too much when you stop turning

    Even better: high angular damping on key press, low angular damping on key release, so that it can still rotate realistically on collision ;) had that one covered^^

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)