I'm just wondering how I could go about detecting the collision between a line and set of two points within an array/table. I say array/table because honestly I'm not sure what the correct terms are.
Once the line and point intersects, I'd like it to return the X and Y position of the point.
Despite it's not very necessary, here's the code for the rotating line and appending points to the array named 'points':
Code: Select all
local angle = 0
nPoint = require("newPoint")
points = {}
circle = {t = "fill", x = 640, y = 360, r = 4}
line = {x1 = 0, y1 = 0, x2 = 0, y2 = 0}
function love.load()
for i = 0, 9
do
x = nPoint.point(math.random(10, 600))
points[i] = x
end
end
function love.update(dt)
width = love.graphics.getWidth()
height = love.graphics.getHeight()
love.timer.sleep(.01)
angle = angle + dt * math.pi/2
angle = angle % (2*math.pi)
end
function love.draw()
love.graphics.setColor(1, 1, 1)
love.graphics.circle(circle.t , circle.x, circle.y, circle.r)
love.graphics.translate(circle.x, circle.y)
love.graphics.rotate(angle)
love.graphics.translate(-circle.x, -circle.y)
love.graphics.line(circle.x + width, circle.y + height, circle.x - width, circle.y - height)
end
Code: Select all
module(..., package.seeall)
function point(px, py)
local object = {}
object.posX = px
object.posY = py
function object:getPosX()
return object.posX
end
function object:getPosY()
return object.posY
end
return object
end
A thought I had was find the X and Y cords for every point along the line and search the array for matching points, but I'm certain there is a more efficient method, I'm just not sure what it is.
If I didn't explain that well, completely feel free to ask for clarification.
Thanks a bunch in advance!