基本步骤:
主要的代码如下:
//------------------------------------------------------------------------------ void TestPhysXApp::OnProcessInput() { // TODO: add your input operation code here if (this->inputServer->GetDefaultKeyboard()->KeyDown(Input::Key::Return)) { NxBodyDesc bodyDesc; bodyDesc.angularDamping = 0.5f; NxBoxShapeDesc boxDesc; boxDesc.dimensions = NxVec3(0.5f, 0.5f, 0.5f); NxActorDesc actorDesc; actorDesc.shapes.pushBack(&boxDesc); actorDesc.body = &bodyDesc; actorDesc.density = 10.0f; actorDesc.globalPose.t = NxVec3(0.0f, 10.0f, 0.0f); this->colors.Append(this->GetRandomColor()); this->nxScene->createActor(actorDesc)->userData = (void*)(this->colors.Size() - 1); } ViewerApplication::OnProcessInput(); } //------------------------------------------------------------------------------ void TestPhysXApp::OnUpdateFrame() { // simulation float frameTime = (float)this->GetFrameTime(); this->nxScene->simulate(frameTime); float curTime = (float) this->GetTime(); // draw ground const float period = 3.0f; float now = float(curTime - int(curTime / period) * period - period * 0.5f); scalar s = n_abs(now / (period * 0.5f)); float4 color = float4::lerp(float4(1, 0, 0, 1), float4(0, 1, 0, 1), s); matrix44 transform = matrix44::identity(); transform.translate(float4(0, -1, 0, 0)); transform.scale(float4(100, 1, 100, 1)); DebugShapeRenderer::Instance()->DrawBox(transform, color); // draw boxes NxActor** actors = this->nxScene->getActors(); SizeT numActors = this->nxScene->getNbActors(); for (IndexT i = 1; i < numActors; ++i) { NxActor* actor = actors[i]; actor->getGlobalPose().getColumnMajor44((float*)&transform); IndexT colorIndex = (IndexT)actor->userData; DebugShapeRenderer::Instance()->DrawBox(transform, this->colors[colorIndex]); } ViewerApplication::OnUpdateFrame(); // fetch simulation results this->nxScene->flushStream(); this->nxScene->fetchResults(NX_RIGID_BODY_FINISHED, true); } //------------------------------------------------------------------------------ void TestPhysXApp::InitNx() { // intialize sdk NxPhysicsSDKDesc desc; this->nxSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION, NULL, NULL, desc); this->nxSDK->setParameter(NX_SKIN_WIDTH, 0.05f); n_assert(NULL != this->nxSDK); // create scene NxSceneDesc sceneDesc; sceneDesc.gravity = NxVec3(0.0f, -9.8f, 0.0f); this->nxScene = this->nxSDK->createScene(sceneDesc); n_assert(NULL != this->nxScene); // set default material NxMaterial* defaultMaterial = this->nxScene->getMaterialFromIndex(0); defaultMaterial->setRestitution(0.0f); defaultMaterial->setStaticFriction(0.5f); defaultMaterial->setDynamicFriction(0.5f); // create ground plane NxPlaneShapeDesc planeDesc; NxActorDesc actorDesc; actorDesc.shapes.pushBack(&planeDesc); this->nxScene->createActor(actorDesc); } //------------------------------------------------------------------------------ float4 TestPhysXApp::GetRandomColor() const { float r = n_rand(0.0f, 1.0f); float g = n_rand(0.0f, 1.0f); float b = n_rand(0.0f, 1.0f); return float4(r, g, b, 1.0f); }