official site: https://gameworksdocs.nvidia.com/PhysX/4.1/documentation/physxguide/Manual/RigidBodyOverview.html
红色代表需要弄懂的。
Introduction
This chapter will introduce the fundamentals of simulating rigid body dynamics using the NVIDIA PhysX engine.
Rigid Body Object Model
PhysX uses a hierarchical rigid body object/actor model, which looks like this:
层级关系:
Class | Extends | Functionality |
---|---|---|
PxBase | N/A | Reflection/querying object types. |
PxActor | PxBase | Actor name, actor flags, dominance, clients, aggregates, query world bounds. |
PxRigidActor | PxActor | Shapes and transforms. |
PxRigidBody | PxRigidActor | Mass, inertia, velocities, body flags. |
PxRigidStatic | PxRigidActor | Interface for static body in the scene. This kind of body has implicit infinite mass/inertia. |
PxRigidDynamic | PxRigidBody | Interface for dynamic rigid body in the scene. Introduces support for kinematic targets and object sleeping. |
PxArticulationLink | PxRigidBody | Interface for a dynamic rigid body link in a PxArticulation. Introduces support for querying the articulation and adjacent links. |
PxArticulation | PxBase | Defines interface for a PxArticulation. Effectively a contained referencing multiple PxArticualtionLink rigid bodies. |
The following diagram shows the relationship between the main types involved in the rigid body pipeline:
不得不说上图总结的很好。当前已经有体会的是: 每个Actor都需要有一个shape。 PxPhysics类创建PxShape,每个PxShape对象包含两个重要的方面: PxMaterial和Geometry. 某些较为复杂的Geometry类必须依赖其他的类,例如PxConvexMeshGeometry必须依赖于PxConvexMesh类。
The Simulation Loop
Simulation这部分在前面有过详细的讲解。
Now use the method PxScene::simulate() to advance the world forward in time. Here is simplified code from the samples' fixed stepper class:
mAccumulator = 0.0f;
mStepSize = 1.0f / 60.0f;
virtual bool advance(PxReal dt)
{
mAccumulator += dt;
if(mAccumulator < mStepSize)
return false;
mAccumulator -= mStepSize; //???
mScene->simulate(mStepSize);
return true;
}
This is called from the sample framework whenever the app is done with processing events and is starting to idle. It accumulates elapsed real time until it is greater than a sixtieth of a second, and then calls simulate(), which moves all objects in the scene forward by that interval. This is probably the simplest of very many different ways to deal with time when stepping the simulation forward.
To allow the simulation to finish and return the results, simply call:
mScene->fetchResults(true);
True indicates that the simulation should block until it is finished, so that on return the results are guaranteed to be available. When fetchResults completes, any simulation event callback functions that you defined will also be called. See the chapter Callback Sequence.
当fetchResults函数完成后,任何你定义的simulatioin event callback函数也会被调用(如果满足触发条件的话)。
It is possible to read and write from the scene during simulation. The samples take advantage of this to perform rendering work in parallel with physics. Until fetchResults() returns, the results of the current simulation step are not available. So running rendering in parallel with simulation renders the actors as they were when simulate() was called. After fetchResults() returns, all these functions will return the new, post-simulate state. See the chapter Threading for more details about reading and writing while the simulation is running.
For the human eye to perceive animated motion as smooth, use at least twenty discrete frames per second, with each frame corresponding to a physics time step. To have smooth, realistic simulation of more complex physical scenes, use at least fifty frames per second.
达到流畅的效果需要至少每秒20帧,对更加负责的常见至少每秒50帧。
Note
If you are making a real-time interactive simulation, you may be tempted to take different sized time steps which correspond to the amount of real time that has elapsed since the last simulation frame. Be very careful if you do this, rather than taking constant-sized time steps: The simulation code is sensitive to both very small and large time steps, and also to too much variation between time steps. In these cases it will likely produce jittery simulation.
仿真引擎对于过小或过大的时间步长都特别敏感! 同时,不要每次给定的时间step是由很大变化的! 否则会卡顿。
See Simulation memory for details of how memory is used in simulation.