Page 1 of 1
Some help with math and polygons
Posted: Tue Jul 15, 2014 12:08 am
by soulaymenc
Hello every one.
I am trying to highlight clicked objects on the screen, so basically each object can have different x,y coordinates (of course) but with different origin offsets as well (ox and oy). I highlight an object by drawing a rectangle that surrounds the object's borders.
So the issues I'm having are:
1/ How do I check if an object was clicked? (Check if the mouse click points exists in the polygon with a particular rotation)
2/ How to perfectly draw the rectangle around the rotated object
Thank you very much.
Re: Some help with math and polygons
Posted: Tue Jul 15, 2014 5:38 am
by micha
2) Make a loop over all coordinates and collect the smallest and largest x- and y-coordinate. Then draw a rectangle using these coordinates:
Code: Select all
local minX = -math.huge
local maxX = math.huge
for i = 1, nNodes do
minX = math.max(minX,rectangleCoordinateX[i])
maxX = math.min(maxX,rectangleCoordinateX[i])
minY = math.max(minY,rectangleCoordinateY[i])
maxY = math.min(maxY,rectangleCoordinateY[i])
end
-- and in draw:
love.graphics.rectangle('line',minX,minY,maxX-minX,maxY-minY)
1) Do you need pixel-perfect clicking? If no, then I suggest using the rectangle from point 2) as a collision area. Checking a click against a rectangle is easy. If you want to do it pixel-perfect, then the math will be more involved.
Re: Some help with math and polygons
Posted: Wed Jul 16, 2014 4:53 pm
by davisdude
Check out this thing I did a while ago if you want to get REALLY deep into it. Just for you I added random-polygon generation.
Re: Some help with math and polygons
Posted: Wed Jul 16, 2014 7:42 pm
by soulaymenc
davisdude wrote:Check out this thing I did a while ago if you want to get REALLY deep into it. Just for you I added random-polygon generation.
Oh thank you! It does what I need
I also found this useful
http://math.stackexchange.com/questions ... d-on-angle
Thank you very much for helping!
I'm working on a new project and hope to release it in few weeks, still a lot of work to do, and questions to be answered
Re: Some help with math and polygons
Posted: Wed Jul 16, 2014 8:27 pm
by davisdude
No problem! (In case you didn't know) You can read through the files by changing the extension to a .zip.
All the math is within the file MLib.lua, and the code that makes it so that you can see it is in main.lua.