# Collision detection

Started by Jul 25 2001 04:12 AM

,
3 replies to this topic

###
#1
Members - Reputation: **133**

Posted 25 July 2001 - 04:12 AM

I just made a program which has an ellipse and a line traveling along the screen and basically bouncing off of walls. I would like to make it so that if they collide, they bounce off each other. However, I''m not sure what condition to test for since the only values I know are the end points of the line and the "corners" of the ellipse. What values should I test for?
Thanks
Daniel

###
#2
Members - Reputation: **351**

Posted 25 July 2001 - 04:29 AM

There are two/three possibilities: either of the ends of the line can hit the ellipse or they can collide at some point along the line.

The first case is simple: just whether each of the line enpoints is inside the ellipse, using the inequality:

x

The second case is trickier but is still calculatable. First there will be a point on the ellipse closest the line: it''s the point with the same gradient/slope as the line, and on the same side as the line. One you have this calculate the point on the line nearest to it.

You now have the points on the line and ellipse closest to each other. They only collide if the point on the line, P, is inside the ellipse and between the ends of the line, X and Y. To test whether it''s inside the ellipse use the same test as for the endpoints. To test whether it''s between the ends of the line work out the dot product of XP and XY, which will be between 0 and XY

A lot of steps but all standard techniques or methods which hopefully you can work out.

The first case is simple: just whether each of the line enpoints is inside the ellipse, using the inequality:

x

^{2}/ a^{2}+ y^{2}/ b^{2}< 1The second case is trickier but is still calculatable. First there will be a point on the ellipse closest the line: it''s the point with the same gradient/slope as the line, and on the same side as the line. One you have this calculate the point on the line nearest to it.

You now have the points on the line and ellipse closest to each other. They only collide if the point on the line, P, is inside the ellipse and between the ends of the line, X and Y. To test whether it''s inside the ellipse use the same test as for the endpoints. To test whether it''s between the ends of the line work out the dot product of XP and XY, which will be between 0 and XY

^{2}if the point is between X and Y.A lot of steps but all standard techniques or methods which hopefully you can work out.

###
#4
Members - Reputation: **164**

Posted 27 July 2001 - 05:03 PM

Another way that will test for both cases is to find the intersection of the ellipse and the line. Basically this involves simultaneously solving the equation for the line

y=mx+b

and the ellipse

(x-h)²/a² + (y-k)²/b² = 1

If you don''t know how to do this search for "line ellipse intersection" or something like that.

Solving will either get no solution (there''s no intersection/collision), one solution or two solutions. If you get one or two solutions you check if the solution(s) lies between the endpoints of your line segment (an easy way is to see if it lies in the rectangle formed by the endpoints).

btw in the equation for the ellipse, (h,k) are the coordinates of the centre of the ellipse, a and b are half the width and half the height of the ellipse respectively.

y=mx+b

and the ellipse

(x-h)²/a² + (y-k)²/b² = 1

If you don''t know how to do this search for "line ellipse intersection" or something like that.

Solving will either get no solution (there''s no intersection/collision), one solution or two solutions. If you get one or two solutions you check if the solution(s) lies between the endpoints of your line segment (an easy way is to see if it lies in the rectangle formed by the endpoints).

btw in the equation for the ellipse, (h,k) are the coordinates of the centre of the ellipse, a and b are half the width and half the height of the ellipse respectively.