Exemplo n.º 1
0
static void CreateCube(const NxVec3& pos, int size=2, const NxVec3* initial_velocity=NULL, bool kinematic = false, bool Static = false)
{
	// Create body
	NxBodyDesc BodyDesc;
	BodyDesc.angularDamping	= 0.5f;
	//	BodyDesc.maxAngularVelocity	= 10.0f;
	if(initial_velocity)	BodyDesc.linearVelocity = *initial_velocity;

	NxBoxShapeDesc BoxDesc;
	BoxDesc.dimensions		= NxVec3(float(size), float(size), float(size));

	NxActorDesc ActorDesc;
	//	ActorDesc.userData		= (void*)size;
	ActorDesc.shapes.pushBack(&BoxDesc);
	if (!Static)
		ActorDesc.body			= &BodyDesc;
	ActorDesc.density		= 10.0f;
	ActorDesc.globalPose.t  = pos;
	ActorDesc.userData = (void*)size;

	NxActor* actor = gMyPhysX.getScene()->createActor(ActorDesc);

	if (kinematic) {
		KinematicActor k;
		k.actor = actor;
		actor->raiseBodyFlag(NX_BF_KINEMATIC);
		if (initial_velocity) {
			k.vel = *initial_velocity;
		} else {
			k.vel.set(0,0,0);
		}
		gKinematicActors.pushBack(k);
	}

}
Exemplo n.º 2
0
NxActor* CreateBoard(const NxVec3& pos)
{
	NxActor* actor = CreateBox(pos + NxVec3(0,0.5,0), NxVec3(1,0.25,0.5), 10);
	actor->raiseBodyFlag(NX_BF_FROZEN_ROT_X);
	actor->setAngularDamping(0.5);

	// Left wheel
	NxWheelDesc wheelDesc;
	//wheelDesc.wheelAxis.set(0,0,1);
	//wheelDesc.downAxis.set(0,-1,0);
    wheelDesc.wheelApproximation = 10;
	wheelDesc.wheelRadius = 0.5;
	wheelDesc.wheelWidth = 0.1;
	wheelDesc.wheelSuspension = 0.5;
//	wheelDesc.wheelSuspension = 1.0;
	wheelDesc.springRestitution = 7000;
	wheelDesc.springDamping = 0;
	wheelDesc.springBias = 0;  
//	wheelDesc.springRestitution = 20;
//	wheelDesc.springDamping = 0.5;
//	wheelDesc.springBias = 0; 
	wheelDesc.maxBrakeForce = 1;
	wheelDesc.frictionToFront = 0.1;
	wheelDesc.frictionToSide = 0.99;
	wheelDesc.position = NxVec3(1.5,0.5,0);
	wheelDesc.wheelFlags = NX_WF_USE_WHEELSHAPE | NX_WF_BUILD_LOWER_HALF | NX_WF_ACCELERATED | 
		                   NX_WF_AFFECTED_BY_HANDBRAKE | NX_WF_STEERABLE_INPUT;

    wheel1 = AddWheelToActor(actor, &wheelDesc);

	// Right wheel
	NxWheelDesc wheelDesc2;
	//wheelDesc2.wheelAxis.set(0,0,1);
	//wheelDesc2.downAxis.set(0,-1,0);
    wheelDesc2.wheelApproximation = 10;
	wheelDesc2.wheelRadius = 0.5;
	wheelDesc2.wheelWidth = 0.1;
	wheelDesc2.wheelSuspension = 0.5;
//	wheelDesc2.wheelSuspension = 1.0;  
	wheelDesc2.springRestitution = 7000;
	wheelDesc2.springDamping = 0;
	wheelDesc2.springBias = 0;
//	wheelDesc2.springRestitution = 20;
//	wheelDesc2.springDamping = 0.5;
//	wheelDesc2.springBias = 0;  
	wheelDesc2.maxBrakeForce = 1;
	wheelDesc2.frictionToFront = 0.1;
	wheelDesc2.frictionToSide = 0.99;
	wheelDesc2.position = NxVec3(-1.5,0.5,0);
	wheelDesc2.wheelFlags = NX_WF_USE_WHEELSHAPE | NX_WF_BUILD_LOWER_HALF | NX_WF_ACCELERATED | 
		                    NX_WF_AFFECTED_BY_HANDBRAKE | NX_WF_STEERABLE_INPUT;

    wheel2 = AddWheelToActor(actor, &wheelDesc2);

	return actor;
}
Exemplo n.º 3
0
void ParticleEmitter::addParticle()
{

	if ((_iTailIndex == _iHeadIndex) && (_iParticleCount != 0)) // FIFO is full
	{
		removeParticle();
		// Now there is a slot free
	}

	// Add a single-shape actor to the scene
	NxActorDesc actorDesc;
	NxBodyDesc bodyDesc;

	NxSphereShapeDesc sphereDesc;
	sphereDesc.radius = 1.0f;
	sphereDesc.group = cParticleCollisionGroup; // this group does not collide with itself
	actorDesc.shapes.pushBack(&sphereDesc);

	actorDesc.body = &bodyDesc;
	actorDesc.density = 10.0f;
	actorDesc.globalPose.t = _vStartingPos;

	NxActor* pNewActor =  _pScene->createActor(actorDesc);

	// Give it an initial linear velocity, scaled by _fStartingVelScale
	NxVec3 vRandVec(NxReal(rand()*_fStartingVelScale), NxReal(rand()*_fStartingVelScale), NxReal(rand()*_fStartingVelScale));
	pNewActor->setLinearVelocity(vRandVec);

	// Turn off gravity for smoke
	pNewActor->raiseBodyFlag(NX_BF_DISABLE_GRAVITY);

	_aParticleArray[_iHeadIndex] = new Particle(pNewActor, _vThermoDynamicAcceleration);

	_iHeadIndex = (_iHeadIndex+1) % ciMaxParticles;
	++_iParticleCount;
}
Exemplo n.º 4
0
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);
			}
		}
	}
}
Exemplo n.º 5
0
pRigidBody*pFactory::createBox(CK3dEntity *referenceObject,CK3dEntity *worldReferenceObject,pObjectDescr*descr,int creationFlags)
{

	pWorld *world=getManager()->getWorld(worldReferenceObject,referenceObject); 
	pRigidBody*result = world->getBody(referenceObject);
	if(result)
	{
		result->destroy();
		delete result;
		result = NULL;
	}
	// we create our final body in the given world :
	result  =  createBody(referenceObject,worldReferenceObject);
	if (result)
	{
		result->setWorld(world);

		using namespace vtTools::AttributeTools;

		result->setFlags(descr->flags);
		result->setHullType(descr->hullType);
		result->setDataFlags(0x000);
		result->checkDataFlags();

		if (result->getSkinWidth()==-1.0f)
		{
			result->setSkinWidth(0.01f);
		}

		VxMatrix v_matrix ;
		VxVector position,scale;		
		VxQuaternion quat;	


		v_matrix = referenceObject->GetWorldMatrix();
		Vx3DDecomposeMatrix(v_matrix,quat,position,scale);
		VxVector box_s= BoxGetZero(referenceObject);

		NxVec3 pos = pMath::getFrom(position);
		NxQuat rot = pMath::getFrom(quat);

		float density = result->getDensity();
		float radius = referenceObject->GetRadius();
		if (referenceObject->GetRadius() < 0.001f )
		{
			radius  = 1.0f;
		}


		//////////////////////////////////////////////////////////////////////////
		//create actors description
		NxActorDesc actorDesc;actorDesc.setToDefault();
		NxBodyDesc bodyDesc;bodyDesc.setToDefault();

		//////////////////////////////////////////////////////////////////////////
		NxMaterialDesc *materialDescr = NULL;
		NxMaterial *material = NULL;
		if (isFlagOn(result->getDataFlags(),EDF_MATERIAL_PARAMETER))
		{
			NxMaterialDesc  entMatNull;entMatNull.setToDefault();
			NxMaterialDesc  *entMat  = createMaterialFromEntity(referenceObject);
			material  = world->getScene()->createMaterial(entMatNull);
			material->loadFromDesc(*entMat);
			result->setMaterial(material);
		}else{
			if (world->getDefaultMaterial())
			{
				result->setMaterial(world->getDefaultMaterial());
			}
		}
		//////////////////////////////////////////////////////////////////////////

		NxBoxShapeDesc boxShape;
		if (creationFlags & E_OFC_DIMENSION )
		{
			boxShape.dimensions = pMath::getFrom(box_s)*0.5f;
		}
		boxShape.density  = descr->density;
		boxShape.materialIndex = result->getMaterial()->getMaterialIndex();
		if (result->getSkinWidth()!=-1.0f)
			boxShape.skinWidth = result->getSkinWidth();

		actorDesc.shapes.pushBack(&boxShape);

		//////////////////////////////////////////////////////////////////////////
		//dynamic object  ? 
		if (result->getFlags() & BF_Moving){
			actorDesc.body = &bodyDesc;
		}
		else
			actorDesc.body = NULL;

		//////////////////////////////////////////////////////////////////////////
		//set transformations
		actorDesc.density  = descr->density;

		if (creationFlags & E_OFC_POSITION)
		{
			actorDesc.globalPose.t = pos;	
		}

		if (creationFlags & E_OFC_POSITION)
		{
			actorDesc.globalPose.M = rot;
		}
		
				//////////////////////////////////////////////////////////////////////////
		//create the actor

		int v = actorDesc.isValid();
		NxActor *actor  = world->getScene()->createActor(actorDesc);
		if (!actor)
		{
			xLogger::xLog(ELOGERROR,E_LI_AGEIA,"Couldn't create actor");
			delete result;
			return NULL;

		}


		//////////////////////////////////////////////////////////////////////////
		//set additional settings : 
		result->setActor(actor);
		actor->setName(referenceObject->GetName());
		actor->userData= result;

		if (result->getFlags() & BF_Moving)
		{
			VxVector massOffsetOut;referenceObject->Transform(&massOffsetOut,&result->getMassOffset());
			actor->setCMassOffsetGlobalPosition(pMath::getFrom(massOffsetOut));
		}

		if (result->getFlags() & BF_Kinematic)
		{
			actor->raiseBodyFlag(NX_BF_KINEMATIC);
		}

		result->enableCollision((result->getFlags() & BF_Collision));
		if (result->getFlags() & BF_Moving)
		{
			if (!(result->getFlags() & BF_Gravity))
			{
				actor->raiseBodyFlag(NX_BF_DISABLE_GRAVITY);
			}
		}

		NxShape *shape  = result->getShapeByIndex();
		if (shape)
		{
			pSubMeshInfo *sInfo  = new pSubMeshInfo();
			sInfo->entID = referenceObject->GetID();
			sInfo->refObject  = (CKBeObject*)referenceObject;
			shape->userData = (void*)sInfo;
			result->setMainShape(shape);
			shape->setName(referenceObject->GetName());
		}
	}
		
	return result;
}