Wednesday, April 27, 2011

PhysX Basics Tutorial: Multiple Bouncing Box

In this tutorial, we will add multiple boxes to the scene. This tutorial assumes that you have successfully understood the basics of how to create an actor using the scene object. If not, u may want to review the previous tutorials as a refresher. Ok so now lets get started. There are a lot of things similar between this and the previous tutorial on the simple box. In fact, the only thing changed when we need to add multiple boxes is the InitializePhysX function. This function is given as follows,

void InitializePhysX() {
if(gPhysicsSDK == NULL) {
cerr<<"Error creating PhysX device."<<endl;
//Create the scene
NxSceneDesc sceneDesc;
sceneDesc.gravity.set(0.0f, -9.8f, 0.0f);

gScene = gPhysicsSDK->createScene(sceneDesc);

NxMaterial* defaultMaterial = gScene->getMaterialFromIndex(0);

gScene->setTiming(myTimestep / 4.0f, 4, NX_TIMESTEP_FIXED);

//Create actors
//1) Create ground plane
NxPlaneShapeDesc planeDesc;
NxActorDesc actorDesc, actorDesc2;


//2) Create cube
NxBodyDesc bodyDesc;
bodyDesc.angularDamping = 0.75f;
bodyDesc.linearVelocity = NxVec3(0,0,0);

NxBoxShapeDesc boxDesc;
size_t box_size = 1;
boxDesc.dimensions = NxVec3(box_size/2.0f, box_size/2.0f, box_size/2.0f);

actorDesc2.body = &bodyDesc;
actorDesc2.density = 1.0f;

We are repeating the same steps as we did in the last tutorial. The only thing that we need to do in case when we need multiple objects of the same type is that we need to create new actors using the scene object. All of this reuse the same actor descriptor object.

for(int i=0;i<10;i++) {
actorDesc2.globalPose.t = NxVec3(0.0f,5.0f+5*i,0.0f);

In the above lines, we loop 10 times. We generate a new y position using the loop variable and then assign this position to the global pose of the actor descriptor. Next we call the createActor function passing it the modified descriptor to create a new object for us.

That's it. The rest of the code remains the same since it will loop through all of the actors and determine their types to call the appropriate draw function as detailed in the last tutorial.

Running the code gives us the following output.
PhysX Multiple Bouncing Boxes

Source code of this tutorial


Popular Posts

Copyright (C) 2011 - Movania Muhammad Mobeen. Awesome Inc. theme. Powered by Blogger.