Log In  

Anyone already have a function coded for checking to see if an x/y point is inside a rectangle with given points?

They're not "straight" rectangles, they'll be moving so I'm drawing them with 4 x/y points. I've found various formulas for getting the answer but wondering maybe someone had that done already? ...just being lazy... :)

P#27427 2016-08-26 22:03 ( Edited 2016-08-27 16:10)

:: dw817

I don't have PICO yet but I've been programming in several others game and application making languages for years now. Here is an easy way to do it.

REM x and y are coordinates to check

recx1=50 ' these are the 4-points of a rectangle (left)
recx2=150 ' (right)
recy1=25 ' (top)
recy2=200 ' (bottom)

if x>=recx1 and x<=recx2 and y>=recy1 and y<=recy2 then
print"Inside rectangle."
endif

P#27428 2016-08-26 22:21 ( Edited 2016-08-27 02:23)

Thanks...I have that type of detection already, and that works great for straight squares when you only have 2 points, but I have all 4 - essentially a polygon that could be rotating.

I found this after some more digging and it seems to work. Still doing testing on it. Not sure how it will do performance-wise but I guess I'll deal with that later if it crops up.

https://love2d.org/wiki/PointWithinShape

P#27429 2016-08-26 22:27 ( Edited 2016-08-27 02:27)
:: dw817

Oh ... a polygon. Ouch. I know one way to do it, would be horrendously slow. Yes, there should be a good formula to deal with that. Are you making a type of QIX ?

Just thinking. If you have the polygon appear as a UNIQUE color, you could simply scan what the color pixel is underneath the point to check. If it matches the UNIQUE then you are inside, if not, you aren't. No need for complex algorithms or formulae. :)

P#27432 2016-08-26 22:35 ( Edited 2016-08-27 02:36)

You could do a first pass with a simple AABB and if it's inside that maybe two tests for the two triangles such a rectangle is made of?

E.g. something like in this ..

http://stackoverflow.com/questions/2049582/how-to-determine-if-a-point-is-in-a-2d-triangle

P#27467 2016-08-27 09:21 ( Edited 2016-08-27 13:21)
:: dw817

Toast, try to look for the target pixel color beneath the polygon. I think this will work and be the easiest and fastest way to do it, for any polygon, triangle, trapezoid, or something that has even more edges.

Just reserve 1 color of the 15 to be the one to look for.

They used methods like this for early collision detection in videogames. I used it myself for the Commodore Amiga in a mock-up Space Invaders to determine if a shot hit something.

THEN I would scan everything and determine which was closest to the shot and that would've been the target.

Back when computers were REAL slow and you couldn't scan every sprite all the time. :)

P#27475 2016-08-27 12:10 ( Edited 2016-08-27 16:10)

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2019-12-12 11:02 | 0.013s | 4194k | Q:22