Check if a frame is on a certain angle of another frame

Hello. My issue is that I am trying to create a check to see if a Frame is in one of the four sections created by another frame that is diamond-shaped.

I tried using absolute position and cartesian math, but that didn’t quite work as Roblox AbsolutePosition for the frames work by increasing the Y as it goes down.

I figured out that checking the angle of the frame relative to the core frame would be an easier solution, but I have no idea about how to do that. Any help would be appreciated!

We can actually use some trigonometry for this! Which if you’re not familiar with, it is ideal to learn it before reading this solution!

In trig, each trig function (sin, cos, tan) have a certain sign in the 4 quadrants of the cartesian plane.
So to put it simply, in the first quadrant (a quadrant is one of the squares that make the plane) or in your picture is deined as section A, any triagnle built there will have all the trig functions be positive, in N°2 only the sin, in N°3 only tan, in N°4 only cos.

trigquadexpl1

Things might’ve become complicated, but let me demonstrate: Let’s say the mousePos is in the first quadrant, we wanna prove that it’s there!

trigquadexpl2

As you can see, the triangle formed by the mousePos (where the opposite side is Mouse.Y the adjacent is Mouse.X and the hypotunuse is the MousePos’s length or in other words MousePos.magnitude, and we have the angle θ which we are going to use the trig function on) is found in the first quadrant, meaning that what we have to do is as follows: check if sin(θ) and cos(θ) and tan(θ) are all positive.

trigquadexpl3

This example shows what happens if mousePos is in first quadrant

And that’s actually it! It works. We calculate the sin(θ) and cos(θ) and tan(θ) and then we put all of our if statments, if all are positive than the mousePos is in first quadrant, if just sin is positive then mousePos is in second quadrant, if just tan is positive than it’s in third quadrant, if just cos is positive than it’s in fourth quadrant.

And we can use math.sign() to check if it’s negative or positive, this function returns 1 if it’s positive, and returns -1 if it’s negative (it does as well return 0 if the input was 0, but in this situation we shouldn’t meet a case where it’s 0)

local adjacent = mouse.X
local opposite = mouse.Y
local hypotenuse = Vector2(adjacent, hypotenuse).magnitude --there isn't a mouse.Position so we make a 2d vector with the mouse x and y and .magnitude is that vector's length

if math.sign(opposite/hypotenuse) == 1  and math.sing(adjacent/hypotenuse) == 1 and math.sign(opposite/adjacent) == 1 then
   print("mouse is in section A")
elseif math.sign(opposite/hypotenuse) == 1 then
   print("mouse is in section B")
elseif math.sign(opposite/adjcent)  == 1then
   print("mouse is in section C")
elseif math.sign(adjacent/hypotenuse) == 1  then
   print("mouse is in section D")
end

There we go! Not sure if it works because I have no means of testing this, but hopefully it does!
And sorry if I didn’t go in depth in explaining this, the basic stuff, because most of it you gotta learn before reading this! So yeah, learn trigonometry, and if you got any question ask

5 Likes

But there is another problem. You see, the Core GUI can be located on any side of the screen, not just in the middle.

And after testing your code, for some reason it considers the whole screen Section A and just above the PlayerCoreGui Section C. I don’t know if you considered it is a diamond as well, that means it makes an X between all Quadrants, not a straight up cross.

Oh yeah, that might be a problem, if the core gui or in other words the center, is rotated.

Yes, else I would’ve used Cartesian math checking if X > Y and Y > 0 to see if it was section A, and etc.

I tried doing that before as stated, but the best solution coming in my mind was really to check the angles between the two frames instead of their positions that aren’t really always accurate.

Yeah, so you gotta calculate the angle somehow, and check if it’s between 0 and 90, or between 90 and 180 ect…

1 Like

https://gyazo.com/c3523c3daa9e7ef9aeb48a3c9f868a2b

You must also remember that the Y on mouse.Y increases as it goes down to the bottom of the screen, and there’s no negative X or negative Y, only the size of the screen.

2 Likes

So then you can edit the code and subtract half of the absolute size.

local adj = mouse.X -(--[[the GUI]]).AbsoluteSize.X/2
local opp = mouse.Y - (--[[the GUI]]).AbsoluteSize.Y/2

This unfortunately will invoke the need for math.abs since the function used to get the area literally just checks if the mouse is further out upwards than it is upwards. So first get the sign of the Y/X coordinate, then that narrows it down to 2 options etc.

Good luck!! :slight_smile:

1 Like

Sadly even by doing that the code still results in the same outputs :expressionless:

Would you mind showing your updated code?

local adjacent = mouse.X - Core.AbsoluteSize.X/2
local opposite= mouse.Y - Core.AbsoluteSize.Y/2
local hypotenuse = Vector2(adjacent, opposite).magnitude

if math.sign(opposite/hypotenuse) == 1  and math.sing(adjacent/hypotenuse) == 1 and math.sign(opposite/adjacent) then
   print("mouse is in section A")
elseif math.sign(opposite/hypotenuse) == 1 then
   print("mouse is in section B")
elseif math.sign(opposite/adjcent)  == 1then
   print("mouse is in section C")
elseif math.sign(adjacent/hypotenuse) == 1  then
   print("mouse is in section D")
end

I managed to find all 4 quadrants with cartesian math, but using angles still looks like the best option.