rect in poly collisions
BlitzMax Forums/BlitzMax Beginners Area/rect in poly collisions
| ||
Hi all, I need to find out if a rect is in a rotated rect so i can have little tiles lighting up and looking all pretty, but being the maths spanner that i am i'm having a bit of trouble and was hoping someone could help. basically, i have a rect (x,y,width,height) and a grid type structure which slowly rotates around a central point on screen. i basically need to know which square the non-rotated rect is in. each section in the grid has four corners. I'm assuming (hence the title) that i'll need some basc form of polygon collision to be able to do this. Any clues... Cheers Charlie |
| ||
Ok, i realized that i can get away with a point in poly thing. Here is my working code for a point in poly function. The odd way i'm drawing the poly is not an issue in my game as i won't actually be needing to draw the poly.SuperStrict Type poly_vert Field x:Float Field y:Float EndType Local poly:poly_vert[4] For Local i:Int=0 To 3 poly[i]=New poly_vert Next poly[0].x=130 poly[0].y=100 poly[1].x=230 poly[1].y=130 poly[2].x=130 poly[2].y=160 poly[3].x=70 poly[3].y=130 Function point_in_poly:Int( poly:poly_vert[], num_verts:Int, px:Float,py:Float ) Local nx:Int, ny:Int, loop:Int ' Clockwise order. For loop = 0 To num_verts-1 ' generate a 2d normal ( no need To normalise ). nx = poly[ ( loop + 1 ) Mod num_verts ].y - poly[ loop ].y; ny = poly[ loop ].x - poly[ ( loop + 1 ) Mod num_verts ].x; Local x:Float = px- poly[loop].x Local y:Float = py- poly[loop].y ' Dot with edge normal To find side. If ( ( x * nx ) + ( y * ny ) > 0 ) Return False; EndIf Next Return True; End Function Graphics 640,480,0,60 While Not KeyHit(key_escape) Local thisPoly:Float[]=[poly[0].x,poly[0].y,poly[1].x,poly[1].y,poly[2].x,poly[2].y,poly[3].x,poly[3].y] DrawPoly thispoly If point_in_poly(poly,4,MouseX(),MouseY())=True DrawText "in poly",10,10 EndIf Flip;Cls Wend |
| ||
I'm assuming (hence the title) that i'll need some basc form of polygon collision to be able to do this. Not necessarily... Instead of thinking of the quad (rect) rotating look from it's perspective and rotate the points the opposite way. Then you've just got a simple "point inside rect" test. |
| ||
I tried this point in poly and it didnt work in all cases.. https://drive.google.com/open?id=0B2xTXNp3qtJXdzRjNGhJQUM3LTg |
| ||
- wrap it in the according code-bbcode to preserve line intendation - maybe provide the "non working" example - maybe there is just a little misconception in the original code which needs a fix bye Ron |
| ||
Here derron an example ive made.. https://drive.google.com/open?id=0B2xTXNp3qtJXdzRjNGhJQUM3LTg Ill add the code later.. If desired.. |