Page 1 of 1

Putting Math in the Context of Game Programming Part V- 2D Collisions

#1 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,102
  • Joined: 27-December 08

Posted 27 April 2011 - 04:08 PM

This tutorial will cover collision handling in two dimensions. Some of the concepts covered include energy, momentum, trig, vector addition, and calculus. I am also assuming that you have read my tutorial on collision handling in one dimension or are familiar with these concepts.

Elastic Collisions
Two dimensional collisions are different than one dimensional collisions in one key way: there are multiple dimensions to take into account mathematically. Using vectors, this becomes a lot simpler. As with all collisions, conservation of momentum is observed. So our variable here is momentum. In two dimensions, we deal with angles as well. We can use this angle and basic trig to extract the x and y components of the vector using the polar to parametric conversions: x = r cos(theta) and y = r sin(theta), where r is our vector and theta is the angle.

Since we have multiple components of our momentum, they must be conserved as well. So our system of equations comes out to::
pI[0]cos(theta) + pI[1]cos(theta) = pF[0]cos(theta1) + pF[1]cos(theta2)
pI[0]sin(theta) + pI[1]sin(theta) = pF[0]sin(theta1) + pF[1]sin(theta2)



As we see above, the two objects collide at an angle of impact, but go off into two separate directions as noted by the separate angles theta1 and theta2. Since the collision is elastic, we can use conservation of kinetic energy to solve for the final velocities, and therefore momenta.

When solving for the angles, we can combine these equations as we would any system of equation. Choose your favorite method. You can add them together, or even divide them. Knowing that sin(x)/cos(x) = tan(x), dividing the two equations may be helpful in reducing the number of trig functions, making it easier to solve for the angles using substitution.

Explosions
Explosions are a special type of elastic collision. Rather than the objects in motion acting on each other, there is a triggering force (the explosion) which supplies energy to the collision and sends objects away from each other. Explosions are elastic, which means energy and momentum are conserved. Some applications of explosions can include modeling chemical reactions, cannonfire, a kickback when firing a gun, etc.

Let's break down a cannon firing some. Our initial system includes the cannon with the ball inside of it- so we have two objects. Because of this, we can use a center of mass frame of reference for our initial momentum: pInit = (mass[ball] + mass[cannon]) * velocityInitial). Often times, the initial velocity of our cannon and ball is 0, so the two Objects end up travelling in opposite directions with momentum[ball] = -momentum[cannon]. This isn't always the case, though, so make sure to still run your numbers through the conservation of energy and momentum checks as well.

Inelastic Collisions
Inelastic collisions, as in one dimension, still have two conditions to account for: perfectly inelastic and non-perfect inelastic collisions. The perfectly inelastic collisions are the easier of the two to calculate. We still take the x and y components of the initial momenta of the objects in our system, and add the corresponding components together. This will give us a total x and a total y initial momentum. Here, momentum is conserved, but in a center of mass frame of reference. In other words, momentum[1] + momentum[2] = (mass[1] + mass[2])velFinal. To find the direction of the final momentum, though, we need use the Pythagorean theorem to add the vectors together: momentumFinal = sqrt(momentumInitx^2 + momentumInity^2). To find the direction the vector is traveling, we can simply take the arctan(momentumY/momentumX). We know that tan(theta) = sin(theta)/cos(theta); and since y = rsin(theta) and x = rcos(theta), the r's cancel out.

Inelastic collisions thar are not perfect are more difficult to deal with than either perfectly inelastic collisions or elastic collisions. The reason being is that we don't have center of mass or conservation of energy to simplify the equations. Instead, we must rely solely on solving the system of equations. When setting these up, try to arrange it so theta1 is on one side of the equations, and theta2 is on the other.

Is This A Good Question/Topic? 3
  • +

Replies To: Putting Math in the Context of Game Programming Part V- 2D Collisions

#2 Vodkacannon  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 07-November 11

Posted 10 November 2011 - 03:31 PM

Really cool man. I've always been interested in physics. Physics is the primal science.

What would be even cooler is if you could explain how a 3d engine works and how you would go about programming one. Maybe using openGl as an API?
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10771
  • View blog
  • Posts: 40,102
  • Joined: 27-December 08

Posted 10 November 2011 - 03:34 PM

Game engines are a lot more complicated than basic two-dimensional collisions. Sorry to say- you'll need a lot more math, physics, and programming experience. No one tutorial would be able to teach you how to do all that. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1