**1**

# Arkanong part 5: Hexagons! (collision detection, again)

Posted by

**molehill mountaineer**, 09 February 2014 · 468 viewscollision detection sphere circle line intersection c++ sfml pong arkanoid arkanong

To make the game a bit more interesting we're going to add a few hexagonal barriers in the playing field.

This object is different from the paddle and balls in that it doesn't have a 'regular' shape. This means that using a rectangular

or circular hitregion will not cover the object properly.

Now, I could fudge the dimensions of the hexagon a little and consider it a circle for the purposes of collision detection, but where's the fun in that?

Luckily I found a neat little algorithm online which detects collisions between a circle and a line segment. Here's how it works:

- make a vector which represents the line segment.

We will call this vector d and form it by subtracting the start of the line segment from the end of the line segment.

linevector = endPoint - startPoint

- make a vector from the center of the circle to the start of the line segment.

We'll call it vector f and form it by subtracting the circle's position from the start of the ray. We'll use this later on to simplify the equation.

centerToStartPoint = start - center

- combine the two following equations:

This equation locates the collision point on the line segment by starting from the first point (e) and 'moving' along the vector (d)

by a certain percentage (t).

In other words, if you have a line that starts at (4,0) and ends at (10,0), and a collision occurs in the middle of the line (= 50%)

t will be 0.5 and p will be equal to (9,0).

This equation uses the pythagorean theorem to represent circle 'touching' the collision point. (h,k) is the center of the circle

it basically means that if you take the distance from the collision point to the centre of the circle it will be equal to the radius ® of the circle.

Now we're going to combine these two equations to end up with our actual collision detection formula:

1 ) first expand equation number two. You'll end up with the following:

x

2) now, in the first equation the points p, e and vector d consists of two components (x and y).

So we 'split' it into:

x = e

y = e

Then you substitute the x and y variables in step 1 with these ones. You'll end up with this:

( e

expand => e

group => t

write as dotproducts, where d = line segment, c = centre of circle

=> t

=> t

remember that e is the starting point of the line segment, and that we have a vector f which is the vector from the

start of the line to the center of the circle (f = e - c) -> we can use this to further simplify the equation

=> t

After we've simplified the formula as much as possible we ended up with the following:

where:

f is the vector from the centre of the circle to the starting point of the ray

d is the vector representing the line itself

r is the radius of the circle

t is the 'percentage' of the line at which is the collision occurs

If you look closely, you will see that this equation is of the form

where:

a = d DOT D

b = 2 * f DOT d

c = f DOT f - r^2

This is a quadratic equation, which means we can solve it by using the quadratic formula!

to the equation by using the quadratic formula and you will end up with the 'percentage' values which in turn can be used to find the coordinates of the collision point.

Now, these calculations consider the line as being infinitely long so we need to check if these percentages are between 0 and 1 (= 0% to 100% of the line). If they are greater or lesser than these values then the collision was either before or after the two points which make up the line segment.

Now that we know how to bounce a ball of a line all that needs doing is doing the check for every side of the hexagon - that's what this piece of code does. It seems to work quite well, except when the ball hits the exact corner of two line segments, I'll take care of that bug a bit later.

You can take a look at the code at https://github.com/bytechomper/arkanong

(code for this article is in the class 'CollisionManager' in the folder 'source')

This object is different from the paddle and balls in that it doesn't have a 'regular' shape. This means that using a rectangular

or circular hitregion will not cover the object properly.

Now, I could fudge the dimensions of the hexagon a little and consider it a circle for the purposes of collision detection, but where's the fun in that?

Luckily I found a neat little algorithm online which detects collisions between a circle and a line segment. Here's how it works:

- make a vector which represents the line segment.

We will call this vector d and form it by subtracting the start of the line segment from the end of the line segment.

linevector = endPoint - startPoint

**=> d = l - e**- make a vector from the center of the circle to the start of the line segment.

We'll call it vector f and form it by subtracting the circle's position from the start of the ray. We'll use this later on to simplify the equation.

centerToStartPoint = start - center

**=> f = e - c**- combine the two following equations:

**a) p = e + t*d**This equation locates the collision point on the line segment by starting from the first point (e) and 'moving' along the vector (d)

by a certain percentage (t).

In other words, if you have a line that starts at (4,0) and ends at (10,0), and a collision occurs in the middle of the line (= 50%)

t will be 0.5 and p will be equal to (9,0).

**b)****(x - h)**^{2}+ (y - k)^{2}= r^{2}This equation uses the pythagorean theorem to represent circle 'touching' the collision point. (h,k) is the center of the circle

it basically means that if you take the distance from the collision point to the centre of the circle it will be equal to the radius ® of the circle.

Now we're going to combine these two equations to end up with our actual collision detection formula:

1 ) first expand equation number two. You'll end up with the following:

x

^{2}- 2xh + h^{2}+ y^{2}- 2yk + k^{2}- r^{2}= 02) now, in the first equation the points p, e and vector d consists of two components (x and y).

So we 'split' it into:

x = e

_{x}+ td_{x}y = e

_{y}+ td_{y}Then you substitute the x and y variables in step 1 with these ones. You'll end up with this:

( e

_{x}+ td_{x})^{2}- 2( e_{x}+ td_{x})h + h^{2}+ ( e_{y}+ td_{y})^{2}- 2( e_{y}+ td_{y})k + k^{2}- r^{2}= 0expand => e

_{x}^{2}+ 2e_{x}td_{x}+ t^{2}d_{x}^{2}- 2e_{x}h - 2td_{x}h + h^{2}+ e_{y}^{2}+ 2e_{y}td_{y}+ t^{2}d_{y}^{2}- 2e_{y}k - 2td_{y}k + k^{2}- r^{2}= 0group => t

^{2}( d_{x}^{2}+ d_{y}^{2}) + 2t( e_{x}d_{x}+ e_{y}d_{y}- d_{x}h - d_{y}k ) + e_{x}^{2}+ e_{y}^{2}- 2e_{x}h - 2e_{y}k + h^{2}+ k^{2}- r^{2}= 0write as dotproducts, where d = line segment, c = centre of circle

=> t

^{2}( d DOT d ) + 2t( e DOT d - d DOT c ) + _e DOT _e - 2( _e DOT_c ) + _c DOT _c - r^{2}= 0=> t

^{2}( d DOT d ) + 2t( d DOT ( e - c ) ) + ( e - c ) DOT ( e - c ) - r^{2}= 0remember that e is the starting point of the line segment, and that we have a vector f which is the vector from the

start of the line to the center of the circle (f = e - c) -> we can use this to further simplify the equation

=> t

^{2}( d DOT d ) + 2t( d DOT f ) + f DOT f - r^{2}= 0After we've simplified the formula as much as possible we ended up with the following:

**t**^{2}* (d DOT d) + 2t*( f DOT d ) + ( f DOT f - r^{2}) = 0where:

f is the vector from the centre of the circle to the starting point of the ray

d is the vector representing the line itself

r is the radius of the circle

t is the 'percentage' of the line at which is the collision occurs

If you look closely, you will see that this equation is of the form

where:

a = d DOT D

b = 2 * f DOT d

c = f DOT f - r^2

This is a quadratic equation, which means we can solve it by using the quadratic formula!

sf::Vector2f lineVector = p_rayEnd - p_rayStart; sf::Vector2f centreToStartOfRay = p_rayStart - ballPosition; float a = (lineVector.x * lineVector.x) + (lineVector.y * lineVector.y); float b = (2 * ((centreToStartOfRay.x * lineVector.x) + (centreToStartOfRay.y * lineVector.y))); float c = ((centreToStartOfRay.x * centreToStartOfRay.x) + (centreToStartOfRay.y * centreToStartOfRay.y)) - (ballRadius * ballRadius); //calculate discriminant to determine if collision has taken place float discriminant = (b*b) - 4 * a*c;If the discriminant if greater than or equal to zero, then the circle has collided with the ray at some point. Calculate the solution

to the equation by using the quadratic formula and you will end up with the 'percentage' values which in turn can be used to find the coordinates of the collision point.

Now, these calculations consider the line as being infinitely long so we need to check if these percentages are between 0 and 1 (= 0% to 100% of the line). If they are greater or lesser than these values then the collision was either before or after the two points which make up the line segment.

//one or more collision points with entire ray //(meaning also past the actual corners of the hexagon) if (discriminant >= 0) { //determine t1 & t2 //(this is the 'percentage' along the ray at which the collision point can be found) discriminant = sqrt(discriminant); float t1 = (-b - discriminant) / (2 * a); float t2 = (-b + discriminant) / (2 * a); //if the 'percentages' are between 0 and 1 there are //one or more collisions points within the actual line segment //(meaning contained between the two corners) if ((t1 >= 0 && t1 <= 1) || (t2 >= 0 && t2 <= 1)) {Once we know the coordinates of the collision point we simply perform the same response as in the previous posts - project the velocity of the ball onto a collision normal unit vector and a unit tangent vector perpendicular to it, then 'flip' the normal part and recombine them into the new velocity of the ball.

Now that we know how to bounce a ball of a line all that needs doing is doing the check for every side of the hexagon - that's what this piece of code does. It seems to work quite well, except when the ball hits the exact corner of two line segments, I'll take care of that bug a bit later.

//loop over the six corners, perform circle to ray collision checks using the corners //as start and end point for the rays for (int i = 0; i < 6; ++i) { //form ray between this corner and next if (i != 5) performCircleToRayCollisionChecks(p_ball, hexagonCorners[i], hexagonCorners[i + 1]); else //last corner connects to first corner performCircleToRayCollisionChecks(p_ball, hexagonCorners[i], hexagonCorners[0]); }If you feel you need to brush up on your algebra (I certainly did while making this) you can do that for free at khanacademy.org - I highly recommend it!

You can take a look at the code at https://github.com/bytechomper/arkanong

(code for this article is in the class 'CollisionManager' in the folder 'source')

**All credit goes to the following stack overflow post**: http://stackoverflow.com/questions/1073336/circle-line-collision-detection