A while back I found a ray tracing function for 3D vectors that I used to check for spacecraft/planet collisions. By chopping out the Z axis stuff, I got it a little smaller and working for a 2D screen. Detects when a circle/ball in question; of a certain radius, touches or intersects a line in question. Needed are the start and end points of a line, a circle center position and radius.
Maybe useful, maybe not.
EDIT: Ah, turns out it's not. It's for infinite rays crossing the ball, rather than a ball intersecting lines. Changing ball4.y to 350 reveals the issue
Maybe useful, maybe not.
EDIT: Ah, turns out it's not. It's for infinite rays crossing the ball, rather than a ball intersecting lines. Changing ball4.y to 350 reveals the issue
Code: (Select All)
'Intersection test
'Up & down arrows change the radius of balls 1 - 3
'left and right arrows move ball 4 across its line
TYPE V2
x AS SINGLE
y AS SINGLE
END TYPE
DIM radius AS INTEGER
radius = 25
DIM AS V2 ball1, ball2, ball3, ball4, line1s, line1e, line2s, line2e, line3s, line3e, line4s, line4e
ball1.x = 50: ball1.y = 200
line1s.x = 100: line1s.y = 25
line1e.x = 100: line1e.y = 300
ball2.x = 150: ball2.y = 200
line2s.x = 175: line2s.y = 25
line2e.x = 175: line2e.y = 300
ball3.x = 300: ball3.y = 200
line3s.x = 310: line3s.y = 25
line3e.x = 310: line3e.y = 300
ball4.x = 50: ball4.y = 450
line4s.x = 200: line4s.y = 350
line4e.x = 400: line4e.y = 550
SCREEN _NEWIMAGE(800, 600, 32)
DO
CLS
IF _KEYDOWN(20480) THEN radius = radius - 1
IF _KEYDOWN(18432) THEN radius = radius + 1
IF _KEYDOWN(19200) THEN ball4.x = ball4.x - 1
IF _KEYDOWN(19712) THEN ball4.x = ball4.x + 1
IF Intersect(line1s, line1e, ball1, radius) < 0 THEN c& = &HFF00FF00 ELSE c& = &HFFFF0000
CIRCLE (ball1.x, ball1.y), radius, c&
LINE (line1s.x, line1s.y)-(line1e.x, line1e.y), c&
IF Intersect(line2s, line2e, ball2, radius) < 0 THEN c& = &HFF00FF00 ELSE c& = &HFFFF0000
CIRCLE (ball2.x, ball2.y), radius, c&
LINE (line2s.x, line2s.y)-(line2e.x, line2e.y), c&
IF Intersect(line3s, line3e, ball3, radius) < 0 THEN c& = &HFF00FF00 ELSE c& = &HFFFF0000
CIRCLE (ball3.x, ball3.y), radius, c&
LINE (line3s.x, line3s.y)-(line3e.x, line3e.y), c&
IF Intersect(line4s, line4e, ball4, 30) < 0 THEN c& = &HFF00FF00 ELSE c& = &HFFFF0000
CIRCLE (ball4.x, ball4.y), 30, c&
LINE (line4s.x, line4s.y)-(line4e.x, line4e.y), c&
_DISPLAY
_LIMIT 30
LOOP UNTIL _KEYDOWN(27)
END
FUNCTION Intersect (lsrt AS V2, lend AS V2, bpos AS V2, rd AS INTEGER)
dx! = lend.x - lsrt.x: dy! = lend.y - lsrt.y
A## = dx! * dx! + dy! * dy!
B## = 2 * dx! * (lsrt.x - bpos.x) + 2 * dy! * (lsrt.y - bpos.y)
C## = (bpos.x * bpos.x) + (bpos.y * bpos.y) + (lsrt.x * lsrt.x) + (lsrt.y * lsrt.y) + -2 * (bpos.x * lsrt.x + bpos.y * lsrt.y) - (rd * rd)
Intersect = (B## * B##) - 4 * A## * C##
'if Intersect < 0 then no intersection; if = 0 then tangent; if > 0 then intersection
END FUNCTION
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
sha_na_na_na_na_na_na_na_na_na: