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

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)[sup]2[/sup] + (y - k)[sup]2[/sup] = r[sup]2[/sup]

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 (r) 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[sup]2[/sup] - 2xh + h[sup]2[/sup] + y[sup]2[/sup] - 2yk + k[sup]2[/sup] - r[sup]2[/sup] = 0

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[sub]x[/sub] + td[sub]x[/sub]

y = e[sub]y[/sub] + td[sub]y[/sub]

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

( e[sub]x[/sub] + td[sub]x[/sub] )[sup]2[/sup] - 2( e[sub]x[/sub] + td[sub]x[/sub] )h + h[sup]2[/sup] + ( e[sub]y[/sub] + td[sub]y[/sub] )[sup]2[/sup] - 2( e[sub]y[/sub] + td[sub]y[/sub] )k + k[sup]2[/sup] - r[sup]2[/sup] = 0

expand => e[sub]x[/sub][sup]2[/sup] + 2e[sub]x[/sub]td[sub]x[/sub] + t[sup]2[/sup]d[sub]x[/sub][sup]2[/sup] - 2e[sub]x[/sub]h - 2td[sub]x[/sub]h + h[sup]2[/sup] + e[sub]y[/sub][sup]2[/sup] + 2e[sub]y[/sub]td[sub]y[/sub] + t[sup]2[/sup]d[sub]y[/sub][sup]2[/sup] - 2e[sub]y[/sub]k - 2td[sub]y[/sub]k + k[sup]2[/sup] - r[sup]2[/sup] = 0

group => t[sup]2[/sup]( d[sub]x[/sub][sup]2[/sup] + d[sub]y[/sub][sup]2[/sup] ) + 2t( e[sub]x[/sub]d[sub]x[/sub] + e[sub]y[/sub]d[sub]y[/sub] - d[sub]x[/sub]h - d[sub]y[/sub]k ) + e[sub]x[/sub][sup]2[/sup] + e[sub]y[/sub][sup]2[/sup] - 2e[sub]x[/sub]h - 2e[sub]y[/sub]k + h[sup]2[/sup] + k[sup]2[/sup] - r[sup]2[/sup] = 0

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

=> t[sup]2[/sup]( d DOT d ) + 2t( e DOT d - d DOT c ) + _e DOT _e - 2( _e DOT_c ) + _c DOT _c - r[sup]2[/sup] = 0

=> t[sup]2[/sup]( d DOT d ) + 2t( d DOT ( e - c ) ) + ( e - c ) DOT ( e - c ) - r[sup]2[/sup] = 0

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[sup]2[/sup]( d DOT d ) + 2t( d DOT f ) + f DOT f - r[sup]2[/sup] = 0

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

t[sup]2[/sup] * (d DOT d) + 2t*( f DOT d ) + ( f DOT f - r[sup]2[/sup] ) = 0

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!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 = 0 && t2

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 , hexagonCorners[i + 1]); else //last corner connects to first corner performCircleToRayCollisionChecks(p_ball, hexagonCorners, 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!

[sharedmedia=gallery:images:4786]

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