playcanvas / engine

Powerful web graphics runtime built on WebGL, WebGPU, WebXR and glTF
https://playcanvas.com
MIT License
9.71k stars 1.36k forks source link

Physics simulation has issues on high refresh rate monitors #4277

Open yaustar opened 2 years ago

yaustar commented 2 years ago

Project: https://playcanvas.com/project/935538/overview/physics-sim-bug

On a 60Hz screen, the box moves between the two cones once the ball reaches the top.

https://user-images.githubusercontent.com/16639049/170557683-f153c996-03eb-4628-b18b-e6245fd09a65.mp4

If we reduce the dt by setting the app.timeScale to 0.1, it no longer switches and says on the left.

https://user-images.githubusercontent.com/16639049/170557705-73f955a4-bff4-4c60-be00-a482976f3dc5.mp4

The code logic teleports the cube (the rigid body) between each cone at the bottom when the ball (a child of the cube) reaches the top.

There's a bit of code at start of the update that checks if the cube has been teleported correctly and it doesn't if the dt is small.

yaustar commented 2 years ago

A way to get around this is to create a new physics body with the following code:

            this.parent.rigidbody.teleport(this.bottom1.getPosition());
            this.parent.rigidbody.type = pc.BODYTYPE_KINEMATIC;
            this.parent.rigidbody.type = pc.BODYTYPE_DYNAMIC;
valentincognito commented 2 years ago

This saved my life! Thanks for the trick

yaustar commented 2 years ago

Another report of the same issue: https://forum.playcanvas.com/t/weird-behaviour-on-120hz-monitors-and-some-120hz-phones/26221