Basic Collision Detection in 2D - Part 2
Page 1 of 2
<1 | 2>
Single-page view
Basic Collision Detection in 2D - Part 2

This article originally appeared in Dev.Mag Issue 29, released in February 2009

Last month we covered some of the very basic ways of testing whether object A hits object B. But, while the techniques we covered could quite likely take you very far, you'll inevitably encounter a time when they simply aren't enough. So this month we'll go a little further.

Line-Line intersection test revised

Occasionally, you'll need slightly more information from your collision tests. Many physical applications will require the precise coordinates of the intersection between lines. Thankfully, this is the easiest test to get that information.

The ol

Referring back to last month, we had defined Pa and Pb as points on the respective lines that we were testing as below:

Pa = A1 + ua (A2 - A1 )

Pb = B1 + ub (B2 - B1 )

We then solved for points ua and ub , or, rather, simply for their denominators to determine their existence. To calculate the precise point of intersection, you'd simply have to calculate and substitute one of these values into the equations for Pa or Pb (it doesn't matter which, they'd be identical) to get your answer. Adjusting our code from last time results in the following:

LineLineCollision
Input
	LineA1	Point	First point on line A
	LineA2	Point	Second point on line A
	LineB1	Point	First point on line B
	LineB2	Point	Second point on line B
Output
	The point of the collision, or null if no collision exists.
Method
	denom = ((LineB2.Y – LineB1.Y) * (LineA2.X – LineA1.X)) – 
		((LineB2.X – LineB1.X) * (LineA2.Y - LineA1.Y))
	if (denom == 0)
		return null
	else
		ua = (((LineB2.X – LineB1.X) * (LineA1.Y – LineB1.Y)) – 
			((LineB2.Y – LineB1.Y) * (LineA1.X – LineB1.X))) / denom
		/* The following 3 lines are only necessary if we are checking line
			segments instead of infinite-length lines */
		ub = (((LineA2.X – LineA1.X) * (LineA1.Y – LineB1.Y)) – 
			((LineA2.Y – LineA1.Y) * (LineA1.X – LineB1.X))) / denom
		if (ua < 0) || (ua > 1) || (ub < 0) || (ub > 1)
			return null
		
		return LineA1 + ua * (LineA2 – LineA1)

Circle-line intersection test

This is another common test that should find a use in many games. Did your ball collide with the wall of the level, or a barrier set up by the enemy? Things like that require a circular object to be tested against a line.

Circle-line collision

This test involves slightly longer equations than previous intersections, but is still in familiar algebraic and geometric ground. And, in fact, the expressions may be simplified greatly by the simple act of working in the circle's local space, effectively removing all circle-centre variables from the equations by setting them all to zero. This is achieved simply by expressing the two coordinates representing the line as positions relative to the centre of the circle. Doing this makes your calculations and equations far simpler.



Words from the readers
No comments posted for this article yet. Have something to say? Make yourself heard below.
Have your say: