Esempio n. 1
0
VehicleCloning::VehicleCloning(hkDemoEnvironment* env, hkBool createWorld)
:	hkDefaultPhysicsDemo(env)
{
	m_bootstrapIterations = 200;

	m_track = HK_NULL;
	m_numWheels = 4;
	
	m_landscapeContainer = HK_NULL;
	setUpWorld();

	if (!createWorld)
	{
		return;
	}

	m_world->lock();

	const VehicleCloningVariant& variant =  g_variants[m_variantId];

	m_vehicles.setSize( variant.m_numVehicles );

	// Create a vehicle to use as a template for cloning other vehicles
	hkpPhysicsSystem* vehicleSystem = createVehicle();


	// Clone the vehicle and add the clones to the world

	hkArray<hkAabb> spawnVolumes;
	spawnVolumes.pushBack(hkAabb(hkVector4(-95, 2, -95), hkVector4(95, 8, 95)));

	AabbSpawnUtil spawnUtil( spawnVolumes );

	for ( int i = 0; i < variant.m_numVehicles; ++i )
	{
		hkpPhysicsSystem* newVehicleSystem = vehicleSystem->clone();

		hkVector4 objectSize( 4, 4, 4 );
		hkVector4 position; 
		spawnUtil.getNewSpawnPosition( objectSize, position );

		newVehicleSystem->getRigidBodies()[0]->setPosition(position);
		m_vehicles[i] = static_cast<hkpVehicleInstance*>(newVehicleSystem->getActions()[0]);
		m_vehicles[i]->addReference();
		m_world->addPhysicsSystem( newVehicleSystem );
		newVehicleSystem->removeReference();
	}

	createWheels(variant.m_numVehicles);

	vehicleSystem->removeReference();

	m_world->unlock();
}
MultipleCharacterRbsDemo::MultipleCharacterRbsDemo(hkDemoEnvironment* env)
:	hkDefaultPhysicsDemo(env)
{

	// Setup the graphics
	{
		forceShadowState(false);
	}

	// Create the world
	{
		hkpWorldCinfo info;
		info.setBroadPhaseWorldSize( 350.0f );  
		info.m_gravity.set(0, -9.8f, 0);
		info.m_contactPointGeneration = hkpWorldCinfo::CONTACT_POINT_ACCEPT_ALWAYS;
		info.m_collisionTolerance = 0.1f; // faster, but better 0.01f for accuracy
		m_world = new hkpWorld( info );
		m_world->lock();

		hkpAgentRegisterUtil::registerAllAgents(m_world->getCollisionDispatcher());

		setupGraphics();
	}

	// Create the ground using the landscape repository
	{
		int landscapeIndex = LandscapeRepository::getInstance().getLandscapeIndexByName("simpleFlatLandOneMetreTris");
		m_landscapeContainer = LandscapeRepository::getInstance().getLandscape(landscapeIndex);

		hkpRigidBodyCinfo groundInfo;
		groundInfo.m_motionType = hkpMotion::MOTION_FIXED;
		groundInfo.m_shape = m_landscapeContainer->m_shape;

		hkpRigidBody* groundbody = new hkpRigidBody(groundInfo);

		m_world->addEntity(groundbody)->removeReference();

		setupDefaultCameras( env, m_landscapeContainer->m_cameraFrom, m_landscapeContainer->m_cameraTo, hkVector4(0,1,0) );
		
	}

	AabbSpawnUtil spawnUtil( m_landscapeContainer->m_spawnVolumes );
	

	//	Create character rigid bodies 

	const int numCharacters = 100;

	{
		m_characterRigidBodies.setSize( numCharacters );

		// Create a capsule to represent the character standing
		hkVector4 top(0, .4f, 0);
		hkVector4 bottom(0,-.4f, 0);		

		hkpShape* characterShape = new hkpCapsuleShape(top, bottom, .6f);

		// Construct characters
		for (int i=0; i < numCharacters; i++)
		{

			// Construct a character rigid body
			hkpCharacterRigidBodyCinfo info;
			info.m_mass = 100.0f;
			info.m_shape = characterShape;

			info.m_maxForce = 2000.0f;
			info.m_up = UP;
			spawnUtil.getNewSpawnPosition( hkVector4(1, 2, 1), info.m_position );

			info.m_maxSlope = 90.0f * HK_REAL_DEG_TO_RAD;

			m_characterRigidBodies[i] = new hkpCharacterRigidBody( info );
			m_world->addEntity( m_characterRigidBodies[i]->getRigidBody() );

		}
		characterShape->removeReference();
	}
	
	// Create the Character state machine and context
	hkpCharacterStateManager* manager;
	{
		hkpCharacterState* state;
		manager = new hkpCharacterStateManager();

		state = new hkpCharacterStateOnGround();
		manager->registerState( state,	HK_CHARACTER_ON_GROUND);
		state->removeReference();

		state = new hkpCharacterStateInAir();
		manager->registerState( state,	HK_CHARACTER_IN_AIR);
		state->removeReference();

		state = new hkpCharacterStateJumping();
		manager->registerState( state,	HK_CHARACTER_JUMPING);
		state->removeReference();

		state = new hkpCharacterStateClimbing();
		manager->registerState( state,	HK_CHARACTER_CLIMBING);
		state->removeReference();

	}

	// Create a context for each character
	{
		m_characterContexts.setSize(numCharacters);

		for (int i=0; i < numCharacters; i++)
		{
			m_characterContexts[i] = new hkpCharacterContext(manager, HK_CHARACTER_ON_GROUND);
			m_characterContexts[i]->setCharacterType(hkpCharacterContext::HK_CHARACTER_RIGIDBODY);
		}
		manager->removeReference();
	}
	
	//Initialized the round robin counter
	m_tick = 0;

	m_world->unlock();
}
Esempio n. 3
0
UnevenTerrainVsDemo::UnevenTerrainVsDemo(hkDemoEnvironment* env)
:	hkDefaultPhysicsDemo(env)
{

	// Setup the graphics
	{
		forceShadowState(false);
	}

	// Create the world
	{
		hkpWorldCinfo info;
		info.setBroadPhaseWorldSize( 350.0f );  
		info.m_gravity.set(0, -9.8f, 0);
		info.m_contactPointGeneration = hkpWorldCinfo::CONTACT_POINT_ACCEPT_ALWAYS;
		info.m_collisionTolerance = m_options.m_worldCollisionTolerance;
		m_world = new hkpWorld( info );
		m_world->lock();

		hkpAgentRegisterUtil::registerAllAgents(m_world->getCollisionDispatcher());

		setupGraphics();
	}

	// Setup layer collision
	{
		// Replace filter
		hkpGroupFilter* groupFilter = new hkpGroupFilter();

		// We disable collisions between characters
		groupFilter->disableCollisionsBetween(LAYER_CHARACTER_PROXY, LAYER_CHARACTER_RIGIDBODY);
		m_world->setCollisionFilter( groupFilter, true);
		groupFilter->removeReference();
	}

	// Create the ground using the landscape repository
	{
		int landscapeIndex = LandscapeRepository::getInstance().getLandscapeIndexByName("simpleFlatLandOneMetreTris");
		m_landscapeContainer = LandscapeRepository::getInstance().getLandscape(landscapeIndex);

		hkpRigidBodyCinfo groundInfo;
		groundInfo.m_motionType = hkpMotion::MOTION_FIXED;
		groundInfo.m_shape = m_landscapeContainer->m_shape;

		hkpRigidBody* groundbody = new hkpRigidBody(groundInfo);

		m_world->addEntity(groundbody)->removeReference();

		setupDefaultCameras( env, m_landscapeContainer->m_cameraFrom, m_landscapeContainer->m_cameraTo, hkVector4(0,1,0) );

	}

	AabbSpawnUtil spawnUtil( m_landscapeContainer->m_spawnVolumes );

	const hkReal characterMass = 100.0f;
	
	hkpShape* characterShape;
	{
		// Create a capsule to represent the character standing
		hkVector4 top( 0, m_options.m_characterCylinderHeight * 0.5f, 0 );
		hkVector4 bottom( 0, -m_options.m_characterCylinderHeight * 0.5f, 0 );		

		characterShape = new hkpCapsuleShape( top, bottom, m_options.m_characterRadius );
	}

	{
		// Construct a character rigid body
		hkpCharacterRigidBodyCinfo info;
		info.m_mass = characterMass;
		info.m_shape = characterShape;

		info.m_up = UP;
		info.m_position = characterStart;
		info.m_collisionFilterInfo = hkpGroupFilter::calcFilterInfo( LAYER_CHARACTER_RIGIDBODY, 0 );
		info.m_supportDistance = m_options.m_rbSupportDistance;
		info.m_hardSupportDistance = m_options.m_rbHardSupportDistance;
		info.m_allowedPenetrationDepth = m_options.m_rbAllowedPenetrationDepth;

		m_characterRigidBody = new hkpCharacterRigidBody( info );

		hkpCharacterRigidBodyListener* listener = new hkpCharacterRigidBodyListener();
		m_characterRigidBody->setListener( listener );
		listener->removeReference();

		m_world->addEntity( m_characterRigidBody->getRigidBody() );

		characterShape->removeReference();
	}

	// Create the Character state machine and context
	{
		hkpCharacterStateManager* manager;
		hkpCharacterState* state;
		manager = new hkpCharacterStateManager();

		static_cast<hkpCharacterStateOnGround*>( state = new hkpCharacterStateOnGround() )->setSpeed( m_options.m_characterWalkSpeed );
		manager->registerState( state,	HK_CHARACTER_ON_GROUND);
		state->removeReference();

		state = new hkpCharacterStateInAir();
		manager->registerState( state,	HK_CHARACTER_IN_AIR);
		state->removeReference();

		state = new hkpCharacterStateJumping();
		manager->registerState( state,	HK_CHARACTER_JUMPING);
		state->removeReference();

		state = new hkpCharacterStateClimbing();
		manager->registerState( state,	HK_CHARACTER_CLIMBING);
		state->removeReference();

		m_characterContext = new hkpCharacterContext(manager, HK_CHARACTER_ON_GROUND);
		m_characterContext->setCharacterType(hkpCharacterContext::HK_CHARACTER_RIGIDBODY);
		manager->removeReference();
	}

	//	Create a character proxy object
	{
		// Construct a shape phantom
		m_phantom = new hkpSimpleShapePhantom( characterShape, hkTransform::getIdentity(), hkpGroupFilter::calcFilterInfo( LAYER_CHARACTER_PROXY, 0 ) );

		// Add the phantom to the world
		m_world->addPhantom(m_phantom);
		m_phantom->removeReference();

		// Construct a character proxy
		hkpCharacterProxyCinfo cpci;
		cpci.m_position = characterStart;
		cpci.m_staticFriction = 0.0f;
		cpci.m_dynamicFriction = 1.0f;
		cpci.m_up = UP;
		cpci.m_userPlanes = 4;
		
		cpci.m_shapePhantom = m_phantom;
		m_characterProxy = new hkpCharacterProxy( cpci );
	}

	// Create the character proxy state machine and context
	{
		hkpCharacterState* state;
		hkpCharacterStateManager* manager = new hkpCharacterStateManager();

		static_cast<hkpCharacterStateOnGround*>( state = new hkpCharacterStateOnGround() )->setSpeed( m_options.m_characterWalkSpeed );
		manager->registerState( state,	HK_CHARACTER_ON_GROUND);
		state->removeReference();

		state = new hkpCharacterStateInAir();
		manager->registerState( state,	HK_CHARACTER_IN_AIR);
		state->removeReference();

		state = new hkpCharacterStateJumping();
		manager->registerState( state,	HK_CHARACTER_JUMPING);
		state->removeReference();

		state = new hkpCharacterStateClimbing();
		manager->registerState( state,	HK_CHARACTER_CLIMBING);
		state->removeReference();

		m_characterProxyContext = new hkpCharacterContext(manager, HK_CHARACTER_ON_GROUND);
		manager->removeReference();
	}

	//Initialized the round robin counter
	m_tick = 0;
	m_unsupportedFramesCountRb = 0;
	m_unsupportedFramesCountProxy = 0;

	m_filterRigidBody.m_framesInAir = m_options.m_clientStateFiltering;
	m_filterProxy.m_framesInAir = m_options.m_clientStateFiltering;

	m_world->unlock();
}
Esempio n. 4
0
ObjectsOnLandscapeDemo::ObjectsOnLandscapeDemo(hkDemoEnvironment* env)
:	hkDefaultPhysicsDemo(env)
{
	m_bootstrapIterations = 200;

	{
		hkpWorldCinfo info;
		m_world = new hkpWorld( info );
		m_world->lock();

		// Register ALL agents (though some may not be necessary)
		hkpAgentRegisterUtil::registerAllAgents(m_world->getCollisionDispatcher());
	}

	const ObjectsOnLandscapeVariant& variant =  g_variants[m_variantId];

	// Increase the stack size.
	m_oldStack = hkThreadMemory::getInstance().getStack();
	m_stackBuffer = hkAllocate<char>( MY_STACK_SIZE, HK_MEMORY_CLASS_DEMO );
	hkThreadMemory::getInstance().setStackArea( m_stackBuffer, MY_STACK_SIZE );

	//
	// Create the ground using the landscape repository
	//
	{
		int landscapeIndex = LandscapeRepository::getInstance().getLandscapeIndexByName(variant.m_landscapeType);
		m_landscapeContainer = LandscapeRepository::getInstance().getLandscape(landscapeIndex);

		hkpRigidBodyCinfo groundInfo;
		groundInfo.m_motionType = hkpMotion::MOTION_FIXED;
		groundInfo.m_shape = m_landscapeContainer->m_shape;

		hkpRigidBody* groundbody = new hkpRigidBody(groundInfo);

		m_world->addEntity(groundbody)->removeReference();
	}

	m_packfileData = HK_NULL;

		// Create a utility for generating positions for objects
	AabbSpawnUtil spawnUtil( m_landscapeContainer->m_spawnVolumes );

	if (variant.m_numObjects > 100)
	{
		spawnUtil.m_allowOverlaps = true;
	}
//	m_env->m_window->getViewport(0)->toggleState(HKG_ENABLED_WIREFRAME);


	switch (variant.m_addObjectType)
	{
	case ADD_CONVEX:
		{
			for ( int i = 0; i < variant.m_numObjects; ++i )
			{
				hkVector4 objectSize(	spawnUtil.m_pseudoRandomGenerator.getRandRange(.5f, 2),
										spawnUtil.m_pseudoRandomGenerator.getRandRange(.5f, 2),
										spawnUtil.m_pseudoRandomGenerator.getRandRange(.5f, 2));

				hkVector4 position; 
				spawnUtil.getNewSpawnPosition( objectSize, position );
				hkReal density = 1000;

				hkpRigidBody* rb = GameUtils::createRandomConvexGeometricFromBox(objectSize, 
																				objectSize(0) * objectSize(1) * objectSize(2) * density, // mass
																				position, 
																				30, // num verts
																				&spawnUtil.m_pseudoRandomGenerator);
				m_world->addEntity(rb);
				rb->removeReference();
			}
			break;
		}
	case ADD_COMPOUND:
		{
			hkString fileName = hkAssetManagementUtil::getFilePath("Resources/Physics/Compoundbodies/compoundbodies.hkx");
			hkIstream infile( fileName.cString() );
			HK_ASSERT( 0x215d080c, infile.isOk() );
			hkPackfileReader* reader = new hkBinaryPackfileReader();
			reader->loadEntireFile(infile.getStreamReader());

			if( hkVersionUtil::updateToCurrentVersion( *reader, hkVersionRegistry::getInstance() ) != HK_SUCCESS )
			{
				HK_ASSERT2(0, 0, "Couldn't update version.");
			}
			m_packfileData = reader->getPackfileData();
			m_packfileData->addReference();

			hkRootLevelContainer* container = static_cast<hkRootLevelContainer*>( reader->getContents( hkRootLevelContainerClass.getName() ) );
			HK_ASSERT2(0xa6451543, container != HK_NULL, "Could not load root level obejct" );

			hkpPhysicsData* physicsData = static_cast<hkpPhysicsData*>( container->findObjectByType( hkpPhysicsDataClass.getName() ) );
			HK_ASSERT2(0xa6451544, physicsData != HK_NULL, "Could not find physics data in root level object" );

			delete reader;

			const hkArray<hkpRigidBody*>& rigidBodies = physicsData->getPhysicsSystems()[0]->getRigidBodies();

			for ( int i = 0; i < variant.m_numObjects; ++i )
			{
				hkpRigidBody* newBody = rigidBodies[i % rigidBodies.getSize()]->clone();
				hkQuaternion rotation; spawnUtil.m_pseudoRandomGenerator.getRandomRotation( rotation );
				newBody->setRotation( rotation );

				hkAabb aabb;  newBody->getCollidable()->getShape()->getAabb(newBody->getTransform(), .05f, aabb );
				hkVector4 objectSize; objectSize.setSub4( aabb.m_max, aabb.m_min );
				hkVector4 position; spawnUtil.getNewSpawnPosition( objectSize, position );
				
				newBody->setPosition( position );

				m_world->addEntity( newBody );
				newBody->removeReference();
			}

			break;
		}
	case ADD_RAGDOLL:
		{

			//
			// Create a group filter to remove collisions between ragdoll bones
			//
			{
				hkpGroupFilter* filter = new hkpGroupFilter();
				hkpGroupFilterSetup::setupGroupFilter( filter );
				m_world->setCollisionFilter( filter );
				filter->removeReference();
			}



			const hkReal height = 2.0f;
			hkVector4 objectSize(height, height, height);

			for ( int i = 0; i < variant.m_numObjects; ++i )
			{
				hkVector4 position; 
				spawnUtil.getNewSpawnPosition( objectSize, position );

				hkQuaternion rotation; rotation.setIdentity();

				GameUtils::RagdollCinfo ragdollInfo;

				ragdollInfo.m_position = position;
				ragdollInfo.m_height = height;
				spawnUtil.m_pseudoRandomGenerator.getRandomRotation(ragdollInfo.m_rotation);
				ragdollInfo.m_systemNumber = i + 1;
				ragdollInfo.m_boneShapeType = GameUtils::RPT_CAPSULE;
				ragdollInfo.m_numVertebrae = 1;
				ragdollInfo.m_ragdollInterBoneCollisions = GameUtils::DISABLE_ONLY_ADJOINING_BONE_COLLISIONS;
				ragdollInfo.m_wantHandsAndFeet = GameUtils::WANT_HANDS_AND_FEET;

				hkpPhysicsSystem* ragdoll = GameUtils::createRagdoll( ragdollInfo );

				const hkArray<hkpRigidBody*>& rigidbodies = ragdoll->getRigidBodies();
				for( int iRB = 0; iRB < rigidbodies.getSize(); iRB++ )
				{
					hkpRigidBody* body = rigidbodies[iRB];
					body->setLinearDamping( 0.1f );
					body->getMaterial().setFriction(0.31f);				
				}

				m_world->addPhysicsSystem(ragdoll);
				ragdoll->removeReference();
				
			}
			break;
		}
	}

	//
	// Setup the camera
	//
	{
		hkVector4 up  (   0.0f,   1.0f, 0.0f );
		setupDefaultCameras( env, m_landscapeContainer->m_cameraFrom, m_landscapeContainer->m_cameraTo, up );
		
		setupGraphics();
	}

	m_world->unlock();
}