Preferred Ellipse Radius (For Hit Detection)

BlitzMax Forums/BlitzMax Programming/Preferred Ellipse Radius (For Hit Detection)

Johnsprogram(Posted 2011) [#1]
I'm having a math-related crisis that one man (me) can not do alone. I'm trying to figure out a way to calculate the radius of the ellipse on the same angle as the light blue line; in other words, the length of the "mystery line."



Here's the situation, I was using trigonometry for locating the X and Y position on the circumference of the ellipse using sine and cosine. However, when I entered the same angle (in degrees), the line point towards point P instead of pointing towards point x. Why am I focusing on point x instead of point P? It's because that the angle is set to a certain point outside the ellipse.

I'm using the calculated radius to detect any targets that enters inside the area of the ellipse. In other words, this is for a hit detection. Unless if there's an efficient way to detecting hits on polygons and ignoring the transparent areas for BlitzMAX, feel free to share it with me.


skidracer(Posted 2011) [#2]
if dx,dy is distance of point from center of ellipse and r is ratio of ellipseheight/ellipsewidth then (dx*r)*(dx*r)+(dy*dy) will be less than ellipseheight*ellipseheight when point is inside ellipse


Johnsprogram(Posted 2011) [#3]
are both the ellipse height & width the radius or the diameters that you mentioned, skidracer?


skidracer(Posted 2011) [#4]
radius


Johnsprogram(Posted 2011) [#5]
any chance that that formula would work for larger widths and smaller heights? Btw, the formula worked for circles and heightened ellipse. How were you able to find that formula?


skidracer(Posted 2011) [#6]
It should work - I made it up based on the fact that your ellipse is just a circle in a squeezed universe.


Floyd(Posted 2011) [#7]
When you use the parametric representation ( in this case with r1 = 40, r2 = 80 )

x = r1 * Cos( t )
y = r2 * Sin( t )

the value t is just a parameter. It is not the central angle, unless the ellipse is really a circle.

Conceptually, it is simpler to scale everything so the ellipse becomes a circle. From the example in the picture we take the center of the ellipse as the origin. Then multiply all x values by 2 so both radii are 80. Points are moved to new points, lines change to different lines and the ellipse becomes the much simpler circle. Whether or not a point is in the ellipse is unchanged by this transformation. Just imagine the entire picture made twice as wide, with the center of the ellipse not moving.


Johnsprogram(Posted 2011) [#8]
It's easy to figure out the radii when you are looking at 90 degree intervals. However, looking in between the two 90 degree intervals is not as clear. I don't know how the scaling works, but rest assure that I probably won't find it out by using Sine and Cosine.

The green line (the line from center to point P) is a result from using Sine and Cosine with THETA = 60. Thus, pin-pointing to point P on the ellipse's edge will end up being on the same horizontal line -- in this case -- as point A on the auxiliary circle's edge. Obviously, that is not what I want. For navigating purposes, I'm trying to find out the radius of the ellipse on the blue "mystery" line (from center to point X), just to be clear.

I believe, somewhere, that there is equation to determine how to scale the ellipse's radius between 80 and 40 accurately. Sine & Cosine would not help in this case since it'll end up like the green line; which, in this example, will be too long. Maybe the change of the radius' scale is hyperbolic?

Now, in terms for detecting a point appearing inside the ellipse, the formula that skidracer gave me works. But, I'm still (very) curious for how to figure out that "mystery line" (center to point x).


Warpy(Posted 2011) [#9]
The locus of the edge of the ellipse is,in this case,

(x/40)^2 + (y/80)^2 = 1

You want to know the length of the line heading at 60 degrees from the vertical to the edge of the ellipse. Call the length L, so the co-ordinates of the end are given by

x = L*sin(60)
y = L*cos(60)

Substitute those into the first equation

(L*sin(60)/40)^2 + (L*cos(60)/80)^2 = 1

Take a factor of L^2 out:

L^2*((sin(60)/40)^2 + (cos(60)/80)^2) = 1

And rearrange:

L = sqr( 1/( (sin(60)/40)^2 + (cos(60)/80)^2 ) )


Some code:

Graphics 600,600,0

an = 60
While Not (KeyHit(KEY_ESCAPE) Or AppTerminate())

	ew# = MouseX()-300
	eh# = MouseY()-300
	
	SetColor 100,100,100
	DrawOval 300-ew,300-eh,ew*2,eh*2
	
	an:+1
	
	l# = Sqr( 1/( (Sin(an)/ew)^2 + (Cos(an)/eh)^2) )
	
	lx# = 300+l*Sin(an)
	ly# = 300-l*Cos(an)
	
	SetColor 0,0,255
	DrawLine 300,300,lx,ly
	
	Flip
	Cls
Wend



Johnsprogram(Posted 2011) [#10]
Thanks a million, warpy! That was exactly that I was looking for!