Physx2.8 tutorial 1: Start NVIDIA physx

Source: Internet
Author: User

From: http://gputoaster.wordpress.com/2010/10/16/tutorial-1-kick-start-nvidia-physx/

 

Let's start

Main content of this article:

1) Create physx Context

2) initialize the graphic environment graphics environment

3) destroy physx

 

1) initialize physx SDK

Before using physxapi, you must initialize the SDK and obligatory. The steps are as follows:

Create an nxpysicssdk object

SDK objects are the foundation of everything. If SDK Initialization is not completed, all subsequent operations on physx objects can only fail.

Nxphysicssdk * Pphysics; // Declare globally
Pphysics = Nxcreatephysicssdk (nx_physics_sdk_version );
If ( ! Pphysics)
Return ;

 

 

Create an nxscenedesc object

The scenario includes the following properties: bodies, restraints, and reducers.

They interact with each other. The scenario is responsible for simulating the object state over time.

Multiple scenarios can exist at the same time, with the condition that all scenarios are part of a large scenario.

Nxvec3 defaultgravity ( 0 . F, - 9.8f , 0 . F ); // Declare somewhere in the global scope

Nxscenedesc scenedesc;
Scenedesc. simtype = Nx_simulation_hw;
Scenedesc. Gravity = Defaultgravity;

 

The nxscenedesc struct has a lot of content. Each item can be considered as a descriptor, which contains information required to create nxscene.

You can use the pushback () method to add the descriptor object to the list of object descriptions.

Physx descriptor defines the nature that will affect the entire physx calculation, behavior behaviour and parameter parameters.

SimtypeThe descriptor defines that physical simulation may be a hardware simulation (nx_simulation_hw) or a software simulation (nx_simulation_sw ). Hardware simulation requires the support of hardware PPU units. NVIDIA geforce 8800gt and above are supported. ATI radeon GPU is also supported.

GravityThe descriptor defines the gravity parameter, which is a three-dimensional vector value. The Y-direction component is-9.8, M/s.

(Note: physx seems to be the left-side coordinate system by default. + X + Y + z screen inwards)

In addition, there are many other descriptors. For more details, see physx.

 

Create nxscene object

The nxscenedesc object created above is one-time and valid only when the scenario object is created.

To create an object, call the createscene () method of the SDK object with the nxscenedesc object as the parameter.

Nxscene * Pscene; // Declare globally (recommended)
Pscene = Pphysics -> Createscene (scenedesc );

If ( ! pscene)
{< br> scenedesc. simtype = nx_simulation_sw;
scenedesc. gravity = defaultgravity;
pscene = pphysics -> createscene (scenedesc );
If ( ! pscene) return ;< br>

}

The hardware does not support hardware simulation when the SDK object is created. Therefore, the pphysics object is null, which causes pscene creation failure to be null.

Therefore, if it is null after the first creation, the SDK object is created and pscene is created in the form of a software model.

Or you don't need to worry about this. When creating the SDK, you can determine how the hardware supports PPU.

If (Gphysicssdk -> Gethwversion () >   0 )
Scenedesc. simtype = Nx_simulation_hw;
Else

Scenedesc. simtype = nx_simulation_sw;


Start Simulation

With the nxscene object, you can use the nxscene: Simulate () method to start the simulation.

This method receives a parameter indicating the time interval.

By calling simulate (), the physx engine performs a series of internal operations and calculations to obtain the scenario after the time interval.

Void Startphysics ( Void )
{
Nxreal currenttime = Gettime ();
Pscene -> Simulate (gdeltatime );
Pscene -> Flushstream ();

}

The startphysics () method simulates physx to the current time. Gettime () returns an nxreal (float type), indicating the current time.

Flushstream (), refresh all the commands in the cache before returning the simulation results to ensure physx has completed the SET command.

 

2) initialize the graphic environment

 

Physx is independent from the graphic environment. Where do you like (OpenGL, d3d, Orge, OSG) to use its simulation result data level.

3) destroy physx

 

If (Pscene)
{
...
Pphysics -> Releasescene ( * Pscene );
}

If(Pphysics)

Pphysics-> release ();


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.