## How to know if a object is [top|bottom|left|right] using another object?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
gcmartijn
Prole
Posts: 20
Joined: Sat Dec 28, 2019 6:35 pm

### How to know if a object is [top|bottom|left|right] using another object?

H!

I thought it was easy, and I guess I will figure it out but maybe someone already made this?

Code: Select all

function something(x,y, x1,y1, x2, y2)
-- here I can simply check that x > then x1 and x2
-- here I can simply check that x < then x1 and x2
-- here I can simply check that y > then y1 and y2
-- here I can simply check that y < then y1 and y2
-- but the combination I don't understand, to get the output I want.

return [top|bottom|left|right]
end

--rect-----
BBBBBBBB
BBBBBBBB A
BBBBBBBB
BBBBBBBB

Code: Select all

local bla = something(a.x, a.y, b.x1, b.y1, b.x2, b.y2)
bla == left

--rect-----
BBBBBBBB
BBBBBBBB
BBBBBBBB
BBBBBBBB
A

Code: Select all

local bla = something(a.x, a.y, b.x1, b.y1, b.x2, b.y2)
bla == bottom


pgimeno
Party member
Posts: 2338
Joined: Sun Oct 18, 2015 2:58 pm

### Re: How to know if a object is [top|bottom|left|right] using another object?

Do you mean a point with respect to a rectangle, or one rectangle with respect to another?

What happens in this case?

Code: Select all

BBBBBBBB
BBBBBBBB
BBBBBBBB
BBBBBBBB
A

Is it considered bottom? Right? Doesn't matter?

Code: Select all

BBBBBBBB
BBBBBBBB
BBBBBBBB
BBBBBBBB
A


gcmartijn
Prole
Posts: 20
Joined: Sat Dec 28, 2019 6:35 pm

### Re: How to know if a object is [top|bottom|left|right] using another object?

Good point, need to explain it better.

Code: Select all

BBBBBBBB
BBBBBBBB
BBBBBBBB
BBBBBBBB
A

output: left side

Code: Select all

BBBBBBBB
BBBBBBBB
BBBBBBBB
BBBBBBBB
A


output: left side

You can say this maybe:
draw virtual 2 horizontal lines and 2 vertical lines around the rect.
And then check on which side the x,y point is.

Code: Select all

--|-------------|-
--|BBBBBBBB|
--|BBBBBBBB|
--|BBBBBBBB|
--|BBBBBBBB|
--|-------------|


For example https://2dengine.com/?p=intersections#P ... _rectangle
There you can see those grey lines.

The only difference would be that in that example two lines light up in the four corners, that needs to be one.
Screenshot 2020-11-22 at 12.27.26.png (17.8 KiB) Viewed 211 times

pgimeno
Party member
Posts: 2338
Joined: Sun Oct 18, 2015 2:58 pm

### Re: How to know if a object is [top|bottom|left|right] using another object?

I'm not sure I'm understanding correctly, but...

Code: Select all

local function pointPosition(x, y, x1, y1, x2, y2)
if y2 < y then return "up" end
if y1 > y then return "bottom" end
if x1 > x then return "right" end
if x2 < x then return "left" end
return "inside"
end

is that what you want? The image contradicts what you said about left side, so I'm a bit confused.

gcmartijn
Prole
Posts: 20
Joined: Sat Dec 28, 2019 6:35 pm

### Re: How to know if a object is [top|bottom|left|right] using another object?

Thanks, I see now that this is not a ideal situation for a problem I try to solve.
I will try to avoid this and re-thing about another solution.

Maybe at some point I need to have the same function like https://2dengine.com/?p=intersections#P ... _rectangle so a point can have with multiple output for the corner. For example: [bottom,left]

RNavega
Prole
Posts: 43
Joined: Sun Aug 16, 2020 1:28 pm

### Re: How to know if a object is [top|bottom|left|right] using another object?

You could have two functions, one for a horizontal comparison and another for vertical comparison. So for example the horizontal function just returns if the object is to the left, center, or right of the reference object.

### Who is online

Users browsing this forum: No registered users and 62 guests