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.
Some help with math and polygons
- soulaymenc
- Prole
- Posts: 36
- Joined: Thu Jul 11, 2013 2:03 pm
- Contact:
Some help with math and polygons
This world is so strange.
Re: Some help with math and polygons
2) Make a loop over all coordinates and collect the smallest and largest x- and y-coordinate. Then draw a rectangle using these coordinates:
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.
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)
Check out my blog on gamedev
Re: Some help with math and polygons
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.
- Attachments
-
- MLib.Shape Demonstration.love
- (10.55 KiB) Downloaded 123 times
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
- soulaymenc
- Prole
- Posts: 36
- Joined: Thu Jul 11, 2013 2:03 pm
- Contact:
Re: Some help with math and polygons
Oh thank you! It does what I need I also found this useful http://math.stackexchange.com/questions ... d-on-angledavisdude 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.
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
This world is so strange.
Re: Some help with math and polygons
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.
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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Who is online
Users browsing this forum: No registered users and 1 guest