Tute: Collision detection and reaction

Aims:

Note: It is not a requirement that all material in this tutorial be included in assignment 2. Please see the specs.

Exercises:

  1. You may want to wrap the current ball variables (position, velocity and radius) in a struct. Then create two ball instances.
  2. Add a mass variable to the ball which is always set proportionally to the area (πr2) of the ball.
  3. Initialize both balls such that they collide when the program is run. Creating a few different scenarios will be useful to test the collision reaction. For example, the second ball might be stationary, the collision may happen on an angle, front on, axis aligned etc.
  4. In the update/idle function, move the balls over time based on their velocities. Each frame check to see if the balls are intersecting. When an intersection is found, calculate the normal at the point of intersection (we can approximate this to be a direction vector from one ball to the other) and reflect the veolcities of both balls.

    Visually debugging collision detection and reaction can be a great way to find errors. Being able to pause time and manually step through simulation frames can be helpful too. Add code to draw normals, velocities and reflection vectors etc. at the point of collision.

  5. You may notice that the collision response does not appear correct, especially if one ball is stationary.

    image

    In the lectures 1 dimensional collision reaction has been discussed (see the notes).

    v1 equation
    v2 equation

    Where U (or Vi) is the initial velocity and V (or Vf) is the final velocity.
    Note: The above equations are for the case of 1D only.

    For higher dimensions the object velocities are split into two components. One in the direction of the normal and the other in the direction of the tangent. The tangent components do not change while the 1D collision reaction equations are applied to the normal components of the velocities. Finding the velocity components can be calculated using a change of basis or scalar projection. The maths for each method is the same.

    • To change basis so the normal and tangent collision vectors are axis aligned, construct a matrix with the normal and tangent vectors as rows. The inverse of this transformation matrix will transform a vector from the standard basis to our "collision" vector space. Then the velocity components are simply the x and y values of the transformed vector. If the matrix is orthogonal, which it is, the transpose is equal to it's inverse. After finding the new 1D velocity, the basis can be reverted using the transformation matrix before inversion.
    • M = [Nx, Ny]
          [Tx, Ty]
      V1i = (M-1 × V1)x
      t = (M-1 × V1)y
      ...
      V'1 = M × {V1f, t}
      

      Where V1 is the initial velocity in the standard basis and V'1 is the final velocity after collision in the standard basis.

    • The alternative way to think about it is to find the scalar projection using the dot product of the velocity in the direction of the normal. The tangent component can then be calculated by subtracting this scalar multiplied by the normal from the original velocity. After the new 1D velocity is calculated, the final velocity is the sum of scalars multiplied by each direction vector (normal and tangent).
      V1i = V1 · N
      t = V1 - N × V1i
      ...
      V'1 = t + N × V1f
      
      The last equation can be simplified to
      V'1 = V1 + N × (V1f - V1i)
      
      It should be noted that in this example, the tangent direction and the velocity scalar component is combined in the vector T.

    This should provide a convincing bounce.

  6. Print the total kinetic energy and momentum of both balls before and after each collision. A correct simulation will conserve momentum and energy (the totals stay the same) although interaction with static objects such as level bounds may need to be handled separately (for example giving static geometry a virtual momentum).