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... :)
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
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.
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. :)
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 ..
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. :)
[Please log in to post a comment]