Lecture 14 - Collision Detection
Today we'll:
- Clean up graphics pipeline for better understanding
- Basic Spatial queries, collision detection and response
- Rendering Effects (brief)
- Circle back on Animations
Our current labs are:
- Choose from 3 selections of labs, pick 2:
- Particle System
- Boids (crowd behavior)
- Frame-buffer object (image-based effects, afterprocessing)
- Two mandatory final-project check-ins
- The last two weeks
- We'll demo on Final's week on Tuesday
- Week 10 -> midterm on Thursday
- Primarily on lighting and camera
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:
And then use:
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
See the following for the matrix:
![[Copy of Graphics pipeline - week 9.pptx.pdf#page=11]]
Notice that:
- If the near plane is
, then we get that everything just moves to to the origin (or onto a line) - If the far plane
then we get that our coordinate goes to infty, so we get a flat plane of everything on the screen (a look-down view)
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