RigsOfRods / rigs-of-rods

Main development repository for Rigs of Rods soft-body physics simulator
https://www.rigsofrods.org
GNU General Public License v3.0
1.01k stars 178 forks source link

Investigating physics precision loss with increasing distance from world origin. #3034

Open ohlidalp opened 1 year ago

ohlidalp commented 1 year ago

It was recently brought up on Discord#development that physics accuracy degrades very quickly with distance from the world origin: https://discord.com/channels/136544456244461568/189904947649708032/1093130839350583306. I was very surprised because I know that internally the node positions are stored and updated relative to local physics origin which is always kept close to the vehicle.

Test on master branch I did a test: load 'simple2' terrain, load 'DAF semitruck', press J to pause physics, align camera on the side, do goto 777777 777777 777777 in the console, press Shift+C for free cam and Alt+C for fixed-freecam, set slow motion ratio to 0.045 (top menubar / settings / slow motion), press K to activate skeletonview and finally J to unpause physics. On master branch, you'll see this: dafNodesAbs777777

Corrected skeletonview on this branch I realized the skeletonview shows node's absolute positions, not the relative positions the physics simulation uses. That's why, even though the motion of individual nodes is jerky, the overall motion is quite continuous. After a bit of reading on transformation matrices, I fixed the skeletonview to display the actual node positions. If you perform the same test with this branch, you'll get this result: dafNodesRel777777 Now the skeletonview moves smoothly but everything else is jerky. This is because both props and cab meshes use absolute node positions. Same applies to flexbodies, flex airfoils and flextires. The camera position is also absolute and follows vehicle's absolute position, that's why it gets crazy jumpy in large distances (the gifs above use fixed-freecam).

Is it all just a visual glitch then? Well, node forces and velocities are always small and centered around their node, so no problem there. However, collisions are all calculated in world space as I saw. Same with airfoils (wings). And there are more minor calculations (fusedrag) which also uses absolute position. I need to do a lot more research.

Reading Links that helped me make this real:

Links that gave me confidence for further fixing:

ohlidalp commented 1 year ago

I fixed the cab mesh to display undistorted anywhere. I took advantage of the fact the mesh is regenerated every frame. Instead of choosing an arbitrary mesh origin and generating the mesh from node absolute positions, I place the mesh origin at physics origin (which only moves on occasion) and generate the mesh from node relative positions. This creates an illusion of smooth movement within world space while the mesh is actualy not moving, only vertices within the mesh are moving. The same fix can be applied to flextires, flexbodies and flex airfoil, but this commit only fixes the cab mesh as proof of concept. dafCabmeshRel777777

CuriousMike56 commented 1 year ago

Definitely not just a visual glitch, N/Bs become increasingly unstable the further away you are from world origin: RoR_2023-04-09_20-20-02 (1 beam broken, 100+ deformed) image On Grenoble (https://forum.rigsofrods.org/resources/grenoble.399/), everything shakes like crazy, including the first person character camera.

EDIT: Ignore most of that, you wrote I want to eliminate the graphical glitches first so I can clearly see the physical glitches. on Discord.

ohlidalp commented 1 year ago

Indeed not just a visual glitch, my first test was with Gavril MV4 and it shaked itself apart. But the fact is, visuals make it look way worse than it actually is. I suspect ground collisions and other external forces (wings, fusedrag) to be the culprit, because when I put DAF semitruck in mid-air (notice the tests above are 777777 on all axes) the N/B seems perfectly stable, only visually distorted.

To sum up what I learned so far:

@paroj Can you please read the above and check if it makes any sense at all? This is the first time I'm digging this deep into 3D. Some of our jargon to make sense of it:

@CuriousMike56 Hopefully it's not all just babble to you, I'm not entirely sure what I'm doing myself :D

paroj commented 1 year ago

see https://ogrecave.github.io/ogre/api/latest/class_ogre_1_1_scene_manager.html#af24677a56a91713c40c3bdb9025da38d