MV Help with math to calculate an oval-shaped range? :')

Hello! So, I've been picking up JS pretty easily. I've been able to learn quickly, and figuring out the logic is pretty intuitive to me. But sometimes, what I want to do involves...

...math 😢

I've found coding to be more like literature than mathematics TBH, with the way my brain processes it at least. I've gotten by so far with my basic AF math skills, but I've just now gotten stuck and have no idea how to even begin approaching this:

This is part of a plugin. I just want to check whether a coordinate point (in screen pixels) is within range of another point (and if so, then push some values into a couple arrays). I've had it working just fine for a while with the basic circle and square range/area shapes, but I've just added the option to make elongated ranges/areas (i.e. ovals and rectangles) by setting separate radius values for the X and Y axes.

But, uh... now I don't know how to mathematically implement the oval one. Or where to even begin - my brain just short circuits when I try to start conceptualizing it, because mAtH hArD. Help please? 😅



This is what I've got so far of the specific portion of code I'm working with (this is part of a larger function, so there's more before and after it that isn't immediately relevant to this math problem):

JavaScript:
var distanceX = Math.abs((TouchInput.x + offsetX) - ($gameMap.event(eventId).screenX() + originX));
var distanceY = Math.abs((TouchInput.y + offsetY) - ($gameMap.event(eventId).screenY() + originY));
let distanceDirect = Math.sqrt((distanceX * distanceX) + (distanceY * distanceY));
if (shape == 'Circle') {
    if (radiusX == radiusY) {
        // This is basically what I had before I split the radius parameter into radiusX and radiusY
        if (distanceDirect <= radiusX) {
            nearbyIds.push(parseInt(eventId));
            allDistances.push(distanceDirect);
        }
    } else {
        // ????????????????????????????????????????????
        // This is where I don't know what to do (when radiusX and radiusY are different) to simulate an oval-shaped range
    }
} else if (shape == 'Square') {
    if (distanceX <= radiusX && distanceY <= radiusY) {
        nearbyIds.push(parseInt(eventId));
        allDistances.push(distanceDirect);
    }
}

Thank you for any and all pointers!!
first and foremost: why oval?

and on that note: I think you can relate ovals to circles in this particular case, by scaling one axis. I worked out my perspective for 3D environments by doing just that, so it can be done, but I don't know if it would apply to this.
first and foremost: why oval?
Edited because I mis-explained it before:

As an option for a more precise event-detection range from the point of touch input. With tile-based movement, squares work fine, but with pixel movement (which I'm using in my game), a circular targeting range is more precise. I want the range to be able to be elongated (by changing the radius parameters), though. A square would become a rectangle, and that much is easy to implement; it's the circle stretching into an oval that isn't so much 😛

The elongated range shape would be applied only for particular events (identified by being listed in a parameter alongside their respective map IDs) that have similarly-elongated sprites (e.g. taller than they are wide, or vice versa), so that - for example - if you click their foot, the range can still capture the origin pixel that the plugin will have defined at their center, but if you click just off to the sprite's left or right, the range will not capture the origin pixel and therefore not target that event.

Or really any other reason someone might want to change the shape and/or size of the click targeting range for certain events - but the above is the most obvious reason I would use it for.

I think you can relate ovals to circles in this particular case, by scaling one axis
What do you mean by this? How might I implement this into my code? I'm also not sure if it would apply, just since I don't technically have any sort of actual graphical representation of a circle (or mathematical simulation thereof) that can be scaled, like you would in 3D modeling?

Thank you, though! 🙂 I'll try to chew on the problem a bit more with your advice in mind.
it's the circle stretching into an oval that isn't so much
a stretched circle is still a circle.
all ranges are circular: all points at a distance from a center are within or outside that circle depending on what distance we are talking about.

What do you mean by this?
I needed to work out a corresponding camera angle.
I had a shape seen at that angle, that I knew was a circle.
since all circles are regular, and I know how to write a circle mathematically, and I know the relationship between length and width of the square binding that circle was 1, I could take the measurements of the ellipse I was seeing, reverse the relationship, and come up with an angle.
that angle was the camera looking down on the object.


(re-reading it, I think what you're looking at is a problem that's better handled through bitmaps, not plain math.... are you trying to pick up a tap over irregular bitmaps? like, for button-mashing operations?)
a stretched circle is still a circle.
all ranges are circular: all points at a distance from a center are within or outside that circle depending on what distance we are talking about.


I needed to work out a corresponding camera angle.
I had a shape seen at that angle, that I knew was a circle.
since all circles are regular, and I know how to write a circle mathematically, and I know the relationship between length and width of the square binding that circle was 1, I could take the measurements of the ellipse I was seeing, reverse the relationship, and come up with an angle.
that angle was the camera looking down on the object.
Ahh, then I mean I guess my issue is that I don't know how to write a circle mathematically in the first place 😛 Would you mind sharing how I might do this? 😊

The way I'm currently checking doesn't actually involve simulating a circle. It's just using the logic that if the distance between the two things is less than the imaginary circle's radius, then it's in the imaginary circle - one simple condition no matter where the two points are in relation to each other. But obviously this doesn't work when the radius changes based on which exact direction it's going off from the center (i.e. when the circle becomes stretched into an oval) 😩 Which is why I think I need to change my approach and actually mathematically model/simulate a circle/oval, but just don't know how to do that 😢
I guess my issue is that I don't know how to write a circle mathematically
well, you start with a center point, for one.
you can also draw it by tangents, but it's more complicated.
a single point and radius is way easier.... so if you're measuring from a base, make that base a middle point by working out the size of the sprite (height / 2, or top + size - offset, whatever expression)


when the circle becomes stretched into an oval
I just told you that the relationships between the boundaries of both shapes are related.

if you have a circle but need an oval related to the rectangle that the picture defines, the relationship between the measurements of that rectangle can be tied to the relationship between the measurements of an oval.
work with rectangles, not circles.
both relationships being equal, it's easier to measure everything in rectangles, not circles.

I'm sure there IS a formula for calculating a point within an oval (I just don't know/don't remember it!), but in any case you still need either the center, the focal point(s), or the difference between radii.
when everything is expressed as Cartesian coordinates, it's a simple matter of comparing greater or lesser than.
I just told you that the relationships between the boundaries of both shapes are related.
I know, haha... I never meant to imply they weren't. The point is the fact that the circle being stretched into an oval does render the particular method I was using ineffective, or at least I couldn't figure out how to adapt it to account for the loss of radial symmetry (which my method depended on). That's all. I never thought the circle and oval were totally different 👋

a single point and radius is way easier.... so if you're measuring from a base, make that base a middle point by working out the size of the sprite (height / 2, or top + size - offset, whatever expression)
if you have a circle but need an oval related to the rectangle that the picture defines, the relationship between the measurements of that rectangle can be tied to the relationship between the measurements of an oval.
work with rectangles, not circles.
both relationships being equal, it's easier to measure everything in rectangles, not circles.
Thanks so much for pointing me in a direction! I feel like this should make better sense to me in a less tired state haha. So I'll come back to play with this information when I can more easily conceptualize it after a good night's sleep 😢 It's probably super simple and I'm probably just being dumbed down by my own exhaustion right now.

Thanks again for taking the time and patience with me!
It's probably super simple and I'm probably just being dumbed down by my own exhaustion right now.
I wrote the problem down, and I'm looking at it, and I'm sure I have it backwards.
I'm telling you, it's simpler than it seems, but I'm also blanking out right now.

usually in math, Cartesian coordinates are the unknown.... you use math to work them out.
here, it's the opposite, *everything* we have is coordinates.
it's a question of establishing how the usual dimensions come up (as center + offset), to which you then plug in the coordinates and out comes the shape you start with.

me flooent in maffs, for reelz!
I wrote the problem down, and I'm looking at it, and I'm sure I have it backwards.
I'm telling you, it's simpler than it seems, but I'm also blanking out right now.
You and me both, haha, for real thanks though 😍 I'm sure I can do something with what you gave me, I just need a rested brain to work with it haha~
I *think* I got it.
after some very improper and inefficient scribbling in not-paper and not-the-proper-notation, I indeed confirmed I had it backwards.
it's a logical problem, and I was analyzing it as a mathematical problem.

o is the center of reference, which would be offset from the base of the sprites.
I was trying to work it out for any R as a function of angle.... but it might not be needed.
I'm sure I missed a couple of steps. I'm rustier than I thought.

nuevo-1.jpg


explanation:
any point P belongs to circle C, IF, the radius Rp is equal or lesser than the circle's own radius R.
point Q belongs to ellipse E, IF radius Rq obeys the limits as stated.
from there, limit to Qx becomes a function of cosine, and limit to Qy becomes a function of sine, since:
when B -> 180
- cos(b) -> 1 and Qx * cos(b)= Qx * 1 <-> R', so Qx < R' ? -> True.
- sin(b) -> 0 and Qy * sin(b) = Qy * 0, so Qy < R ? -> True

even if I'm wrong.... I love problems like this... I'd work on it all day, but I'm tired lol
@gstv87 Wow!! Yeah that's so much more math that I just totally dumped out of my brain after high school 😢 Thank you for sitting down to do all that!! But I'm so sorry I couldn't report back here sooner due to being in classes all day, but I did do some more Googling between classes earlier and found an equation that should supposedly work:

Where the center of the circle is h,k and the point we're checking is x,y:

((x-h)^2 / radiusX) + ((y-k)^2 / radiusY) <= 1

If true, it's in the ellipse; if false, it's outside of it. I haven't put it to the test yet though. But at least I can kind of understand it now that I've slept and taken my Adderall haha~

If it doesn't work or isn't accurate, I'll try to figure out your way... although I'm really hoping this way works because even with a rested brain I still literally have no idea what I'm looking at on that image 😅 It looks similar to what you started doing at the top, but then you got into trigonometry and my head kinda exploded - I learned trig for like two weeks in high school, barely understood it, and then never really had to touch it again lmaoo

But for real, thank you for taking the time to help me 😍 At the very least, the direction you pushed me in influenced my later search attempts that led me to the equation above - again, I'll report back on how it works!
((x-h)^2 / radiusX) + ((y-k)^2 / radiusY) <= 1
that could be...
I was heading towards that out of that Q€E expression, but ended up dropping it.

both stem from sin^2 + cos^2 = 1, except that's where I realized it shouldn't be = because that'd put the point ON the edge, and you're checking for IN the shape.... so it had to be <= 1, or <= R.

Original thread: https://forums.rpgmakerweb.com/threads/help-with-math-to-calculate-an-oval-shaped-range.147328/