Wednesday 22 February 2012

Collision theory

My last two posts have shown how to do basic collisions without explaining why they work. In many cases it's not important to know why, and a lot can be done with just bouncing balls off walls and the floor. But the theory shows where the results come from, how they fit together and most important how to use it to solve other collision problems.


To bounce a ball off a wall, floor or other edge we need the normal to that surface. This is a vector, orthogonal or at right-angles to the edge or surface and pointing away from it.


In two dimensions the Perp() function, described in this post, can be used. As well as being orthogonal the normal vector should be a unit vector, with length 1, achieved by scaling it by the reciprocal of its length. For a fixed edge or surface this only needs to be calculated once, so for the purpose of this post I will assume the normal has been calculated and has value n, where n is a vector.


The distance of an object from the edge or surface is given by the dot product. If it's position is given by the vector x then the distance d is 




Where a is the position of a point on the edge/surface. This should be positive for points above the surface/inside the edge. If d is negative then it is below the surface, and to move it back it is moved along the vector n by a distance two times d, i.e.




The velocity calculations are similar. If the ball has velocity v, again a vector, then the speed s with which it approaches the edge or surface – the impact speed – is given by


To bounce the ball back off the surface an impulse is applied, which has the effect of changing the velocity, again in a direction along the normal vector and depending on the coefficient of restitution e:




These are the general equations for a ball bouncing off a smooth wall (one without friction that does not make the ball rotate). They can be used not just for straight edges (oriented in any direction) but for more general edges, as long as a normal vector perpendicular to the edges can be found. And because they use general vectors they work as well in three dimensions as two.


Where the edges are horizontal or vertical the calculations are constrained to one dimension/axis and are much simpler, as described yesterday. But even the most general case is straightforward for a non-rotating ball.

No comments:

Post a Comment