Consistent physics regardless of the device

Hey all, I’m developing a game that uses some physics on it, but I didn’t found a way to make the physics consistent across devices.

With inconsistent I mean that it jumps faster or slower depending of the device, I have these examples here:
PC 1:

PC 2: (sorry I left my audio on it)

PC3: (sorry I left my audio on it)

Thats basically what I have here:

  const impulseDirection = new Vector3(0, 1, 0);
  const impulseMagnitude = 10.5;

  player.mesh.physicsImpostor.applyImpulse(
    impulseDirection.scale(impulseMagnitude), 
    player.mesh.getAbsolutePosition()
  );

I tried to multiply the impulse with the framerate without success, it stills inconsistent:

  const impulseDirection = new Vector3(0, 1, 0);
  const impulseMagnitude = 10.5;
  const animationRatio = scene.getAnimationRatio();

  player.mesh.physicsImpostor.applyImpulse(
    impulseDirection.scale(impulseMagnitude * animationRatio), 
    player.mesh.getAbsolutePosition()
  );

The issue seems to be the same as the fancy old games that vary its speed depending of the computing capacity of the machine, but I’m not sure how to solve it with physics. :slightly_frowning_face:

How can I make the jump consistent?

Project link: CATRUN - A JavaScript and BabylonJS game!!@!!!!!!!!1111ONZE

I think you might need to use a deterministic time step. @Cedric could speak more about it, and also correct me if I am fully wrong :slight_smile:

Yes, fixed frame time can help…if framerate is the same accross devices.
I know some racing games compute physics independently from rendering (at a higher frequency sometimes) and result, used for rendering is interpolated for the current rendering time.

Another solution is to not use physics for characted control but custom computation that can use the technics explained here:

1 Like

OMG thank you, but dangit I thought it was something easier x.x

Hey @Cedric I spent the last two days trying to fix this with no success x.x

Like, this is a common issue with physics? I think its pretty weird that I didnt found many questions about this topic and I thought that it would be something very common.

I didn’t want to lock the framerate cause it sounds like a work arround to me (maybe I’m wrong) and neither wanted to avoid use physics at all cause I wanted to learn this for further projects, so I would like to try to solve this with computing the phisics independently, but how can I do that? Do babylon have an example of that issue solved?

Sorry for that many questions, Im very unexperienced with game development and 3d so I’m very confused with that :face_with_spiral_eyes:

Do I’m trying to solve a uncommon issue or the wrong way?

Thank you!

No, it’s not a common issue. At least not in this forum.
I think a lot of people assume their program will run at a fixed framerate and code everything to stick with that.
AFAIK, there is nothing in the engine to get a deterministic physics with a variable time step.
I’m not even sure it’s possible with physics. maybe more with custom coded spring/dynamics.

You can try to use physics substeps: Advanced Physics Features | Babylon.js Documentation

I’m sure it will tend toward what you want without being exactly it.

1 Like