Friday, April 17, 2015

java - Calculate when round object is inside an ellipse


I want to calculate in java when a circle is inside an ellipse. I drew an example below:


Play field


For this particular problem, the top left corner of the field has the position 0.0f, 0.0f. The bottom right corner has the position 1.0f, 1.0f. The position of the bat is: 0.48f, 0.94f. The goal is located at: 0.5f, 1.0f, height is 0.1f and the radius 0.4f.


How can I calculate if the bat is inside the ellipse? What is the general algorithm?



Answer



Ellipse math is not significantly more difficult than circle math (for detecting distance from the center).


enter image description here



To detect if a point is inside an ellipse, you check a (a/b) ratio of each component of the coordinate against a known constant. So really, it's the same as any 2-Vector distance check, except you manipulate the coordinates slightly before squaring them.


For example, if you scale the y-coordinate to the same range as the x-coordinate, you can treat it identically to circle. Like so:


Vector2 puckPos = {puck_x, puck_y};
Vector2 goalCenter = {screen_height, screen_width / 2};

float a = 0.4f * screen_width;
float b = 0.1f * screen_height;

if ((
(puckPos.y * (a / b) - goalCenter.y) ^ 2 +

(puckPos.x - goalCenter.x) ^ 2
) < a ^ 2) {

// inside ellipse
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...