예제 #1
0
void DemoKeeper::BrickWallDemo( void )
{
	mWorld->markForWrite();

	//
	// Create the walls
	//

	hkVector4 groundPos( 0.0f, 0.0f, 0.0f );
	hkVector4 boxSize( 1.0f, 1.0f, 1.0f);
	hkReal deltaZ = 25.0f;
	groundPos(2) = -deltaZ * 10 * 0.5f;

	for ( int y = 0; y < 4; y ++ )			// first wall
	{
		createBrickWall( 4, 8, groundPos, 0.2f, boxSize );
		groundPos(2) += deltaZ;
	}

	mWorld->unmarkForWrite();
}
예제 #2
0
void GameScreen::createExplosion(glm::vec3 const& position, float size, float duration, bool removeBlocks)
{
	// Add new explosion
	explosions.push_back(Explosion(position, glm::vec2(size), duration));
	// Limit explosion sounds to one per frame
	if (explosionsThisFrame < 1)
	{
		game->getSoundManager()->playSound("Sounds/bombexplosion.mp3", position, 30.f);
	}
	explosionsThisFrame++;

	// Find all bombs within the explosion radius
	Bomb::id_set nearbyBombs;
	glm::vec2 groundPos(glm::swizzle<glm::X, glm::Z>(position));
	getNearbyBombs(groundPos, size * 0.5f, nearbyBombs);
	BOOST_FOREACH(Bomb::id const& id, nearbyBombs)
	{
		// Only deal with the players own bombs
		if (id.first == myID)
		{
			Bomb& bomb = myEntities[id.second];
			if (bomb.isAlive())
			{
				Packet::ptr packet(new Packet13RemoveBomb(myID, id.second, true));
				client->write(packet);

				bomb.setIsAlive(false);
			}
		}
	}
	
	// Blow up blocks
	if (removeBlocks)
	{
		float blockExplosionRadius = size * 0.8f;

		glm::vec2 bPos = groundPos - glm::vec2(0.5f);

		glm::ivec2 minPos(glm::floor(bPos - glm::vec2(blockExplosionRadius)));
		glm::ivec2 maxPos(glm::ceil(bPos + glm::vec2(blockExplosionRadius)));

		minPos = glm::max(minPos, 0);
		maxPos = glm::min(maxPos, blockMap.getSize() - glm::ivec2(1));

		std::vector<glm::ivec2> blocks;

		// For every block in a square...
		for (int x = minPos.x; x <= maxPos.x; x++)
		{
			for (int y = minPos.y; y <= maxPos.y; y++)
			{
				glm::ivec2 oPos(x, y);

				Block::ptr block = blockMap.getBlock(oPos);			

				if (block && block->isDestructible() && glm::distance(glm::vec2(oPos), bPos) < blockExplosionRadius)
				{
					blocks.push_back(oPos);
				}
			}
		}

		// If any block to remove was found...
		if (!blocks.empty())
		{
			Packet::ptr packet(new Packet14RemoveBlocks(blocks));
			client->write(packet);
		}
	}

	// Give score for every explosion close to the opponents base
	if (glm::distance(position, opponentBasePos) < size * 0.5f)
	{
		scoreThisFrame += BASE_POINTS_PER_BOMB;
	}
}
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 );


}
예제 #4
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();
}