- Home /
Unity best practices for determining if something is inside or outside the circumference of an ellipse
Hello,
I am try to setup an asset that needs to identify regions for biome selection. I would like to do this using ellipses so I can make an island of sorts using procedural code. Basically I need a function that will determine if a 2D point is inside our outside the circumference of an ellipses given 2 sets of x/y coordinates. The first set would determine the dimensions of the ellipse. The second set would be used to determine if the point was either inside or outside the ellipse returning Boolean. I am pretty new to using geometry in a video game setting and am researching the math. I figured there were tons of situations where the same formulas/algorithms would be useful, e.g. target ranges, etc. I am guessing if there is not already an established way to do this easily, there are supportive libraries I am just not familiar with that might help with some of the heavier math.
Here are some of the articles I've been going from to try to get a handle on what I need to do so far: http://en.wikipedia.org/wiki/Ellipse#Circumference
http://en.wikipedia.org/wiki/Elliptic_integral#Complete_elliptic_integral_of_the_second_kind
http://en.wikipedia.org/wiki/Circumference
http://www.mathopenref.com/coordparamellipse.html
http://www.mathopenref.com/coordgeneralellipse.html
http://www.mathopenref.com/coordcirclealgorithm.html
And just because I thought these would be interesting to do the same with:
http://en.wikipedia.org/wiki/Hypotrochoid
http://en.wikipedia.org/wiki/Epitrochoid
It would seem to me, plotting points along the edge of an ellipse and filling the area of an ellipse are easier (or maybe just done more often) than determining the circumference of an ellipse. Am I mistaken?
Can anyone help with how I can determine inside vs the outside of an ellipse. And as a bonus maybe explain how I can even expand on that to include procedurally generated shapes like an epitrochoid? (That would make for much more realistic shorelines, ranges, etc...)
Thank you in advance, Boysenberry Payne
Have you looked at Physics.OverLapSphere or Physcis.CapsuleCast
Answer by Owen-Reynolds · May 12, 2015 at 04:05 PM
"Best practices" in Unity is not to use an ellipse. There's no built-in collider for it, the math is harder, and it's just a game. We'd use a capsule collider or something. A big part of making a game is identifying places where you can tweak/downgrade the specification just a little, in return for 80% less work.
So that's why you don't see much about odd shapes here.
If you really need an ellipse (sounds like you do): you could just do the math `(ax^2+by^2
I started down that path last night. I have a tendency to make things a little hard than need be, thus the question.
I don't $$anonymous$$d going the collider route at all, I am just not sure how to implement it. I was thinking maybe raycasting on a sprite similar to collusion detection in 2D games.
The issue for me is I am getting a coordinate for a voxel in world space and I need to be able to know what biome the coordinate belongs to. I thought the most straight forward approach would be using concentric oblong rings to deter$$anonymous$$e each of the main biomes, e.g. beach leading to mountains then to the valley within.
How would I do this with colliders, make them invisible in world space then hit testing using raycasting (or something else) to deter$$anonymous$$e the boundaries of each biome/collider?
PS for the math portion (ax^2+by^2) a and b are the coordinates to deter$$anonymous$$e whether a point is inside or outside, and x and y are for deter$$anonymous$$ing the size of the ellipse, right?
Answer by boysenberry · May 18, 2015 at 02:08 PM
So, the answer for me came in the form of a splatmap. Basically I make a ellipse the dimensions I want in Photoshop or GIMP (or what ever image app you use) import it into Unity and then check the sprite out using Texture2D and its methods, i.e. ellipse.GetPixel(x,y).r (using a white ellipse here). If the value is greater than 0 than I know I am in the ellipse.
The colliders weren't doing it for me because none of this is happening in the scene, but is to effect what happens in the scene.
Also the ax^2+by^2 formula works only for plotting or placing things along the edge of a ellipse, but isn't really helpful when it comes to telling whether something is on one side or the other of that edge.
I hope this makes it easier for someone after me. I couldn't find an answer for it in the forums...
?? I think the problem is you're thinking of this as a Unity issue, when it's really just math. If you focused on the trig, you'd be able to look up how ax^2+bx^2=c
is exactly used to check in/out (plug in x/y from the center, and check the result is less than / greater than c.) Likewise, a 2D array of bools would probably work better than a splatmap/texture (which uses 4 ints for each yes/no.)
You're absolutely right. I missed the =c part on your initial post and wasn't sure if it meant that. If I use the x/y from 0 of the ellipse, then feed a/b for the x/y I am testing with what value of c will be in vs out of the circumference. That's the part I am not sure of.
As for the 2D array of bools, that will probably be what I end of working with, but outside of using an image to generate those values I am not sure how to derive them.
Unfortunately, and the reason for asking the question, my trig is rather weak. I know how to look it up, but translating the math notation in to what I know syntactically, well lets just say up to now I haven't needed it much.
HT$$anonymous$$L did eat the last pat of my answer (I think there was a less-than sign. Odd, since it was there in the preview.)
To repeat myself, you can usually make slight changes in the rules, and most people play to their strengths. If you aren't so great at trig and such, and decide to go with a trig-heavy approach, you just volunteered yourself to google a bunch of High School trig.
You wrote that you "would like to" do it using ellipses. So, I assume it's not a firm biome rule. If what you have works and is fast enough, you're done. If not, maybe a good time to rethink the approach, not that you've explored using ellipses.
Your answer
![](https://koobas.hobune.stream/wayback/20220613195341im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move Object allong an ellipsoid path 1 Answer
Calculate a spline with start- and endpos 0 Answers
Drawing a moving objects trajectory 4 Answers
Constant speed on elliptical path 0 Answers