HTML and CSS Reference
In-Depth Information
for (var ballA, i = 0, len = numBalls - 1; i < len; i++) {
ballA = balls[i];
for (var ballB, j = i + 1; j < numBalls; j++) {
ballB = balls[j];
checkCollision(ballA, ballB);
}
}
balls.forEach(draw);
}());
};
</script>
</body>
</html>
Of course, you're free to investigate your own solutions to the problem, and if you come up with something
that is simpler, more efficient, and more accurate, please share!
Important Formulas in this Chapter
The important formula in this chapter is the one for conservation of momentum.
Conservation of Momentum, in Straight Mathematical Terms
(m0 - m1) × v0 + 2 × m1 × v1
v0Final = ----------------------------
m0 + m1
(m1 - m0) × v1 + 2 × m0 × v0
v1Final = ----------------------------
m0 + m1
Conservation of Momentum in JavaScript, with a Shortcut
var vxTotal = vx0 - vx1;
vx0 = ((ball0.mass - ball1.mass) * vx0 + 2 * ball1.mass * vx1) /
(ball0.mass + ball1.mass);
vx1 = vxTotal + vx0;
Summary
Congratulations! You've made it through the heaviest math in the topic and you now have in your
repertoire the methods for handling accurate collision reactions. One thing we've ignored in these
examples, just to keep them simple, is the concept of friction. You can try to add that into the system
because you certainly know enough at this point to do so. Be sure to check out Chapter 19, where you'll
see a little trick to use in the case that both objects have the same mass.
 
Search WWH ::




Custom Search