-- Collison convex polygon function love.load() polys = { {60, 100, 150, 180, 150, 300, 60, 380}, {160, 60, 440, 60, 360, 180, 240, 180}, {470, 130, 560, 170, 550, 270}, {420, 280, 540, 300, 540, 420, 470, 450, 370, 350}, {260, 280, 320, 320, 320, 400, 260, 440, 190, 400, 190, 320}, } hits = {} for i = 1, #polys do hits[i] = false end end function getCrossVec2D(vx1, vy1, vx2, vy2) return (vx1 * vy2 - vx2 * vy1) end function checkInConvexPoly(pos, mx, my) local cc = {} local x0, y0, x1, y1 x0, y0 = pos[#pos-1], pos[#pos] for i = 1, #pos-1 ,2 do x1, y1 = pos[i], pos[i+1] table.insert(cc, (getCrossVec2D(x1 - x0, y1 - y0, mx - x1, my - y1) < 0)) x0, y0 = x1, y1 end local c0, c1 c0 = cc[1] for i = 2, #cc do c1 = cc[i] if c0 ~= c1 then return false end c0 = c1 end return true end function love.update(dt) local mx, my mx, my = love.mouse.getPosition() for i = 1, #polys do hits[i] = checkInConvexPoly(polys[i], mx, my) end end function love.draw() -- clear canvas love.graphics.clear(0, 0, 0, 1) -- draw polygon for i = 1, #polys do if hits[i] then love.graphics.setColor(1, 0, 0, 1) else love.graphics.setColor(0, 1, 0, 1) end love.graphics.polygon("fill", polys[i]) end -- draw text love.graphics.setColor(1, 1, 1, 1) love.graphics.print("ESC to exit.", 8, 8) end function love.keypressed(key, scancode, isrepeat) if key == "escape" then love.event.quit() end end