Ageia's physx processor is the world's first physical analog processor (PPU), which removes the burden of a central processor for physical simulation. Physx PPU's design architecture is based on vertex-based multi-thread operations, allowing game developers to perform precise, smooth, and animated creations and motion simulations, such as hair, fabric, liquid, and fluid. Currently, ageia physx is the world's first and only physical-focused physx processor. Algorithm Processor products.
When using physx SDK physical engine development kit to achieve our simulation results, the following steps are generally required:
(1) printcontrols ();
(2) initglut (argc, argv );
(3) initnx ();
(4) glumainloop ();
(5) releasenx ();
The most important function is initnx (). It is also used to initialize physx, create a physx SDK instance, and create our scenario. The following describes the functions of each function.
I. Printcontrols ();
Obviously, this function is used to tell players how to perform operations. The operation buttons can be set according to your preferences.
II. Initglut (argc, argv );
Physx is developed on OpenGL. Therefore, an OpenGL framework must be established before the physx instance is initialized.
①. Gluinit (& argc, argv) is used to initialize glut and process arbitrary command line variables.
②. Gluinitwindowsize (INT width, int size) specifies the size of the window in pixels.
③. Create a window with dual cache, RGB color model, and large cache in the gluinitdisplaymode (glu_rgb | glu_double | glu_depth)
④. Create a window with OpenGL in the valley window (char * string). String indicates the window name of the window.
⑤. Fig ()
6. gludisplayfunc (rendercallback) Rendering
Processcamerakeys ();
Setupcamera ();
If (gscene &&! Bpause)
{
Getphysicsresults ();
Processinputs (); Based on the selected object, apply the force in the front and back, up and down, left and right directions to the object, and then call the object method addforce to produce different physical effects.
Startphysics ();
}
// Display scene
Renderactors (bshadows );
Call the drawactor (nxactor * Actor) function to render the objects in the scene. It is true that the drawactor (nxactor * Actor) function calls the painting function of different shapes to render the objects according to the object shape. During rendering, the display list is used to draw objects of different shapes. In physx, object shapes are divided into the following types: nx_shape_plane (panel-like), nx_shape_box (box-like), nx_shape _ sphere (ball shape), nx_shape_capsule (capsule shape ), nx_shape_convex (convex multilateral shape), nx_shape_mesh (mesh shape ).
When bshadows is true, the shadow of the rendered object is not drawn if it is false.
Drawforce (box, gforcevec, nxvec3 (1, 1, 0 ));
Rendering the force direction of an object
7. Fig (reshapecallback)
Setting window
Callback. glutidlefunc (idlecallback );
Callback. Fig (keyboardcallback );
Callback. glukeyboardupfunc (keyboardupcallback );
?. Gluspecialfunc (specialcallback );
Here, call resetnx () to re-render
?. Glumousefunc (mousecallback );
?. Glumotionfunc (motioncallback );
?. Motioncallback (0, 0 );
3. Initnx () because we need to initialize the physx SDK instance and create the scenario we need, we need to set the following variables and set them as global variables.
Nxphysicssdk * gphysicssdk = NULL; // physx SDK Instance Object
Nxscene * gscene = NULL; // scene object
Nxvec3 gdefagrgravity (0,-9.8, 0 );
* ** Note: the orientation of the coordinate system varies with physx, OpenGL, and 3dmax. You can see it when running the demo. Their coordinate systems are as follows:
The following describes how to initialize an instance and create a scenario in initnx.
①. Instantiate physics SDK
Gphysicssdk = nxcreatephysicssdk (nx_physics_sdk_version );
After the physics SDK is initialized, it is just a simple instance. You can set the physical parameters of the instance to enrich our simulation results.
Gphysicssdk-> setparameter (nx_skin_width, 0.01 );
②. Create a scenario
Nxscenedesc scenedesc; // scenario table object
Scenedesc. Gravity = gdefaultgravity;
Scenedesc. broadphase = nx_broadphase_coherent;
Scenedesc. collisiondetection = true;
Gscene = gphysicssdk-> createscene (scenedesc );
In physx, when you create a scenario or an object role, you must first set the scenario and physical parameters of each object through the corresponding Descriptor (not accurate in translation, used to simulate real world environments and objects. After the interpreter is created, you can use the createscene (nxscenedesc) function to create a scenario object.
Generally, the parameter of the scenario descriptor is to set the gravity acceleration scenedesc. Gravity, whether to perform the collision detection collisiondetection, and true. It is widely used in physx SDK descriptors. The descriptor includes all the information about the object you created. broadphase-coherent is one of the three collision detection methods.
Gphysicssdk-> setparameter (nx_skin_width, 0.01 );
When the materials of the collision objects are very soft, in reality, we will find that when the collision occurs, the objects will be embedded into each other, here we can use the physical parameter nx_skin_width, which defaults to 0.05 m. The larger the value, the more embedded
At the same time, we can create materials for all objects in the scenario. The created material defines the physical properties of collision and object materials. Such as rebound coefficient, static friction, sliding friction.
// Create the default material creates a material object through the Material Index
Nxmaterial * defaultmaterial = gscene-> getmaterialfromindex (0 );
Defaultmaterial-> setrestitution (0.5 );
Defaultmaterial-> setstaticfriction (0.5 );
Defaultmaterial-> setdynamicfriction (0.5 );
Create an object. Take box as an example.
Nxactor * box = createbox (nxvec3 (5, 1, 0 ));
Nxactor * createbox (const nxvec3 & Pos)
{
// Add a single-shape actor to the scene
Nxactordesc actordesc;
Nxbodydesc bodydesc;
// The actor has one shape, a box
Nxboxshapedesc boxdesc;
Boxdesc. dimensions. Set (0.5, 1, 0.5 );
Actordesc. shapes. pushback (& boxdesc );
Actordesc. Body = & bodydesc;
Actordesc. density = 10;
Actordesc. globalpose. t = Pos;
Return gscene-> createactor (actordesc );
}
We create a role participant box, whose type is nxactor *. When creating this object, you need to set its descriptor, and then use the createactor (nxactordesc actordesc) function to add this object to the scenario. Each object has a descriptor corresponding to its own shape. It is used to set the physical parameters of an object. Boxdesc the descriptor describes the length, width, and height of the box as 0.5, the initial position, and the density of the box.
③. When all object objects are created, call updatetime () to get the time from the previous frame rendering to the present
④ When the scenario is created successfully, use the startphysics () function to start its first frame simulation.
Void startphysics ()
{
// Update the time step
Nxreal deltatime = updatetime ();
// Start collision and dynamics for Delta time since the last frame
Gscene-> simulate (deltatime );
Gscene-> flushstream ();
}
Simulate (deltatime) is the key to physx's solution to physics.
Simulation of time stepping by flushstream ()
4. Glumainloop ()
Program Will remain in the glumainloop () until the user ends. After a scene is rendered, the rendercallback () callback function is called every time the next scene is set.
5. Releasenx ()
Delete all objects and scenes in a scenario