-- Collison Triangle -- -- 点が三角形の内側か判定したい - Thoth Children -- http://www.thothchildren.com/chapter/5b267a436298160664e80763 function love.load() polys = { {200, 30, 300, 30, 250, 200}, {400, 100, 600, 60, 500, 200}, {120, 300, 300, 250, 500, 400}, {60, 20, 180, 180, 20, 100}, } hits = {} for i=1, #polys do hits[i] = false end mx, my = 0, 0 end function getCrossVec2D(vx1, vy1, vx2, vy2) return (vx1 * vy2 - vx2 * vy1) end function checkHitTri(pos, xa, ya) local b1, b2, b3 local x1, y1, x2, y2, x3 x1, y1 = pos[1], pos[2] x2, y2 = pos[3], pos[4] x3, y3 = pos[5], pos[6] b1 = (getCrossVec2D(x2 - x1, y2 - y1, xa - x1, ya - y1) < 0) b2 = (getCrossVec2D(x3 - x2, y3 - y2, xa - x2, ya - y2) < 0) b3 = (getCrossVec2D(x1 - x3, y1 - y3, xa - x3, ya - y3) < 0) return ((b1 == b2) and (b2 == b3)) end function love.update(dt) mx, my = love.mouse.getPosition() for i = 1, #polys do hits[i] = checkHitTri(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) love.graphics.print("mx, my = " .. mx .. "," .. my, 8, 24) end function love.keypressed(key, scancode, isrepeat) if key == "escape" then love.event.quit() end end