PhysX - Scene scaling, object size, timestep and gravity inconsistency.

This topic is 1597 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

OK - well this looks right now.

One quick thing to test is whether your game's accumulation of time is correct. So in your loop you can just print out the total time you've passed to PhysX, and as the game runs, check whether it is counting up in seconds (i.e. compare it with a watch):

        ...

If the time printed out is slow, then you need to check the timing code.

If the time printed out is fine, then I'm pretty sure PhysX is doing the right thing - just that you have some scaling problems. I'm still not sure from your posts (including #13) how you're determining that the simulation is running slower than expected. If you're looking at the time it takes for a falling object to move the size of a human sized object - you need to be sure that your "human sized object" is really about 2m big.

Since you set gravity to 9.81 m/s that implies your distance unit is metres, and your time unit is seconds. if not - then you need to use a different gravity value.

Feel free to upload a pvd capture and I'll take a look...

i tried outputting elapsed time through printf and it does output realtime seconds quite perfectly. so i guess, timestepping is fine.

i just determine it visually, objects fall and interact to slowly.  i guess, problem is scaling. because in my scene, 1 unit is like 5-6cm and objects have corresponding weight. if i increase gravity, it doesn't go well. objects start shaking on the ground. and if i re-scale my scene by a factor of 0.05f(1/20), speed gets normal. although it seems like precision for smaller objects is lost. if i shoot a dense sphere with high velocity at a small object, it most likely will just pass through, it just feels a bit off.

it says "Error You aren't permitted to upload this kind of file", so i uploaded PVD clip here:

http://rghost.net/private/48672806/63f9e682ffa0f7a14746166f83a12cd0

it demonstrates test scene without any re-scaling, so it's slow-mo, and i just fly around for a bit, shooting spheres.

Edited by N01

Share on other sites

Wait, is that actually representative of your actual code? Those calls to sleep (especially in the physics thread) will not be doing you any good.

The accumulation loop you posted is design to run as quickly as possible (i.e. in a while(true) style loop) and fire the Physics events at the correct time. With the setup you have now the simulation will not be being called at the correct frequency due to differences in sleep time and the fact that your time deltas are going to be huge.

this is simplified code. but yes, my threads do sleep, but they also do take in account time they were busy with processing, so they don't sleep more than it's required. and my deltaTime is not really huge, it's always around 0.033sec with rare deviations of 0.001sec. this loop is not designed to run continuously, it's designed to compensate for elapsed time. the only case, then such organisation may cause trouble, is if processingThread cannot finish in given time of 30ms, but that is undesirable for application in general, so i shouldn't allow it to happen. and you generally don't want any continuous  thread to run without any sleeping, it is similarly irresponsible and dumb as using vsync to limit your framerate.

Edited by N01

Share on other sites

i just determine it visually, objects fall and interact to slowly.  i guess, problem is scaling. because in my scene, 1 unit is like 5-6cm and objects have corresponding weight. if i increase gravity, it doesn't go well. objects start shaking on the ground. and if i re-scale my scene by a factor of 0.05f(1/20), speed gets normal. although it seems like precision for smaller objects is lost. if i shoot a dense sphere with high velocity at a small object, it most likely will just pass through, it just feels a bit off.

OK - well if 1 unit is, say 0.05m, then you need to set gravity to 9.81 / 0.05, and get rid of your time scaling.

That's not all though - you need to:

1. Pass in an appropriate set of scales when you create the physics SDK, since the defaults will be inappropriate. Look up physx::PxTolerancesScale

2. Increase the contact offset on the shapes you create - by a factor of 1/0.05.

The second thing should get rid of the jittering.

You also need to consider what density you want, and what your mass units are. If you want mass in kg, then you will have pretty weird densities, because you'll have a factor of 20^3.

A better option is to work in metres, kg and seconds - either by converting your source/game assets, or by scaling whenever you pass things across the PhysX API.

One general thing though - it really is (in my experience) worth trying to be quite precise when dealing with physics simulation. Even just to use a physics SDK you need to have a reasonable understanding of physics (what's the difference between force and impulse etc), and be quite careful - otherwise you may hack things into working, but in the future (e.g. if you change the timestep) it can all come back to haunt you!

I'll try to have a look at the PVD later...

Share on other sites
OK - well if 1 unit is, say 0.05m, then you need to set gravity to 9.81 / 0.05, and get rid of your time scaling.

That's not all though - you need to:

1. Pass in an appropriate set of scales when you create the physics SDK, since the defaults will be inappropriate. Look up physx::PxTolerancesScale

2. Increase the contact offset on the shapes you create - by a factor of 1/0.05.

The second thing should get rid of the jittering.

You also need to consider what density you want, and what your mass units are. If you want mass in kg, then you will have pretty weird densities, because you'll have a factor of 20^3.

A better option is to work in metres, kg and seconds - either by converting your source/game assets, or by scaling whenever you pass things across the PhysX API.

One general thing though - it really is (in my experience) worth trying to be quite precise when dealing with physics simulation. Even just to use a physics SDK you need to have a reasonable understanding of physics (what's the difference between force and impulse etc), and be quite careful - otherwise you may hack things into working, but in the future (e.g. if you change the timestep) it can all come back to haunt you!

I'll try to have a look at the PVD later...

i did the latter, found precise scaling factor for the scene and rescaled while passing to PhysX. it shouldn't couse trouble in future(unlike other option), less fiddly and i already had this option programmed into my engine.

Share on other sites

i did the latter, found precise scaling factor for the scene and rescaled while passing to PhysX. it shouldn't couse trouble in future(unlike other option), less fiddly and i already had this option programmed into my engine.

The objects in the PVD doesn't look like they've had this scaling. Gravity in the scene is 9.81 - which means you're using metres as your distance unit. Yet the box in the scene is 24m long - and similarly the capsules - if this is supposed to be a road with debris(?) it's absolutely huge, and no wonder everything moves in a way that looks like it's in slow motion.

Share on other sites
The objects in the PVD doesn't look like they've had this scaling. Gravity in the scene is 9.81 - which means you're using metres as your distance unit. Yet the box in the scene is 24m long - and similarly the capsules - if this is supposed to be a road with debris(?) it's absolutely huge, and no wonder everything moves in a way that looks like it's in slow motion.

yes, i've stated in that post(where i posted a clip from PVD), that PVD clip is before re-scaling scene to 1u=1m scale.

here's PVD clip with scene rescaled:

rghost.net/private/48675822/15cb958a221323afef1c3231e4bd4451

Edited by N01