Some help with math and polygons

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
soulaymenc
Prole
Posts: 36
Joined: Thu Jul 11, 2013 2:03 pm
Contact:

Some help with math and polygons

Post 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.
This world is so strange.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Some help with math and polygons

Post 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.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Some help with math and polygons

Post 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. :)
Attachments
MLib.Shape Demonstration.love
(10.55 KiB) Downloaded 122 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
User avatar
soulaymenc
Prole
Posts: 36
Joined: Thu Jul 11, 2013 2:03 pm
Contact:

Re: Some help with math and polygons

Post 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! :awesome: 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.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Some help with math and polygons

Post 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.
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
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], pgimeno and 62 guests