void setupPhysics(hkpWorld* physicsWorld)
{
    //
    //  Create the ground box
    //
    {
        hkVector4 groundRadii( 70.0f, 2.0f, 140.0f );
        hkpConvexShape* shape = new hkpBoxShape( groundRadii , 0 );

        hkpRigidBodyCinfo ci;

        ci.m_shape = shape;
        ci.m_motionType = hkpMotion::MOTION_FIXED;
        ci.m_position = hkVector4( 0.0f, -2.0f, 0.0f );
        ci.m_qualityType = HK_COLLIDABLE_QUALITY_FIXED;

        physicsWorld->addEntity( new hkpRigidBody( ci ) )->removeReference();
        shape->removeReference();
    }

    hkVector4 groundPos( 0.0f, 0.0f, 0.0f );
    hkVector4 posy = groundPos;

    //
    // Create the walls
    //

    int wallHeight = 8;
    int wallWidth  = 8;
    int numWalls = 6;
    hkVector4 boxSize( 1.0f, 0.5f, 0.5f);
    hkpBoxShape* box = new hkpBoxShape( boxSize , 0 );
    box->setRadius( 0.0f );

    hkReal deltaZ = 25.0f;
    posy(2) = -deltaZ * numWalls * 0.5f;

    for ( int y = 0; y < numWalls; y ++ )			// first wall
    {
        createBrickWall( physicsWorld, wallHeight, wallWidth, posy, 0.2f, box, boxSize );
        posy(2) += deltaZ;
    }
    box->removeReference();

    //
    // Create a ball moving towards the walls
    //

    const hkReal radius = 1.5f;
    const hkReal sphereMass = 150.0f;

    hkVector4 relPos( 0.0f,radius + 0.0f, 50.0f );

    hkpRigidBodyCinfo info;
    hkpMassProperties massProperties;
    hkpInertiaTensorComputer::computeSphereVolumeMassProperties(radius, sphereMass, massProperties);

    info.m_mass = massProperties.m_mass;
    info.m_centerOfMass  = massProperties.m_centerOfMass;
    info.m_inertiaTensor = massProperties.m_inertiaTensor;
    info.m_shape = new hkpSphereShape( radius );
    info.m_position.setAdd4(posy, relPos );
    info.m_motionType  = hkpMotion::MOTION_BOX_INERTIA;

    info.m_qualityType = HK_COLLIDABLE_QUALITY_BULLET;


    hkpRigidBody* sphereRigidBody = new hkpRigidBody( info );
    g_ball = sphereRigidBody;

    physicsWorld->addEntity( sphereRigidBody );
    sphereRigidBody->removeReference();
    info.m_shape->removeReference();

    hkVector4 vel(  0.0f,4.9f, -100.0f );
    sphereRigidBody->setLinearVelocity( vel );


}
Example #2
0
BrickWallDemo::BrickWallDemo(hkDemoEnvironment* env)
:	hkDefaultPhysicsDemo(env)
{

	// Disable warnings:									
	hkError::getInstance().setEnabled(0xaf55adde, false); //'No runtime block of size 637136 currently available. Allocating new block from unmanaged memory.'


	hkDefaultPhysicsDemo::enableDisplayingToiInformation( true );
	m_frameToSimulationFrequencyRatio = 1.0f;
	m_timestep = 1.0f / 60.0f;

	const BrickWallVariant& variant =  g_variants[m_variantId];

	int wallHeight = variant.m_height;
	int wallWidth  = variant.m_width;
	int numWalls = variant.m_numWalls;
	hkpWorldCinfo::SimulationType simulationType;
	simulationType = hkpWorldCinfo::SIMULATION_TYPE_CONTINUOUS;
	
	//
	// Setup the camera so that we can see the ball hit the (first) wall.
	//
	{
		hkVector4 from(40.0f, 20.0f, 40.0f);
		hkVector4 to  ( 0.0f, 10.0f, 0.0f);
		hkVector4 up  ( 0.0f,  1.0f, 0.0f);
		setupDefaultCameras( env, from, to, up );
	}


	{

		hkpWorldCinfo info;
		info.setBroadPhaseWorldSize( 5000.0f );
		info.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_HARD);
		info.m_collisionTolerance = 0.01f;
		info.m_gravity = hkVector4( 0.0f,-9.8f,0.0f);
		info.m_simulationType = simulationType;
		info.m_broadPhaseBorderBehaviour = info.BROADPHASE_BORDER_FIX_ENTITY;
		//info.m_enableDeactivation = false;
		//info.m_enableSimulationIslands = false;
		info.m_enableDeactivation = false;
		m_world = new hkpWorld( info );
		m_world->lock();
	}
	{
		// Register ALL agents (though some may not be necessary)
		hkpAgentRegisterUtil::registerAllAgents(m_world->getCollisionDispatcher());

		setupGraphics();

		// enable instancing (if present on platform)
		hkpShapeDisplayViewer* shapeViewer = (hkpShapeDisplayViewer*)getLocalViewerByName( hkpShapeDisplayViewer::getName() );
		if (shapeViewer)
		{
			shapeViewer->setInstancingEnabled( true );
		}
	}




	//
	//  Create the ground box
	//
	{
		hkVector4 groundRadii( 70.0f, 2.0f, 70.0f );
		hkpConvexShape* shape = new hkpBoxShape( groundRadii , 0 );
		
		hkpRigidBodyCinfo ci;
		
		ci.m_shape = shape;
		ci.m_motionType = hkpMotion::MOTION_FIXED;
		ci.m_position = hkVector4( 0.0f, -2.0f, 0.0f );
		ci.m_qualityType = HK_COLLIDABLE_QUALITY_FIXED;
		
		m_world->addEntity( new hkpRigidBody( ci ) )->removeReference();
		shape->removeReference();
	}

	hkVector4 groundPos( 0.0f, 0.0f, 0.0f );
	hkVector4 posy = groundPos;
	
	//
	// Create the walls
	//
	if(1)
	{
		hkVector4 boxSize( 1.0f, 0.5f, 0.5f);
		hkpBoxShape* box = new hkpBoxShape( boxSize , 0 );
		box->setRadius( 0.0f );
		hkReal deltaZ = 25.0f;
		posy(2) = -deltaZ * numWalls * 0.5f; 

		for ( int y = 0; y < numWalls; y ++ )			// first wall
		{
			createBrickWall( m_world, wallHeight, wallWidth, posy, 0.2f, box, boxSize );
			posy(2) += deltaZ;
		}
		box->removeReference();
	}


	//
	// Create the ball
	//
	if (1)
	{
		const hkReal radius = 1.5f;
		const hkReal sphereMass = 150.0f;

		hkVector4 relPos( 0.0f,radius + 0.0f, 20.0f );

		hkpRigidBodyCinfo info;
		hkpMassProperties massProperties;
		hkpInertiaTensorComputer::computeSphereVolumeMassProperties(radius, sphereMass, massProperties);

		info.m_mass = massProperties.m_mass;
		info.m_centerOfMass  = massProperties.m_centerOfMass;
		info.m_inertiaTensor = massProperties.m_inertiaTensor;
		info.m_shape = new hkpSphereShape( radius );
		info.m_position.setAdd4(posy, relPos );
		info.m_motionType  = hkpMotion::MOTION_BOX_INERTIA;

		if ( variant.m_usePredictive)
		{
			info.m_qualityType = HK_COLLIDABLE_QUALITY_BULLET;
		}
		else
		{
			info.m_qualityType = HK_COLLIDABLE_QUALITY_DEBRIS;
		}

		hkpRigidBody* sphereRigidBody = new hkpRigidBody( info );

		m_world->addEntity( sphereRigidBody );
		sphereRigidBody->removeReference();
		info.m_shape->removeReference();

		hkVector4 vel(  0.0f,4.9f, -200.0f );
		sphereRigidBody->setLinearVelocity( vel );
	}

	m_world->unlock();
}