Esempio n. 1
0
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;
}
Esempio n. 2
0
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();
			}
		}
	}
}