Ejemplo n.º 1
0
void C_NEORagdoll::OnDataChanged( DataUpdateType_t type )
{
	BaseClass::OnDataChanged( type );

	if ( type == DATA_UPDATE_CREATED )
	{
		CreateNEORagdoll();

		AngularImpulse angularVel( 0, 0, 0 );

		m_pPhysicsObject->AddVelocity( &m_vecRagdollVelocity.Get(), &angularVel );
	}
}
void WorldRayCastMultithreadedDemo::createBodies()
{
	hkpRigidBodyCinfo rigidBodyInfo;
	rigidBodyInfo.m_collisionFilterInfo = hkpGroupFilter::calcFilterInfo(1, 1);
	hkPseudoRandomGenerator rand(100);


	// Note: with a SPU MOPP cache of 32768 bytes we can currently go to 811 objects and still get the broadphase onto SPU.
	for( int i = 0; i < 100; i++)
	{
		// All bodies created below are movable
		rigidBodyInfo.m_motionType = hkpMotion::MOTION_SPHERE_INERTIA;

		// A collection of 100 rigid bodies is randomly created by generating an integer in the range(0,4) and choosing
		// one of the following shapes; MOPP, Convex Vertices, Box, Sphere or Triangle depending on the outcome:
		int shapeType = (int) (rand.getRandRange(0,1) * 5);
		switch(shapeType)
		{
			case 0:
			// Create MOPP body
			{
				hkpMoppBvTreeShape* shape = createMoppShape();
				rigidBodyInfo.m_shape = shape;
				break;
			}

			// The construction of each of these is quite similar and for the purposes of this tutorial we will just outline
			// that of the Convex Vertices object, the code for which is presented below.
			// Create ConvexVertices body
			case 1:
			{
				// Data specific to this shape.
				int numVertices = 4;

					// 16 = 4 (size of "each float group", 3 for x,y,z, 1 for padding) * 4 (size of float)
				int stride = 16;

				float vertices[] = { // 4 vertices plus padding
					-2.0f, 1.0f, 1.0f, 0.0f, // v0
					 1.0f, 2.0f, 0.0f, 0.0f, // v1
					 0.0f, 0.0f, 3.0f, 0.0f, // v2
					 1.0f, -1.0f, 0.0f, 0.0f  // v3
				};
				

				hkpConvexVerticesShape* shape;
				hkArray<hkVector4> planeEquations;
				hkGeometry geom;
				{
					hkStridedVertices stridedVerts;
					{
						stridedVerts.m_numVertices = numVertices;
						stridedVerts.m_striding = stride;
						stridedVerts.m_vertices = vertices;
					}

					hkGeometryUtility::createConvexGeometry( stridedVerts, geom, planeEquations );

					{
						stridedVerts.m_numVertices = geom.m_vertices.getSize();
						stridedVerts.m_striding = sizeof(hkVector4);
						stridedVerts.m_vertices = &(geom.m_vertices[0](0));
					}

					shape = new hkpConvexVerticesShape(stridedVerts, planeEquations);
				}

				rigidBodyInfo.m_shape = shape;
				break;
			}

			// Create Box body
			case 2:
			{
				// Data specific to this shape.
				hkVector4 halfExtents = hkVector4(1.0f, 2.0f, 3.0f);
				hkpBoxShape* shape = new hkpBoxShape(halfExtents );
				rigidBodyInfo.m_shape = shape;
				break;
			}

			// Create Sphere body
			case 3:
			{
				hkReal radius = rand.getRandRange(0.5f, 2.0f);
				hkpConvexShape* shape = new hkpSphereShape(radius);
				rigidBodyInfo.m_shape = shape;		
				break;
			}

			// Create Triangle body
			case 4:
			{
				hkVector4 a(-1.5f, -1.5f,  0.0f);
				hkVector4 b(1.5f, -1.5f,  0.0f);
				hkVector4 c(0.0f,  1.5f,  0.0f);

				hkpTriangleShape* shape = new hkpTriangleShape(a, b, c);
				rigidBodyInfo.m_shape = shape;
				break;
			}
		}	// end case

		// As usual we fill out the hkpRigidBodyCinfo 'blueprint' for the rigidbody, with the code above specifying
		// the necessary information for the 'm_shape' member. To create a hkpConvexVerticesShape we need a set of vertices and
		// we must generate a set of plane equations from these points. As you can see from the code this is all performed 
		// prior to instantiating the shape.

		// Fake Inertia tensor for simplicity, assume it's a unit cube
		{
			hkVector4 halfExtents(0.5f, 0.5f, 0.5f);
			hkReal mass = 10.0f;
			hkpMassProperties massProperties;
			hkpInertiaTensorComputer::computeBoxVolumeMassProperties(halfExtents, mass, massProperties);

			rigidBodyInfo.m_inertiaTensor = massProperties.m_inertiaTensor;
			rigidBodyInfo.m_centerOfMass = massProperties.m_centerOfMass;
			rigidBodyInfo.m_mass = massProperties.m_mass;			
		}	

		// The object is then assigned a random position, orientation and angular velocity and added to the world:

		rigidBodyInfo.m_position.set( rand.getRandRange(-10.0f, 10.0f), rand.getRandRange(-10.0f, 10.0f), rand.getRandRange(0.0f, -40.0f));
		rand.getRandomRotation( rigidBodyInfo.m_rotation );

		rigidBodyInfo.m_collisionFilterInfo = hkpGroupFilterSetup::LAYER_DEBRIS;

		hkpRigidBody* rigidBody = new hkpRigidBody(rigidBodyInfo);
		rigidBodyInfo.m_shape->removeReference();

		// Give them an initial velocity
		hkVector4 angularVel(rand.getRandRange(-1.0f, 1.0f), rand.getRandRange(-1.0f, 1.0f), rand.getRandRange(-1.0f, 1.0f));
		rigidBody->setAngularVelocity(angularVel);
		rigidBody->setAngularDamping(0.0f);

		m_world->addEntity(rigidBody);
		rigidBody->removeReference();
	}
}
void OptimizedWorldRaycastDemo::createBodies()
{
	hkpRigidBodyCinfo rigidBodyInfo;
	rigidBodyInfo.m_collisionFilterInfo = hkpGroupFilter::calcFilterInfo(1, 1);
	hkPseudoRandomGenerator rand(100);

	hkArray<hkpEntity*> bodyArray;
	bodyArray.reserve(m_numRigidBodies);

	hkpShape* shape;
	{
		hkVector4 halfExtents(0.5f, 0.5f, 0.5f);
		shape = new hkpBoxShape( halfExtents, 0.0f );
	}

	for( int i = 0; i < m_numRigidBodies; i++)
	{
		// All bodies created below are movable
		rigidBodyInfo.m_motionType = hkpMotion::MOTION_SPHERE_INERTIA;

		// A collection of many rigid bodies is randomly created using a hkpBoxShape
		rigidBodyInfo.m_shape = shape;

		// As usual we fill out the hkpRigidBodyCinfo 'blueprint' for the rigidbody, with the code above specifying
		// the necessary information for the 'm_shape' member. To create a hkpConvexVerticesShape we need a set of vertices and
		// we must generate a set of plane equations from these points. As you can see from the code this is all performed 
		// prior to instantiating the shape.

		// Fake Inertia tensor for simplicity, assume it's a unit cube
		{
			hkReal mass = 10.0f;
			hkReal d = mass * 0.5f;
			rigidBodyInfo.m_inertiaTensor.setDiagonal( d,d,d );
			rigidBodyInfo.m_mass = mass;			
		}	

		// The object is then assigned a random position, orientation and angular velocity and added to the world:

		rigidBodyInfo.m_position.set(	rand.getRandRange(-1.f*m_worldSizeX, 1.f*m_worldSizeX),
										rand.getRandRange(-1.f*m_worldSizeY, 1.f*m_worldSizeY),
										rand.getRandRange(-1.f*m_worldSizeZ, 1.f*m_worldSizeZ));
		rand.getRandomRotation( rigidBodyInfo.m_rotation );

		rigidBodyInfo.m_collisionFilterInfo = hkpGroupFilterSetup::LAYER_DEBRIS;

		hkpRigidBody* rigidBody = new hkpRigidBody(rigidBodyInfo);

		// Give them an intial velocity
		hkVector4 angularVel(rand.getRandRange(-1.0f, 1.0f), rand.getRandRange(-1.0f, 1.0f), rand.getRandRange(-1.0f, 1.0f));
		rigidBody->setAngularVelocity(angularVel);
		rigidBody->setAngularDamping(0.0f);

		bodyArray.pushBack( rigidBody );

		// There is no gravity vector for this world and so the bodies will appear to float in space.
	}
	shape->removeReference();

	// Batch add all bodies to the system and defragment the broadphase
	m_world->addEntityBatch( bodyArray.begin(), bodyArray.getSize() );
	m_world->getBroadPhase()->defragment();
		
	//
	//	Remove all references to bodies. They are now referenced by m_world
	//
	{
		for ( int i = 0; i < bodyArray.getSize(); i++ )
		{
			bodyArray[i]->removeReference();
		}
	}
}