Esempio n. 1
0
// Check our collisions and handle them accordingly
void Demo::privCheckCollisions(const float timeIn)
{
	PhysicsContact contact;
	contact.Reset();

	// Check bullet and ground
	if (CheckColliding(ground, bullet, contact))
	{
		// Handle collision
		contact.CalculateData(timeIn);
		contact.ChangeVelocity();
		contact.ChangePosition();
		contact.Reset();
	}

	// Check all bricks against ground
	for (int i = 0; i < NUM_BRICKS; i++)
	{ 
		if (CheckColliding(bricks[i], ground, contact))
		{
			// Handle collision
			contact.CalculateData(timeIn);
			contact.ChangeVelocity();
			contact.ChangePosition();
			contact.Reset();
		}
	}

	// Check all bricks against bullet
	for (int i = 0; i < NUM_BRICKS; i++)
	{ 
		if (CheckColliding(bullet, bricks[i], contact))
		{
			contact.CalculateData(timeIn);

			// Time to have some fun with all blocks within certain distance of this collision
			// Launch those bricks upward with random angular velocity
			for (int k = 0; k < NUM_BRICKS; k++)
			{
				// use mag squared to avoid square root
				Vect diffPos = bricks[k].position - contact.contactPoint;
				float magSquared = diffPos.magSqr();
				
				if (magSquared < 1500.0f && bricks[k].position[1] >= bricks[i].position[1])
				{
					Vect velocityChange(diffPos[0] > 0 ? 30.0f : -30.0f, 200.0f, 0.0f);
					bricks[k].velocity += velocityChange;

					static int x = 987444303;
					srand(x);
					x += 5134;

					// Random angular velocity
					Vect angVelocityChange(0.0f, 0.0f, 0.0f);
					angVelocityChange[0] = (float)(rand() % 60 - 30);
					angVelocityChange[1] = (float)(rand() % 60 - 30);
					angVelocityChange[2] = (float)(rand() % 60 - 30);

					bricks[k].angVelocity += angVelocityChange;
				}
			}

			// Slow time on this collision
			if (!this->timeSlowed)
			{
				this->slowTimer = 0.0f;
				this->timeSlowed = true;
				this->motionBlur.blurOn = true;
			}
			contact.Reset();
			bullet.active = false;
			break;
		}
	}

	// Now check all bricks with each other
	for (int i = 0; i < NUM_BRICKS; i++)
	{
		for (int j = i + 1; j < NUM_BRICKS; j++)
		{
			if (CheckColliding(bricks[i], bricks[j], contact))
			{
				// Handle collision accordingly
				contact.CalculateData(timeIn);
				contact.ChangeVelocity();
				contact.ChangePosition();
				contact.Reset();
			}
		}
	}

	return;
};