Physx SDK 3.2 was released recently. Out of curiosity, I downloaded it and looked at it. I found it quite good. Compared with 2.x, it is greatly changed and more powerful. The confusing library names in version 3.1 are also standardized, and two new examples are added. One is the role cloth simulation, and the other is the custom gravity (this is basically the same as the example in Havok ).
Starting from 3.x, the SDK samples were packaged using a sample framework. It was not very convenient to study and took some time to write a test program. (Csdn won't typeset code. It's ugly. Let's take a look)
# Include <pxphysicsapi. h>
# Include <extensions \ pxdefasimulsimulationfiltershader. h>
# Include <extensions \ pxextensionsapi. h>
# Include <extensions \ pxdefaultallocator. h>
# Include <extensions \ pxdefaulterrorcallback. h>
# Include <Foundation/pxfoundation. h>
# Include <pxtask/pxcudacontextmanager. h>
# Include <physxprofilesdk \ pxprofilezonemanager. h>
# Include <pxscene. h>
// 3. x starts to use the namespace.
Using namespace physx;
Bool recordmemoryallocations = true;
// The same is true for the error callback function Havok.
Static pxdefaulterrorcallback gdefaulterrorcallback;
Static pxdefaultallocator gdefaultallocatorcallback;
Static pxsimulationfiltershader gdefaultfiltershader = pxdefasimulsimulationfiltershader;
Int main (INT argc, char ** argv)
{
Pxfoundation * mfoundation = pxcreatefoundation (px_physics_version, gdefaultallocatorcallback, gdefaulterrorcallback );
If (! Mfoundation ){
Printf ("pxcreatefoundation failed! ");
Return-1;
}
Bool recordmemoryallocations = true;
// Pxprofilezonemanager used for Performance Analysis
Pxprofilezonemanager * mprofilezonemanager = & pxprofilezonemanager: createprofilezonemanager (mfoundation );
If (! Mprofilezonemanager ){
Printf ("pxprofilezonemanager: createprofilezonemanager failed! \ N ");
Return-1;
}
// Detect and initialize the Cuda module. If you have a video card that supports cuda, you can use it here. (We recommend that you install the latest graphics card driver to test the program ).
Pxtask: cudacontextmanagerdesc;
Pxtask: cudacontextmanager * mcudacontextmanager = pxtask: createcudacontextmanager (* mfoundation, cudacontextmanagerdesc, mprofilezonemanager );
If (mcudacontextmanager ){
If (! Mcudacontextmanager-> contextisvalid ()){
Mcudacontextmanager-> release ();
Mcudacontextmanager = NULL;
}
}
If (mcudacontextmanager ){
Printf ("device name: % s \ n", mcudacontextmanager-> getdevicename ());
Printf ("driver version: % d \ n", mcudacontextmanager-> getdriverversion ());
Printf ("Total Bytes: % d \ n", mcudacontextmanager-> getdevicetotalmembytes ());
Printf ("core count: % d \ n", mcudacontextmanager-> getmultiprocessorcount ());
}
// Pxtolerancesscale () is used for scale, but the SDK says it hasn't been done yet, but it just occupies a pitfall.
Pxphysics * mphysics = pxcreatephysics (px_physics_version, * mfoundation,
Pxtolerancesscale (), recordmemoryallocations, mprofilezonemanager );
If (! Mphysics ){
Printf ("pxcreatephysics failed! \ N ");
Return-1;
}
If (! Pxinitextensions (* mphysics )){
Printf ("pxinitextensions failed! \ N ");
Return-1;
}
Physx: pxcooking * mcooking = pxcreatecooking (px_physics_version, * mfoundation, pxcookingparams ());
If (! Mcooking ){
Printf ("pxcreatecooking failed! \ N ");
Return-1;
}
Pxscene * mscene;
Pxscenedesc scenedesc (mphysics-> gettolerancesscale ());
Scenedesc. Gravity = pxvec3 (0.0f,-9.81f, 0.0f );
// Create a CPU distributor (literal translation, my translation). The pxdefacpucpudispatchercreate parameter is the number of threads.
Pxtask: cpudispatcher * mcpudispatcher;
If (! Scenedesc. cpudispatcher ){
Mcpudispatcher = pxdefacpucpudispatchercreate (4 );
If (! Mcpudispatcher ){
Printf ("pxdefacpucpudispatchercreate failed! \ N ");
Return-1;
}
Scenedesc. cpudispatcher = mcpudispatcher;
}
If (! Scenedesc. filtershader ){
Scenedesc. filtershader = gdefaultfiltershader;
}
// Try to use the GPU distributor
If (! Scenedesc. gpudispatcher & mcudacontextmanager ){
Scenedesc. gpudispatcher = mcudacontextmanager-> getgpudispatcher ();
}
// Create a physical scenario. If there are so many physical scenarios above, this line is actually used.
Mscene = mphysics-> createscene (scenedesc );
If (! Mscene ){
Printf ("createscene failed! \ N ");
Return-1;
} Else {
Printf ("createscene successful! \ N ");
}
// Release one by one in sequence. Otherwise, a warning is prompted.
Mphysics-> release ();
Mcooking-> release ();
Mcudacontextmanager-> release ();
Pxcloseextensions ();
Mfoundation-> release ();
Return 0;
}
The above is all the test code. I personally think that physx of version 3.2 is very mature and more professional. It is different from Havok. If you use a video card with powerful Cuda hardware acceleration capabilities, it can be said that it is better. By the way, NVIDIA also provides sdks for Linux and Android, which are well performed across platforms. Unlike the free version of Havok, only two components can be used, and it must be limited to Windows.