Page 1 of 1

How to detect a collision between two points in an array and a rotating line?

Posted: Sat Jul 04, 2020 10:49 pm
by Charlie Gallie
Hey,

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
And 'newPoint' is simply:

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
I'm more after your thoughts on how I could do this, the actual programming side doesn't matter.

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!

Re: How to detect a collision between two points in an array and a rotating line?

Posted: Sun Jul 05, 2020 2:53 am
by duaner
A line can be described as "y * sin(a) + x * cos(a) - p = 0". If the line goes through the origin, you can ignore the "-p". If you know the angle (a), just fill in the x value from your point, then solve for y (or vice versa).

Code: Select all

y = (x * cos(a)) / (- sin(a))
If y is close to your point's y value, you have an intersection.

(Note that this assumes that your coordinates are set up the way mathematicians set theirs up, which is not true in computer graphics. You'll have to invert (negate) the y value.)

Re: How to detect a collision between two points in an array and a rotating line?

Posted: Mon Jul 06, 2020 10:38 pm
by Charlie Gallie
duaner wrote: Sun Jul 05, 2020 2:53 am A line can be described as "y * sin(a) + x * cos(a) - p = 0". If the line goes through the origin, you can ignore the "-p". If you know the angle (a), just fill in the x value from your point, then solve for y (or vice versa).

Code: Select all

y = (x * cos(a)) / (- sin(a))
If y is close to your point's y value, you have an intersection.

(Note that this assumes that your coordinates are set up the way mathematicians set theirs up, which is not true in computer graphics. You'll have to invert (negate) the y value.)
Thank you very much for the help.