Posted on

Tutorial 2: PhysX Processing (NVIDIA PhysX)


“This tutorial is designed and explained for absolute beginners who have no knowledge of API programming in C/C++ and flexible in general laws of Physics.”

In the previous tutorial we have seen how to initialize PhysX. Apart from initializing and destroying the PhysX SDK, there are some functions that are required to be taken care off during the simulation. In this tutorial we are going to see how the physics is simulated given a rendering environment.

a) Query the SDK to see whether the simulation has finished.

To process the next frame of physics simulation, SDK needs to know whether the current frame physics simulation has finished. This helps prevent mixing of one or more physics simulation across the simulation frames. Mixing frames would result inappropriate animation.

Therefore you need to get the results of physics simulation before NxScene::simulate() is called again.

A quick question can arise: How come NxScene::simulate() is called again? Remember the graphics loop is imminent and so is the call to NxScene::simulate().


void GetPhysicsResult(void)
{
     while(!pScene->fetchResults(NX_RIGID_BODY_FINISHED, false));
}

When the code is executed the program checks the status of the simulation described by the first parameter and returns the buffer depending on the second parameter. The first parameter describes a certain part of the SDK that needs to be checked. The second part is toggle parameter which blocks the NxScene::fetchResults() to return when set to true i.e. it waits for the simulation to finish. if you dont want to wait (as in many situation) then set this parameter to false.

b) Applying force to the Actors.

At some point in time, actors may apply some type of force to each other. This force can be conventional, impulsive. Also the force can be linear or angular (Torque). To apply force you need to choose right force mode and calculate how much force should be applied  to the Actor.

PhysX force is defined in the global coordinate frame which acts at a local coordinates to the actor. In general the force is applied to the center of mass(CoM) of the actor. If the force acts other than the CoM then a corresponding Torque is also applied (If it doesn’t make sense here, don’t worry, its natural as Physics is not every one’s cup of tea. At this stage you must refer to some basics of physics).

You can apply force to the actor in the following way:

NxVec3 forceVector = forceStrength * forceDirection* timeQuantum;
actor->addForce(forceVec);

We calculate a force vector which comprises of

  • Strength: How much force is applied.
  • Direction of force: In which direction as force is vector.
  • A Time Quantum: If this parameter is skipped then an uncontrolled force will be applied per frame. Applying a time quantum will synchronize the simulation with the FPS.

c) Render the Actors.

Once you have finished creating Actors, it is now time to render/display. Ideally there may be more than one actor described for a scene. One way to render such a scene is by creating actors and render it on the screen immediately. This is ideal only when the scene contains a single actor. But we are talking about games where massive number of actors exist and there must exist an optimized way to render these actors. Take any example of present day game and you would see the degree at which the objects are rendered. PhysX on the other hand keeps track of the number of actors and the actors itself for a specific scene. NxScene::getNbActors() and NxScene::getActors() are the methods which come in handy. NxScene::getNbActors() returns an Integer which specifies the number of Actors present in a scene. NxScene::getActors() return a array of actor pointers(or actors).

To render these actors, loop through the array and pass the actor to the rendering logic. The sample code could look like:

Code:

void RenderActors(void)
{
    NxU32 nbActors = pScene->getNbActors();
    NxActor** actors = pScene->getActors();

    while(nbActors--)
    {
       NxActor* actor = *actors++;
       /*Draw Actor Logic goes here*/
    }
}

We are still to see how these actors get rendered on the display. This will be covered in the proceeding sections. To jump right away, see “How to render actors based on the actor’s shape(Tutorial unavailable at this moment)”.

Advertisements

About GPUToaster

https://gputoaster.wordpress.com/about

9 responses to “Tutorial 2: PhysX Processing (NVIDIA PhysX)

  1. Antonio ⋅

    Hi,
    interesting, but if I want calculate a force between two actors? in a collision?
    how I can do it?

  2. Actually, PhysX handles all the force or impact calculations itself, so you can see, for example, a ball bouncing when it is impacted against the terrain.

    I guess that’s what you mean in your question.
    Happy to help more.

    Regards,

    GPUToaster

  3. Antonio ⋅

    well,
    -I have two objects collide.
    -at the time of the collisions, I want to calculate the force.
    -for example, knowing the depth of penetration, you could calculate the force as F = k * x, maybe you can add the effect of a damper.
    -or with F = m * a, but there are two derivatives …
    then I would find the depth of penetration …
    can go well?
    thanks

    • Considering the material is linearly elastic, then the force of reaction force is linearly dependent on the penetration depth i.e. the deeper the penetration, the stronger is the reaction force. This is the kind of model PhysX follows, if I am not wrong. So if you want to pierce an object with larger k would produce a stronger reaction force making penetration difficult. PhysX automatically computes this depth, contact point and the unit normal of the two colliding objects. There is a method NxUserContactModify::onContactConstraint() with which you can play with. I have not used it till now, but no harm to experiment with. Also set ActorDescription.flags |= NX_AF_CONTACT_MODIFICATION or NX_NOTIFY_CONTACT_MODIFICATION to enable contact modification. Let me know if you could get any result.

      By the way, nice question. 🙂

      Regards.

  4. Frederic ⋅

    Great tutorial. I hope you’ll find time to add more info later.
    So does a PhysX actor also hold skinning information such that if a skinned actor gets a kick or bounces on a wall his legs/arms etc would move in a realistic way?

    • Hey Fredric,

      Thanks for your comment.
      About your question, Actors in PhysX forms the basis of simulation. Yes, you can attach all types of simulation information to the actors. In a broader sense, PhysX has no one type of Actor. Each one of them are treated differently by the engine. Once you start configuring (I mean attaching simulation rules) the actors, they will respond. Basically, you make those siluations with simple object shapes such as cube, sphere, cylinders, etc.
      Will try to post these methods in near future. Stay tuned.

      regards,
      GPUToaster.

  5. escorts nyc ⋅

    I’m really impressed with your writing skills and also with the layout on your weblog. Is this a paid theme or did you customize it yourself? Anyway keep up the excellent quality writing, it’s rare to see a
    great blog like this one these days.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s