Physx ( 1 )
By sssa2000
Contacts officially started todayPhysx, I 'd like to say a few simple words about this thing. This is a veryNBPhysical engine,Xbox360AndPS3All use this stuff.
PhysxThis engine is easy to use. I can't get it on his official homepage.SDKNow , You must register and submit an application before downloading. The latest official version seems to be2.5Version. I used the previously downloaded 2.4.4 .
It is worth mentioning thatPhysxFrom path settingsTutorialsOfCodeAll explanations are provided. Check if you are free. I just copied the document.
1 , Installation and setting
After installation, set the compiler path:
IncludeFile:
Sdks \ Foundation \ include ",
"Sdks \ physics \ include"
"Sdks \ physxloader \ include
"Sdks \ nxcharacter \ include"
"Sdks \ cooking \ include"
LibFile:
"Sdks \ Lib \ Win32 \"
OKNow you can officially use this engineSDK.
2 Concept
First, I would like to explain a fewPhysxConcept:
To use this physical engine to simulate real physical phenomena, you need the following:
Nxphysicssdk:Physics SDKObject, which is the most basic thing for physical simulation. It provides interfaces for creating physical systems and is a pure virtual class.
Nxscene:Scenario. It can be viewed as a world of physical concepts, responsible for managing all objects and the gravity of the world. OneProgramMultiple scenarios can be maintained.
Descriptor: A class responsible for initialization. You can use this class to initialize anything that the physical engine uses. Of courseDescriptorThere are also many typesSceneOfDescriptorWhich is responsible for initializing objects.DescriptorAnd so on. .
Nxmaterial: Material that describes the surface features of an object.
Actors: It is an object in a scenario. That is, the simulated object.
Joints:The object to connect.
Of course there are still many concepts, such as liquids. But it is enough for the beginning.
In addition, it is worth mentioning that the concept of time, like all real-time rendering graphics libraries, plays an indispensable role in this real-time Physical engine. The physical engine calculates the required physical computation within the specified time slice and then transmits the calculation result to the graphic interface.
3 Build a physical world
Nxphysicssdk*Gphysicssdk=Null;
Nxscene*Gscene=Null;
Nxactor*Groundplane=Null;
Nxactor*Box=Null;
First, createSDKAnd initialize the object, and then use the scenario and object respectively.
Void Initnx()
{
1,UseNxcreatephysicssdk() CreateSDKAnd initialize global variables.
2,CreateNxscenedesc, That is, the descriptionSceneTo assign the initial value to it, such as gravity and wind.
3,UseNxscenedescCreateScene:Gphysicssdk->Createscene(Scenedesc);
4,Create and initializeNxmaterial
5,Create2ItemsNxactordescAnd use them to create2ItemsNxactor.
6,Get current time
7,Start physical computing
}
I will stop listing the Code. If you needSDKYou can contact me, or who can provide a space for better storage.
It is worth noting that2ItemsNxactorBecauseNxactorMany parameters need to be set.]
4 , Physical Simulation
We need to perform physical operations before each rendering and then extract the results to the graphic part for rendering.
It is easy to use the engine for physical simulation. After we initialize the world and set the object, we only need2Then, the physical engine can automatically perform corresponding physical operations:
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();
}
// ================================================ ======================================
Void Getphysicsresults()
{
// Get results from gscene-> simulate (deltatime)
While(!Gscene->Fetchresults(Nx_rigid_body_finished,False));
}
NoteFetchresultsThe final parameter passed during function execution isFalseThis indicates that the program will be called in a non-blocking manner. If you use the blocking method, you will waitSimulateReturns results.
So call this before each rendering.2And then plot:
Void rendercallback ()
{
...
If (gscene &&! Bpause )//Paused or not Or do you need to plot the scenario?
{
Getphysicsresults ();//Obtain the last simulation result
Processinputs ();//Process Input
Startphysics ();//Perform new operations based on input
}
...
}
5 Dynamic Force Application
So far, the program has been able to simulate physical movements in a gravity environment, as well as collision monitoring with the ground. We can also dynamically apply force in any direction to an object, just one sentence:
Nxvec3 Gforcevec(0, 0 );//Global variable used to describe the force direction and size
Nxreal Gforcestrength= 150;
Gforcevec=Applyforcetoactor(Box,Nxvec3(0, 0, 1 ),Gforcestrength); // ZForce direction
In this way, the code for processing the buttons is obvious:
Switch(I)
{
// Force controls
Case'I ':{Gforcevec=Applyforcetoactor(Box,Nxvec3(0, 0, 1 ),Gforcestrength);Break;}
Case'K ':{Gforcevec=Applyforcetoactor(Box,Nxvec3(0, 0,-1 ),Gforcestrength);Break;}
Case'J ':{Gforcevec=Applyforcetoactor(Box,Nxvec3(1, 0, 0 ),Gforcestrength);Break;}
Case'L ':{Gforcevec=Applyforcetoactor(Box,Nxvec3(-1, 0, 0 ),Gforcestrength);Break;}
Case'U ':{Gforcevec=Applyforcetoactor(Box,Nxvec3(0, 0 ),Gforcestrength);Break;}
Case'M ':{Gforcevec=Applyforcetoactor(Box,Nxvec3(0,-1, 0 ),Gforcestrength);Break;}
// Return box to (0, 5, 0)
Case'T ':{Box->Setglobalposition(Nxvec3(0, 5, 0 ));Break;}
}
6 Draw objects
Currently, all object information is locatedActor. So we need to get allActor. Draw them in cycles.
Void Renderactors(Bool Shadows)
{
Nxu32 Nbactors=Gscene->Getnbactors(); // Number
Nxactor**Actors=Gscene->Getactors(); // ActorArray
While(Nbactors--)
{
Nxactor*Actor= *Actors++;
Drawactor(Actor);
If(Shadows)//Process shadow
{
Drawactorshadow(Actor);
}
}
}
7 , Cleaning
Void releasenx ()
{
If (gscene)
{
Getphysicsresults ();//Make sure noFetchresultsWaiting for running results
Gphysicssdk-> releasescene (* gscene );
}
If (gphysicssdk) gphysicssdk-> release ();
}
Void resetnx ()//Reset
{
Releasenx ();
Initnx ();
}
End
I think it is more important to understand the workflow of a physical engine than to understand the details.PhysxThe code of the graphic part included in the example is also very interesting, but the focus is not that.
I think I still don't quite understand it. The information of the entire program object is stored inSENCEInActorIncluding the final rendering.ActorIf the geometric objects in the game are complicated and many are used, I think it is not appropriate. Of course, this is only the first contact.Physx, There should be a better way to organize data.
For the first time, errors are inevitable. please correct me. Thank you!
2006-10-18