Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
30 replies to this topic

#21 N01   Members   -  Reputation: 194

Like
0Likes
Like

Posted 09 September 2013 - 10:55 PM

Do you mean that in 2.5 seconds (that is: 2.5 seconds of simulation time), your sphere, starting at rest, has fallen 132m? If so, that's much further than expected - you'd expect 0.5 * 9.81 * 2.5 * 2.5 which is about 30.7m. That would suggest your simulation is running faster than normal... so I'm not sure I've interpreted your numbers right.

 

In post 13 your loop multiplies by mStepSize, which is definitely wrong - but then you correct it in post 15. But it's still not quite clear what your latest version looks like.

 

In any case I'd expect this to work:

...

I've replaced two calls to clock with one. I wonder - with your current code, assuming your scene is really simple, that update loop may spend a lot of time just spinning round, whilst it waits for the accumulator to accumulate, in which case each time it will lose a little bit of time - maybe that's where your slowdown is coming from?

 

 

resulting numbers also confused me. to clarify, ~2.5 seconds is 149frames/60 FPS in PVD. according to formula, it should've fallen 30 units in 2.5 seconds. but in my scene it's less, than human height. rigid objects shouldn't fall from that height for 2.5 seconds. even what i consider as "slow mo" runs faster. but you measure it in meters, and i do in units and i'm not sure, how do you convert arbitrary scene into metrical system. ...so i guess, scale somehow matters. and also, even when i multiply deltaTime by 5, so in-game physics are close to speed i expect, PVD's recodred clip is still in slow-mo.

 

 

scene is not super basic, but it's not a full scale level. it's just some part of the level done, with some additional testing objects.

running simulation looks like that in semi-pseudocode:

physicsManager::update() {
   float currentTime = clock();
   float deltaTime = (currentTime - mLastTime)/CLOCKS_PER_SEC;
   mAccumulator  += deltaTime;
   mLastTime = currentTime;

   while(mAccumulator > mStepSize) {
         mAccumulator -= mStepSize;
         mScene->simulate(mStepSize);
         mScene->fetchResults(true);
   }

   //update objects
   ...
}

logicAndProcessingThread {
   //do stuff
   ...
   physicsManager.update();
   Sleep(30 - timeTookProcessing); //running at 30FPS
}

renderingThread() {
   //render stuff
   ...
   Sleep(16 - timeTookRendering); //running at 60 FPS
}

Edited by N01, 09 September 2013 - 11:32 PM.


Sponsor:

#22 MrRowl   Crossbones+   -  Reputation: 1482

Like
1Likes
Like

Posted 10 September 2013 - 02:35 AM

physicsManager::update() {
float currentTime = clock();
   float deltaTime = (currentTime - mLastTime)/CLOCKS_PER_SEC;
   mAccumulator  += deltaTime;
   mLastTime = currentTime;

   while(mAccumulator > mStepSize) {
         mAccumulator -= mStepSize;
         mScene->simulate(mStepSize);
         mScene->fetchResults(true);
   }

}

 

OK - I see you changed this after first posting it to replace two calls to clock with one as I suggested. Can you confirm whether it does/doesn't work now?

 

There's still a potential problem that, according to the above code, you're probably storing your absolute time (mLastTime) as a float, as well as converting it to a float immediately after getting it. When the time gets large - which may not take very long since I expect CLOCKS_PER_SEC is pretty huge, then your deltaTime calculation subtracts two huge floats - and the result will get less and less precise. 



#23 N01   Members   -  Reputation: 194

Like
0Likes
Like

Posted 10 September 2013 - 03:36 AM

OK - I see you changed this after first posting it to replace two calls to clock with one as I suggested. Can you confirm whether it does/doesn't work now?

 

There's still a potential problem that, according to the above code, you're probably storing your absolute time (mLastTime) as a float, as well as converting it to a float immediately after getting it. When the time gets large - which may not take very long since I expect CLOCKS_PER_SEC is pretty huge, then your deltaTime calculation subtracts two huge floats - and the result will get less and less precise. 

 

no, it didn't affect the behavior.

 

i also tried making currentTime and lastTime clock_t, and updating the function accordingly:

        clock_t currentTime = clock();
        float deltaTime = (currentTime - mLastTime)/float(CLOCKS_PER_SEC);
        mAccumulator  += deltaTime;
        mLastTime = currentTime;

        while(mAccumulator > mStepSize) {
            mAccumulator -= mStepSize;
            mScene->simulate(mStepSize);
            mScene->fetchResults(true);
        }

it acts exactly the same.



#24 MrRowl   Crossbones+   -  Reputation: 1482

Like
0Likes
Like

Posted 10 September 2013 - 04:31 AM

clock_t currentTime = clock();
        float deltaTime = (currentTime - mLastTime)/float(CLOCKS_PER_SEC);
        mAccumulator  += deltaTime;
        mLastTime = currentTime;

        while(mAccumulator > mStepSize) {
            mAccumulator -= mStepSize;
            mScene->simulate(mStepSize);
            mScene->fetchResults(true);
        }


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):
 

        while(mAccumulator > mStepSize) {
            mAccumulator -= mStepSize;
            mScene->simulate(mStepSize);
            mScene->fetchResults(true);

            static float physXTime = 0.0f;
            physXTime += mStepSize;
            printf("%f\n", physXTime);
            }

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...

 

 



#25 adt7   Members   -  Reputation: 425

Like
0Likes
Like

Posted 10 September 2013 - 06:20 AM

 
logicAndProcessingThread {
   //do stuff
   ...
   physicsManager.update();
   Sleep(30 - timeTookProcessing); //running at 30FPS
}

renderingThread() {
   //render stuff
   ...
   Sleep(16 - timeTookRendering); //running at 60 FPS
}

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.

#26 N01   Members   -  Reputation: 194

Like
0Likes
Like

Posted 10 September 2013 - 06:21 AM

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, 10 September 2013 - 06:21 AM.


#27 N01   Members   -  Reputation: 194

Like
0Likes
Like

Posted 10 September 2013 - 07:18 AM

 

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, 10 September 2013 - 08:08 AM.


#28 MrRowl   Crossbones+   -  Reputation: 1482

Like
0Likes
Like

Posted 10 September 2013 - 07:25 AM


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...



#29 N01   Members   -  Reputation: 194

Like
0Likes
Like

Posted 10 September 2013 - 08:06 AM

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.



#30 MrRowl   Crossbones+   -  Reputation: 1482

Like
0Likes
Like

Posted 10 September 2013 - 08:23 AM


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.



#31 N01   Members   -  Reputation: 194

Like
0Likes
Like

Posted 10 September 2013 - 08:30 AM

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, 10 September 2013 - 08:40 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS