GUI overlapping detection isn't working

Yeah I know, there are a bunch of posts floating around about this subject, but for whatever reason when I try making my own code to check and prevent the “X” markers from getting outside of the frame, it doesn’t work and the print function continues to return true even when the markers leave the main crafting frame.

function GraphModule:DrawEquation()
	self:CalcEquation() -- Calculates the position of points from an equation
	for y, x in pairs(self.LineData) do	-- self.LineData is a table containing the calculated points

		local point = self:CreatePoint() -- self-explanatory

		print(CheckBounds(point, self.Board)) -- The print function continues to return true even when the points leave the main frame
		-- I don't think this bit is causing the problem so you can ignore
		point.Position =*POINT_SPACE*point.Size.X.Scale, 0, STARTPOINT.Y.Scale+y*POINT_SPACE*point.Size.Y.Scale, 0)
		if math.abs(y)+1 == len(self.LineData) then	-- if they equal, then that specific point is the final point
			STARTPOINT = point.Position

function CheckBounds(gui1,gui2)
	local gui1_topLeft,gui1_bottomRight = gui1.AbsolutePosition, gui1.AbsolutePosition + gui1.AbsoluteSize
	local gui2_topLeft,gui2_bottomRight = gui2.AbsolutePosition, gui2.AbsolutePosition + gui2.AbsoluteSize
	return ((gui1_topLeft.x < gui2_bottomRight.x and gui1_bottomRight.x > gui2_topLeft.x) and (gui1_topLeft.y < gui2_bottomRight.y and gui1_bottomRight.y > gui2_topLeft.y))

UI can be a pain, are you sure its Zindex is higher than the frame? (just guessing)

Zindex obviously doesn’t matter when CheckBounds is pure maths

I believe it’s working correctly for the most part although I noticed you’re checking the opposite bounds:
(This was take from the code provided with no alterations just asterisk pointing out what you’re comparing)

-- Your code just checks whether any bit is within the main frame, not whether the whole box (frame) is contained within the bounding regions.
-- First check
local is_in_x_bounds = (gui1_**topLeft**.x < gui2_**bottomRight**.x and gui1_**bottomRight**.x > gui2_**topLeft**.x) 
-- // Second check
local is_in_y_bounds = (gui1_**topLeft**.y < gui2_**bottomRight**.y and gui1_**bottomRight**.y > gui2_**topLeft**.y)

-- // So you might want to compare bottom right with bottom right, and top left with top left

Asides from the print, you’re not using the function in the provided script.
(If you’re just trying to make it not show outside of the provided frame you can flip on “ClipDescendants” on the main frame)

I should add, the larger background frame (gui2) has its anchorpoint set to 0.5, 0.5. Would this affect the frame’s absolute position?

Anchor point has no effect on absolute position, only relative position.

For some odd reason, printing gui1_topLeft and gui1_bottomRight gives the exact same positions for all of the “X” markers. This is obviously not the case when you can clearly see the “X” markers are all in different positions. Why is this happening?


Turns out, I’m stupid and completely missed the fact that the CheckBounds function is being called BEFORE I set the position of the “X” markers.
This would obviously mean gui1_topLeft, gui1_bottomRight would all return the same vector2 value as the position of each marker hasn’t been set yet.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.