Lecture 14 - Collision Detection

Today we'll:

Our current labs are:

Simple Collision Detection (Overview)

Every object has a bounding sphere, which contains:

struct 
{
	glm::vec3 center,  // center point in (x,y,z)
	float radius       // radius for the size of the object, r
};

We can determine if two objects like this are colliding if:

if distance(c1, c2) < r1 + r2:
	# colliding

To get the center point based on the object, take the average of the glm::max and glm::min vectors. To get the radius, take the largest distance between the center and each corner (really the corners are symmetrical so you only need to check 2).

You could alternatively calculate the Center Of Mass by finding the average of all the vertices of our mesh:

COM=vinumber of v’s

And then use:

ri=max(COM,all other vertices)

Movement

If you want to restrict the movement of each of the boids (smaller creatures), you can define a structure that defines the object's velocity:

struct physics {
	glm::vec3 velocity,   // update pos with velocity * deltaTime (based on frame times)
	glm::vec3 pos
};

void update(float t) {
	physics obj;
	pos += t * obj->velocity;
}

When we collide, we may want to inhibit movements only for certain directions:

void whenCollideEdges()
{
	if(obj.x < leftwall)
	{
		vel.x *= -1; // if we hit the left wall, flip the x velocity
	}
}

Looking at the Labs in More Detail

See the slideshow below to generate ideas for your final project:

![[Some things - week8.pptx.pdf]]

Projection Matrix

We haven't talked about having object coordinates and camera coordinates, but at the top we need to talk about the perspective matrix. We call glmPerspective and put it on our Projection->pushMatrix().

The call here is:

glmPerspective(field_of_view, aspect_ratio, near, far)

Why do we do this? We want to have things in a perspective view to have closer things look closer, and farther things get "shrunk" down:

How do we generate this? Use similar triangles. The farther away something is, the projected y gets smaller as z gets bigger:

See the following for the matrix:

![[Copy of Graphics pipeline - week 9.pptx.pdf#page=11]]

Notice that:

Here the window transform on slide 16 was the box from [[Lecture 3 - Simulate the World Now#Writing Software to Move to OpenGL]]! Notice what l and r and so on correlate with there...