Tute: Collision detection and reaction

Download and test this base code and replace its vec2f with your own ("-lm" may need to be added to the Makefile).

Note that abs() and min/max functions on linux cast to an int. Use fabs() instead!

  1. Double check your vector functions evaluating, for example, the dot and cross product of: A=(1, 0) and B=(0, 1).
  2. Using the base code, create an x-axis aligned line segment near the top left of the screen. This will be drawn using GL_LINES and should behave the same way as the paddle: Add code to make the ball bounce off the line segment. Note that the ball does not bounce correctly at the line end points.

    For now we are not concerned with large timesteps (which may cause the ball to move straight through an object). We assume that a collision can be detected just by knowing if two objects currently intersect.

  3. Now create an axis aligned rectangle in the top center of the screen. You will need to check for collision against each side of the rectangle, after which you can reflect the position velocity off that axis. Refer to the intersection detection notes on how to implement the Circle-AABB test.
  4. Create a circle in the top right of the screen. Read the lecture notes and use your vector functions from the first part of this tute to test for a collision. Then think about how to make the ball bounce off the circle in the correct direction.

    reflect velocity around normal

    In this image the velocity is reflected just like the light vector was in phong lighting. Note that simply reflecting the velocity does not work when both objects can move. We'll cover this case next week.

  5. Lastly we want an arbitrary polygon. This will require line segments that are not axis aligned. Create one diagonal line to test this collision. Again, use your vector functions to test for a collision and bounce the ball off the line. Once this is working, create 2 extra lines to form a triangle.

Extra: