How to create a realistic physics in UE4 (or UE5)

I tried to ask this question also in UE4 Answerhub, but lastly had problem with “question need moderator approval”. But it may be interesting also for the forum.

I’m trying to get realistic results values in order to build some simulation - I don’t need real time simulation, thus, I would like to use UE4 with a good physics calculations even if it takes cpu time.

I did a very simple test for moving a cube over a floor with some friction, but got values that are not fit to the simple physics equation.

First experiment - zero friction

I set a physical material with zero friction for both, a floor and a cube, then add one shot of force of 1N to the cube within axis x:

  • FPS = fix 40fps (so tick time step = 0.025s)
  • Gravity = -2000cm/s^2 (just in order to have nice numbers)
  • Cube mass = 1kg
  • Physical Material Friction = 0.0
  • addForce = 1N

The change of velocity should be:

  • TickStepTime = 1/fps = 0.025s
  • addForce = 1N
  • velocity change after one shot of force = 1N*0.025s/1kg = 0.025m/s = 2.5cm/s

And this is exactly what I get
Test8noriction

Second experiment - friction value = 0.025

At this time, the expected kinetic frictional force should be:

  • weight of cube = 2000cm/s^2 * 1kg = 20N
  • the kinetic frictional force should be then = 0.025 * 20N = 0.5N

The change of velocity after the force shot should be:

  • TickStepTime = 1/fps = 0.025s
  • addForce = 1N
  • kinetic frictional force = 0.5N
  • velocity change after one shot of force = (1N-0.5N)*0.025s/1kg = 0.0125m/s = 1.25cm/s

But the results are strange - velocity is not only at x axis, and there even ticks where the x velocity is negative as shown in the snapshot. Even the first velocity value is 0.396 cm/s and is absolutely far from the expected value 1.25cm/s
Test8friction

I will be glad for a help for understanding how to fit the UE4 physics behavior to the expected values, or maybe there is different way to use realistic physics in UE4?

Thanks.

========================================================================

Settings and cube blp snapshots attached here

========================================================================


First. Retry with substep enabled.

Second. Throw away unreal’s setup and make your own.
There’s reported issues with a bunch. And chaos is only going to make it worse.

There’s a physics subsection in the forum btw.

If you have the chops and the money, Havok is an option…

I tried enabling substep, also making max substep time short to alllow aruond 6 substeps , but no change with results.

I’m new to UE, what are the unreal’s setup should I have to throw - I use a physicals material , it overrides the default physics settings , isn’t it?

And yes, I posted this question under the physics category, hopefully to have a help.
Thanks.

There’s a few possibilities to get better physics via Nvidia Flex
https://github.com/NvPhysX/UnrealEngine
You have to implement the SDK yourself as the distros are old - or you could just use the older engine, which to be honest probably works A LOT better than the current sorry excuse for software they have been releasing.

Only things to “know”
is that the built in nvidia implementation is horribly bad. Has been for years. No one at epic ever gave a s*it about fixing it.

And that Chaos is currently even worse, but they may actually care about fixing it.

I would suggest you get a CHAOS branch and try it out. It can easily be a lot worse than the default launcher versions. It can also be a lot better. Depends on the current state, which being in development is rather fluid.
So you can get really bad stuff happening one day, and it can be running fine the day after (if you re-build from fresh source ofc).

All that aside.
pysmat works more-or less. The primary function for me has always been to provide different sounds for footsteps rather than change how physics work.
But ice can be made slippery and it works ok-ish.

There’s a few reports of the object weight not being taken into account for calculations.
So that may be something to keep in mind… relevant to physx

Those are options to check thanks.
But for so simple scenario like I had show - an object moving while there is a friction… can’t we use unreal engine (with physx ) for simulating it ?

I would say the last part of that sentence is the main problem. You can get more realistic / accurate results out of PhysX even in old versions like the one currently used in UE, but it’s often not exposed to users (correct me if I’m wrong).

As far as I can tell UE still uses PhysX 3.4, which is super old now. But even in that version you could select a more realistic friction model (like https://github.com/NVIDIAGameWorks/PhysX-3.4/blob/master/PhysX_3.4/Include/PxSceneDesc.h#L118). And there were additional parameters (like https://github.com/NVIDIAGameWorks/PhysX-3.4/blob/master/PhysX_3.4/Include/PxMaterial.h#L80) to tweak the default “patch friction”. If anything there are too many of them and it’s a bit overwhelming, but there are definitely some ways to get results closer to the analytical model.

A much better option though would be using PhysX 4, and in particular this flag (https://github.com/NVIDIAGameWorks/PhysX/blob/4.1/physx/include/PxMaterial.h#L93), which improves the results a lot for the default friction model, with no impact on performance. But of course for that UE would need to finally upgrade to a more recent version of PhysX.

Other than that it is useful to switch to fixed timesteps, and the TGS solver (again in PhysX 4). This gets quite close to the analytical model for simple cases like a box sliding on a plane.

1 Like

Ok, luckily, I found the solution for kinetic friction - the Friction value should be half of the physical one. Why? I don’t know.

So if I set Friction to 0.025/2 = 0.0125

Then results are correct.
image

I guess that the “noisy” values around 0.0 (in all directions) are results of the physical solver which mathematically ripples at the end. I think this may be handled by adding blp logic preventing velocities under some value.

I’m glad to find that UE4 can simulate such simple case correctly - I really don’t know why Friction parameter should be half of the physical Friction coeficient.

It could be that only half the force is initially applied because friction is immediately taken into account when you apply force.

And I’m not positive if that’s wrong or not. In theory i suppose it isn’t.

You could try lifting the cube off the floor before you apply the impulse to see.

To do it, enable simulate physics and immediately apply the impulse.
If the cube is 1mm off the floor, that’s enough to assume the friction came into play after the push.

Could it be related to what the PhysX docs mention here?

The friction force is known also from the reduction of the velocity per tick within the movement.
I tried with friction value of 0.01.
It should make a velocity change of -0.5cm/s per tick.
But the results shows the follow velocities within the ticks: 1.5cm/s, 0.5cm/s, 0cm/s…

If I will change the friction value to 0.005 (half of the real physical value) then I will get the correct results:
2.0cm/s, 1.5cm/s, 1.0cm/s…

Thanks for pointing out about this Physx flag in the document. I will check this.

What I’m saying is that maybe applying x100 force to an object that’s sitting on top of a floor with any friction makes it so that the initial value of force is reduced by the friction itself…

If the engine never applied 1N you’d never get the results you expect…

And by the looks of things the result you are getting is a fraction in relation to the friction…

Following comment by @ Theo_Poilmek , it seems that the patch method is the reason.
In Physx 3.2 documentation it seems that default friction mode is not the accurate one and may create a frictional force up to twice stronger than the analytical one. There is need to change the Friction mode as explained here:
https://documentation.help/NVIDIA-PhysX-SDK-Guide/CoulombFriction.html