コード例 #1
0
// Release one of the shapes from our compound. At the moment
// we are just releasing the first object in the array.
// As long as the index is below the number of shapes (uiNumShapes)
// it should work, but be careful about releasing an object inside
// other objects (for performance reasons)
void ReleaseRandomShape()
{
	// Get the array of pointers to shapes
	NxShape*const* ppShapeArray = mainBox->getShapes();

	// Find out how many shapes there are left
	NxU32 uiNumShapes = mainBox->getNbShapes();

	// Don't remove the last shape (or we'll have a shapeless actor)
	if (uiNumShapes < 2)
		return;

	// Record the pose of the shape we intend to release, for later
	NxShape* pShapeToRelease = ppShapeArray[0];
	NxMat34 mNewBoxPose = pShapeToRelease->getGlobalPose();

	// Release the object, and make a note of the new total number of objects
	mainBox->releaseShape(*pShapeToRelease);
	--iNumberObjects;

	// We need to update the mass and intertial tensors. We choose to base it
	// on the density of the objects, which are all fBoxDensity
	mainBox->updateMassFromShapes(fBoxDensity, 0.0f);

	// Create the new box, in the pose we recorded earlier
	CreateBoxPiece(mNewBoxPose);
}
コード例 #2
0
void CPhysicsManager::DrawActor (NxActor* actor, CRenderManager* render)
{

	CPhysicUserData* physicUserData = NULL;
	physicUserData =(CPhysicUserData*)actor->userData;
	//Si está petando aquí quiere decir que se ha registrado un objeto físico sin proporcionarle UserData
	assert(physicUserData);
	if( !physicUserData->GetPaint())
	{
		return;
	}

	NxShape*const* shapes = actor->getShapes();
	NxU32 nShapes = actor->getNbShapes();

	nShapes = actor->getNbShapes();
	while (nShapes--)
	{
		switch(shapes[nShapes]->getType())
		{
		case NX_SHAPE_PLANE:
			{
				CColor color = physicUserData->GetColor();
				float distance = shapes[nShapes]->isPlane()->getPlane().d;
				NxVec3 normal =  shapes[nShapes]->isPlane()->getPlane().normal;
				Vect3f n(normal.x,normal.y,normal.z);
				render->DrawPlane(100.f, n, distance,color,40,40);
			}
			break;
		case NX_SHAPE_BOX:
			{
				NxF32 m_aux[16];
				shapes[nShapes]->getGlobalPose().getColumnMajor44(m_aux);
				Mat44f m( m_aux[0], m_aux[4], m_aux[8], m_aux[12], 
					m_aux[1], m_aux[5], m_aux[9], m_aux[13], 
					m_aux[2], m_aux[6], m_aux[10], m_aux[14], 
					m_aux[3], m_aux[7], m_aux[11], m_aux[15]);

				render->SetTransform(m);
				NxVec3 boxDim = shapes[nShapes]->isBox()->getDimensions();
				CColor color = physicUserData->GetColor();
				render->DrawCube(Vect3f(boxDim.x*2,boxDim.y*2,boxDim.z*2), color);
				//render->DrawCube(boxDim.y*2,color);
			}
			break;
		case NX_SHAPE_SPHERE:
			{
				NxF32 m_aux[16];
				shapes[nShapes]->getGlobalPose().getColumnMajor44(m_aux);
				Mat44f m(	m_aux[0], m_aux[4], m_aux[8], m_aux[12], 
					m_aux[1], m_aux[5], m_aux[9], m_aux[13], 
					m_aux[2], m_aux[6], m_aux[10], m_aux[14], 
					m_aux[3], m_aux[7], m_aux[11], m_aux[15]);

				render->SetTransform(m);
				NxReal radius = shapes[nShapes]->isSphere()->getRadius();
				CColor color = physicUserData->GetColor();
				render->DrawSphere(radius,MAX_ARISTAS,color);
			}
			break;
		case NX_SHAPE_CAPSULE:
			{
				NxF32 m_aux[16];
				shapes[nShapes]->getGlobalPose().getColumnMajor44(m_aux);
				Mat44f m(	m_aux[0], m_aux[4], m_aux[8], m_aux[12], 
					m_aux[1], m_aux[5], m_aux[9], m_aux[13], 
					m_aux[2], m_aux[6], m_aux[10], m_aux[14], 
					m_aux[3], m_aux[7], m_aux[11], m_aux[15]);

				Mat44f translation, total;
				translation.SetIdentity();
				render->SetTransform(m);

				const NxReal & radius = shapes[nShapes]->isCapsule()->getRadius();
				const NxReal & height = shapes[nShapes]->isCapsule()->getHeight();
				CColor color = physicUserData->GetColor();
				translation.Translate(Vect3f(0.f, (height*0.5f), 0.f));

				total = m * translation;
				render->SetTransform(total);
				render->DrawSphere(radius,MAX_ARISTAS,color);
				translation.Translate( Vect3f(0.f, -(height*0.5f), 0.f ));
				total = m * translation;
				render->SetTransform(total);
				render->DrawSphere(radius, MAX_ARISTAS, color);			}
			break;
		case NX_SHAPE_CONVEX:

			break;
		case NX_SHAPE_MESH:
			{
				NxShape* mesh = shapes[nShapes];

				NxTriangleMeshDesc meshDesc;
				mesh->isTriangleMesh()->getTriangleMesh().saveToDesc(meshDesc);

				typedef NxVec3 Point;
				typedef struct _Triangle { NxU32 p0; NxU32 p1; NxU32 p2; } Triangle;

				NxU32 nbVerts = meshDesc.numVertices;
				NxU32 nbTriangles = meshDesc.numTriangles;

				Point* points = (Point *)meshDesc.points;
				Triangle* triangles = (Triangle *)meshDesc.triangles;

				CColor color = physicUserData->GetColor();
				NxF32 m_aux[16];
				mesh->getGlobalPose().getColumnMajor44(m_aux);
				Mat44f m(	m_aux[0], m_aux[4], m_aux[8], m_aux[12], 
					m_aux[1], m_aux[5], m_aux[9], m_aux[13], 
					m_aux[2], m_aux[6], m_aux[10], m_aux[14], 
					m_aux[3], m_aux[7], m_aux[11], m_aux[15]);

				render->SetTransform(m);

				Vect3f a,b,c;
				while(nbTriangles--)
				{
					a = Vect3f(points[triangles->p0].x, points[triangles->p0].y,points[triangles->p0].z);
					b = Vect3f(points[triangles->p1].x, points[triangles->p1].y,points[triangles->p1].z);
					c = Vect3f(points[triangles->p2].x, points[triangles->p2].y,points[triangles->p2].z);

					render->DrawLine(a, b, color);
					render->DrawLine(b, c, color);
					render->DrawLine(c, a, color);
					triangles++;

				}
			}
			break;
		case NX_SHAPE_WHEEL:
			{
				//TODO...
			}
			break;
		default:
			{
				//TODO...
			}
			break;
		}
	}
}