void PxWorld::_releaseQueues() { AssertFatal( mScene, "PhysXWorld::_releaseQueues() - The scene is null!" ); // We release joints still pending in the queue // first as they depend on the actors. for ( S32 i = 0; i < mReleaseJointQueue.size(); i++ ) { NxJoint *currJoint = mReleaseJointQueue[i]; mScene->releaseJoint( *currJoint ); } // All the joints should be released, clear the queue. mReleaseJointQueue.clear(); // Now release any actors still pending in the queue. for ( S32 i = 0; i < mReleaseActorQueue.size(); i++ ) { NxActor *currActor = mReleaseActorQueue[i]; bool isStatic = !currActor->isDynamic(); mScene->releaseActor( *currActor ); if ( isStatic ) PhysicsStatic::smDeleteSignal.trigger(); } // All the actors should be released, clear the queue. mReleaseActorQueue.clear(); // Now release any cloth still pending in the queue. for ( S32 i = 0; i < mReleaseClothQueue.size(); i++ ) { NxCloth *currCloth = mReleaseClothQueue[i]; mScene->releaseCloth( *currCloth ); } // All the actors should be released, clear the queue. mReleaseClothQueue.clear(); // Release heightfields that don't still have references. for ( S32 i = 0; i < mReleaseHeightFieldQueue.size(); i++ ) { NxHeightField *currHeightfield = mReleaseHeightFieldQueue[i]; if ( currHeightfield->getReferenceCount() == 0 ) { gPhysicsSDK->releaseHeightField( *currHeightfield ); mReleaseHeightFieldQueue.erase_fast( i ); i--; } } }
void PxWorld::releaseActor( NxActor &actor ) { AssertFatal( &actor.getScene() == mScene, "PhysXWorld::releaseActor() - Bad scene!" ); // Clear the userdata. actor.userData = NULL; // If the scene is not simulating then we have the // write lock and can safely delete it now. if ( !mIsSimulating ) { bool isStatic = !actor.isDynamic(); mScene->releaseActor( actor ); if ( isStatic ) PhysicsStatic::smDeleteSignal.trigger(); } else mReleaseActorQueue.push_back( &actor ); }
void PhysicsLib::updateVisualization(const VC3 &cameraPosition, float range, bool forceUpdate) { bool visualizeCollisionShapes = data->featureMap[PhysicsLib::VISUALIZE_COLLISION_SHAPES]; bool visualizeDynamic = data->featureMap[PhysicsLib::VISUALIZE_DYNAMIC]; bool visualizeStatic = data->featureMap[PhysicsLib::VISUALIZE_STATIC]; bool visualizeCollisionContacts = data->featureMap[PhysicsLib::VISUALIZE_COLLISION_CONTACTS]; bool visualizeFluids = data->featureMap[PhysicsLib::VISUALIZE_FLUIDS]; bool visualizeJoints = data->featureMap[PhysicsLib::VISUALIZE_JOINTS]; bool visualizeCCD = data->featureMap[PhysicsLib::VISUALIZE_CCD]; if (forceUpdate || visualizeCollisionShapes || visualizeDynamic || visualizeStatic || visualizeCollisionContacts || visualizeFluids || visualizeJoints || visualizeCCD) { // (do the update) } else { // do not unnecessarily do this stuff! return; } float rangeSq = range * range; int actorAmount = data->scene->getNbActors(); NxActor **actorArray = data->scene->getActors(); if(visualizeCollisionShapes || visualizeStatic || visualizeCollisionContacts) data->sdk->setParameter(NX_VISUALIZE_COLLISION_SHAPES, 1); else data->sdk->setParameter(NX_VISUALIZE_COLLISION_SHAPES, 0); if(visualizeDynamic) data->sdk->setParameter(NX_VISUALIZE_BODY_MASS_AXES, 1); else data->sdk->setParameter(NX_VISUALIZE_BODY_MASS_AXES, 0); data->sdk->setParameter(NX_VISUALIZE_CONTACT_NORMAL, visualizeCollisionContacts); data->sdk->setParameter(NX_VISUALIZE_CONTACT_FORCE, visualizeCollisionContacts); data->sdk->setParameter(NX_VISUALIZE_CONTACT_POINT, visualizeCollisionContacts); data->sdk->setParameter(NX_VISUALIZE_COLLISION_SKELETONS, visualizeCCD); for(int i = 0; i < actorAmount; ++i) { NxActor *actor = actorArray[i]; if(!actor) continue; NxVec3 nxpos = actor->getGlobalPosition(); VC3 pos(nxpos.x, nxpos.y, nxpos.z); VC3 diff = (pos - cameraPosition); diff.y = 0; // ignore height bool inRange = false; if (diff.GetSquareLength() < rangeSq) inRange = true; if(actor->isDynamic()) { //if(visualizeDynamic && inRange) if(visualizeDynamic) actor->raiseBodyFlag(NX_BF_VISUALIZATION); else actor->clearBodyFlag(NX_BF_VISUALIZATION); } int shapeAmount = actor->getNbShapes(); NxShape *const*shapes = actor->getShapes(); while(shapeAmount--) { NxShape *shape = shapes[shapeAmount]; if(actor->isDynamic()) { //if(visualizeCollisionShapes && inRange) if(visualizeCollisionShapes) shape->setFlag(NX_SF_VISUALIZATION, true); else shape->setFlag(NX_SF_VISUALIZATION, false); } else { if(visualizeStatic && !shape->isHeightField() && inRange) shape->setFlag(NX_SF_VISUALIZATION, true); else shape->setFlag(NX_SF_VISUALIZATION, false); } } } }
void FPxSceneMain::Tick( FLOAT DeltaTime ) { pxguard(FPxSceneMain::Tick); if( mScene ) { //mCharManager->updateControllers(); // // Kinematic actors move unreal actors // /*NxActor** actors = mScene->getActors(); int nbActors = mScene->getNbActors(); while(nbActors--) { NxActor* actor = *actors++; if( actor->userData && actor->isDynamic() && actor->readBodyFlag( NX_BF_KINEMATIC ) ) { AActor* aptr = (AActor*)actor->userData; // update location and rotation FCheckResult Hit(1.0f); NxMat34 m = actor->getGlobalPose(); aptr->GetLevel()->MoveActor( aptr, FVector(0,0,0), ToRotator(m.M), Hit ); aptr->GetLevel()->FarMoveActor( aptr, ToFVS(m.t), 0, 1 ); } }*/ // // Unreal actors move kinematic actors // /*NxActor** actors = mScene->getActors(); int nbActors = mScene->getNbActors(); while(nbActors--) { NxActor* actor = *actors++; if( actor->userData && actor->isDynamic() && actor->readBodyFlag( NX_BF_KINEMATIC ) ) { AActor* aptr = (AActor*)actor->userData; // update location and rotation NxMat34 m = ToMat34(aptr->Rotation,aptr->Location); actor->setGlobalPose(m); } }*/ // // Dynamic actors move unreal actors // NxActor** actors = mScene->getActors(); int nbActors = mScene->getNbActors(); while(nbActors--) { NxActor* actor = *actors++; if( actor->userData && actor->isDynamic() && !actor->readBodyFlag( NX_BF_KINEMATIC ) ) { AActor* aptr = (AActor*)actor->userData; // update location and rotation FCheckResult Hit(1.0f); NxMat34 m = actor->getGlobalPose(); aptr->GetLevel()->FarMoveActor( aptr, ToFVS(m.t) ); aptr->GetLevel()->MoveActor( aptr, FVector(0,0,0), ToRotator(m.M), Hit ); } } // // TODO: optimize timing // // Minimum acceptable framerate is 5fps if( DeltaTime > 0.2f ) { DeltaTime = 0.2f; } mTime += DeltaTime; while( /*!mSimulating &&*/ mTime >= PX_TIMESTEP ) { mTime -= PX_TIMESTEP; mScene->simulate(PX_TIMESTEP); mScene->flushStream(); mScene->fetchResults(NX_RIGID_BODY_FINISHED, true); //mSimulating = true; } //renderScene(); /*if( mSimulating && mScene->checkResults(NX_RIGID_BODY_FINISHED, false) ) { mScene->fetchResults(NX_RIGID_BODY_FINISHED, true); mSimulating = false; //modifyScene(DeltaTime); }*/ // get debug info if( GPxPhysics.mDrawPhysX ) mDebugRen = mScene->getDebugRenderable(); } unguard; }
void render() { static Timer t; if(!gMyPhysX.isPaused()) { for (NxU32 i = 0; i < gKinematicActors.size(); i++) { NxActor* actor = gKinematicActors[i].actor; NxVec3 pos = actor->getGlobalPosition(); pos += gKinematicActors[i].vel * 1.f/60.f; actor->moveGlobalPosition(pos); } } gMyPhysX.simulate(t.elapsed_time()); t.reset(); // Clear buffers glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glColor4f(0.5,0.9,0.5,1.0); DrawSkyBox(SKYEXTENTS); drawPlane(SKYEXTENTS); // Keep physics & graphics in sync for (NxU32 pass = 0; pass < 2; pass++) { int nbActors = gMyPhysX.getScene()->getNbActors(); NxActor** actors = gMyPhysX.getScene()->getActors(); actors += nbActors; while(nbActors--) { NxActor* actor = *--actors; float size; bool isTrigger = false; bool isKinematic = actor->isDynamic() && actor->readBodyFlag(NX_BF_KINEMATIC); NxVec3 color; NxF32 alpha = 1; if (actor->isDynamic()) { if (actor->readBodyFlag(NX_BF_KINEMATIC)) { color.set(1,0,0); } else { color.set(0,1,0); } } else { color.set(0.2f,0.2f,0.2f); } if (*(int *)(&actor->userData) < 0) { NxI32 triggerNumber = -(*(NxI32 *)(&actor->userData)); NxI32 triggerIndex = triggerNumber - 1; // This is our trigger isTrigger = true; size = 10.0f; color.z = gNbTouchedBodies[triggerIndex] > 0.5f ? 1.0f:0.0f; alpha = 0.5f; if (pass == 0) continue; } else { // This is a normal object size = float(*(int *)(&actor->userData)); if (pass == 1) continue; } float glmat[16]; glPushMatrix(); actor->getGlobalPose().getColumnMajor44(glmat); glMultMatrixf(glmat); glColor4f(color.x, color.y, color.z, 1.0f); glutSolidCube(size*2.0f); glPopMatrix(); // Handle shadows if( !isTrigger) { glPushMatrix(); const static float ShadowMat[]={ 1,0,0,0, 0,0,0,0, 0,0,1,0, 0,0,0,1 }; glMultMatrixf(ShadowMat); glMultMatrixf(glmat); glDisable(GL_LIGHTING); glColor4f(0.1f, 0.2f, 0.3f, 1.0f); glutSolidCube(size*2.0f); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); glEnable(GL_LIGHTING); glPopMatrix(); } } } }