Beispiel #1
0
// This runs once every physics timestep.
void update(float dt)
{
	// This section just checks to make sure the object stays within a certain boundary. This is not really collision detection.
	glm::vec3 tempPos = obj2->GetPosition();
	
	if (fabsf(tempPos.x) > 0.9f)
	{
		glm::vec3 tempVel = obj2->GetVelocity();

		// "Bounce" the velocity along the axis that was over-extended.
		obj2->SetVelocity(glm::vec3(-1.0f * tempVel.x, tempVel.y, tempVel.z));
	}
	if (fabsf(tempPos.y) > 0.8f)
	{
		glm::vec3 tempVel = obj2->GetVelocity();
		obj2->SetVelocity(glm::vec3(tempVel.x, -1.0f * tempVel.y, tempVel.z));
	}

	// Rotate the objects if you'd like, this will show you how the AABB stays aligned on the X and Y axes regardless of the object's orientation.
	//obj1->Rotate(glm::vec3(glm::radians(0.0f), glm::radians(0.0f), glm::radians(1.0f)));
	//obj2->Rotate(glm::vec3(glm::radians(0.0f), glm::radians(0.0f), glm::radians(1.0f)));

	// Re-calculate the Axis-Aligned Bounding Box for your object.
	// We do this because if the object's orientation changes, we should update the bounding box as well.
	// Be warned: For some objects this can actually cause a collision to be missed, so be careful.
	// (This is because we determine the time of the collision based on the AABB, but if the AABB changes significantly, the time of collision can change between frames,
	// and if that lines up just right you'll miss the collision altogether.)
	obj1->CalculateAABB();
	obj2->CalculateAABB();

	if (TestAABB(obj1->GetAABB(), obj2->GetAABB()))
	{
		// Create a local velocity variable based off of the moving object's velocity.
		glm::vec3 velocity = obj2->GetVelocity();

		// Reverse the velocity in the x direction
		// This is the "bounce" effect, only we don't actually know the axis of collision from the test. Instead, we assume it because the object is only moving in the x 
		// direction.
		velocity.x *= -1;

		obj2->SetVelocity(velocity);
	}

	obj1->Update(dt);
	obj2->Update(dt);

	// Update your MVP matrices based on the objects' transforms.
	MVP = PV * *obj1->GetTransform();
	MVP2 = PV * *obj2->GetTransform();
}
Beispiel #2
0
		bool TestObjects(Object* O1, Object* O2){

			//Determine which algorithm we need
			int Complexity;
			if(O1->GetComplexity() > O2->GetComplexity()){
				Complexity = O1->GetComplexity();
			}else{
				Complexity = O2->GetComplexity();
			}

			//use the appropriate algorithm
			bool collision = false;
			if(Complexity == COMP_AABB){
				//typecast to our more specific AABB structure.
				collision = TestAABB((AABB*)O1, (AABB*)O2);
			}

			return collision;
		}