About the easiest thing one can do is a circular collision detection. You just calculate the distance from center to center and if the result is smaller than the summed radii the circles are colliding.
But in my game the objects have oval shadows, well basically just circles with half the height. I noticed the collision detection should match the shadows, otherwise it feels weird when playing.
So how can I check if 2 ovals are colliding? The ovals are not roated, as said before they are just half-the-height shrinked circles.
Any ideas?
Thanks, Fox
Last edited by Fox; Nov 4th, 2005 at 06:19 PM.
Reason: [resolved]
First of all, are your "ovals" ellipses? I mean, can they be described by a simple equation or are they defined as a bunch of arbitrary coordinates (i.e. a polygon)?
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Well, I'm going to assume you work with ellipses all of the same size and the same orientation as you've mentioned they are not rotated (this latter assumption considerably simplifies the problem).
Let's consider a pair of ellipses. I'll set the coordinate origin at the center of one of them (see figure).
A collision will take place if
F = OO' - (OM + ON) <=0
By symmetry, OM = ON so:
F = OO' - 2*OM <=0
In order to find OM we need the intercept of the first ellipse with the line connecting the centers, i.e. we must solve this system of equations:
(x/a)2 + (y/b)2 = 1
y = (q/p)x
where a and b are the semiaxes and (p,q) are the coordinates of the center for the second ellipse. This leads to:
(x/a)2 + ((qx/p)/b)2 = 1
and finally:
x = abp/Sqrt(a2q2 + b2p2)
y = abq/Sqrt(a2q2 + b2p2)
so that OM = Sqrt(x2 + y2) = ab*Sqrt(p2 + q2) / Sqrt(a2q2 + b2p2)
which can be finally expressed more ellegantly as:
Sqrt[(p / a)2 + (q / b)2] <= 2
Last edited by krtxmrtz; Nov 7th, 2005 at 03:55 AM.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
by assuming random values for p and q and results don´t always match. Try, for instance, a = 15; b = 10; p = 25 and q = 15. Equation 1) returns -9,44 < 0, which satisfies, but equation 2) returns 2,24 > 2, which doesn´t. I add an Excel file where you can give any values to a and b and let p and q vary randomly.
Please correct me if I am wrong.
Today (12 of November) I exchanged the Excel file that I had attached to this post by another one which is now correct after the Krtxmrtz comments made in the next posts.
Last edited by Rassis; Nov 13th, 2005 at 10:52 AM.
...este projecto dos Deuses que os homens teimam em arruinar...
Well, I've spotted a mistake in cell G9: the last term "+F4^2" should be "+C5^2*F4^2". Check it up.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
You are right. I am so sorry. Thank you and congratulations for the ellegant demonstration (I rated your post).
Anyway, thanks for checking it. Although this time it was correct, I often make mistakes when cumbersome algebraic derivations are involved.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
in order to speed up the code, specially if the number of ovals to be tested is large. But probably you had already thought of that.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
I’d like to leave here a new Excel application with the correction of my mistake spotted today by Krtxmrtz. It does confirm the correctness of the expression deducted by him.
Last edited by Rassis; Nov 8th, 2005 at 03:50 AM.
...este projecto dos Deuses que os homens teimam em arruinar...