Angle Collisions
#64
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

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:
Reply


Messages In This Thread
Angle Collisions - by james2464 - 10-15-2022, 01:39 AM
RE: Angle Collisions - by bplus - 10-15-2022, 02:16 AM
RE: Angle Collisions - by james2464 - 10-15-2022, 04:01 AM
RE: Angle Collisions - by James D Jarvis - 10-15-2022, 10:39 PM
RE: Angle Collisions - by Pete - 10-15-2022, 10:46 PM
RE: Angle Collisions - by James D Jarvis - 10-15-2022, 11:24 PM
RE: Angle Collisions - by james2464 - 10-16-2022, 12:17 AM
RE: Angle Collisions - by OldMoses - 10-16-2022, 01:14 PM
RE: Angle Collisions - by james2464 - 10-16-2022, 07:11 PM
RE: Angle Collisions - by OldMoses - 10-18-2022, 08:20 PM
RE: Angle Collisions - by james2464 - 10-18-2022, 10:47 PM
RE: Angle Collisions - by OldMoses - 10-19-2022, 12:23 AM
RE: Angle Collisions - by Pete - 10-16-2022, 12:13 AM
RE: Angle Collisions - by Pete - 10-16-2022, 12:50 AM
RE: Angle Collisions - by bplus - 10-16-2022, 01:01 PM
RE: Angle Collisions - by bplus - 10-16-2022, 01:27 PM
RE: Angle Collisions - by OldMoses - 10-16-2022, 04:02 PM
RE: Angle Collisions - by bplus - 10-16-2022, 06:46 PM
RE: Angle Collisions - by bplus - 10-16-2022, 07:45 PM
RE: Angle Collisions - by james2464 - 10-16-2022, 08:04 PM
RE: Angle Collisions - by James D Jarvis - 10-16-2022, 08:07 PM
RE: Angle Collisions - by bplus - 10-16-2022, 08:47 PM
RE: Angle Collisions - by James D Jarvis - 10-16-2022, 08:55 PM
RE: Angle Collisions - by bplus - 10-17-2022, 10:07 AM
RE: Angle Collisions - by bplus - 10-17-2022, 12:26 PM
RE: Angle Collisions - by OldMoses - 10-17-2022, 12:58 PM
RE: Angle Collisions - by bplus - 10-17-2022, 01:11 PM
RE: Angle Collisions - by james2464 - 10-17-2022, 01:57 PM
RE: Angle Collisions - by OldMoses - 10-17-2022, 02:19 PM
RE: Angle Collisions - by bplus - 10-17-2022, 02:49 PM
RE: Angle Collisions - by james2464 - 10-17-2022, 03:46 PM
RE: Angle Collisions - by bplus - 10-17-2022, 04:53 PM
RE: Angle Collisions - by james2464 - 10-17-2022, 05:27 PM
RE: Angle Collisions - by Dav - 10-18-2022, 02:22 AM
RE: Angle Collisions - by james2464 - 10-18-2022, 03:25 AM
RE: Angle Collisions - by Pete - 10-17-2022, 04:10 PM
RE: Angle Collisions - by bplus - 10-17-2022, 04:55 PM
RE: Angle Collisions - by james2464 - 10-17-2022, 05:23 PM
RE: Angle Collisions - by james2464 - 10-18-2022, 02:00 AM
RE: Angle Collisions - by Pete - 10-18-2022, 02:10 AM
RE: Angle Collisions - by Pete - 10-18-2022, 03:20 AM
RE: Angle Collisions - by james2464 - 10-18-2022, 03:57 AM
RE: Angle Collisions - by bplus - 10-18-2022, 03:27 PM
RE: Angle Collisions - by james2464 - 10-18-2022, 04:11 PM
RE: Angle Collisions - by bplus - 10-18-2022, 08:27 PM
RE: Angle Collisions - by Pete - 10-18-2022, 08:44 PM
RE: Angle Collisions - by bplus - 10-18-2022, 10:10 PM
RE: Angle Collisions - by Pete - 10-18-2022, 10:19 PM
RE: Angle Collisions - by james2464 - 10-20-2022, 12:30 AM
RE: Angle Collisions - by bplus - 10-20-2022, 02:36 AM
RE: Angle Collisions - by james2464 - 10-20-2022, 01:51 PM
RE: Angle Collisions - by Pete - 10-20-2022, 03:48 AM
RE: Angle Collisions - by bplus - 10-20-2022, 02:52 PM
RE: Angle Collisions - by james2464 - 10-20-2022, 04:21 PM
RE: Angle Collisions - by bplus - 10-20-2022, 04:37 PM
RE: Angle Collisions - by james2464 - 10-21-2022, 07:10 PM
RE: Angle Collisions - by Pete - 10-21-2022, 07:20 PM
RE: Angle Collisions - by bplus - 10-21-2022, 09:05 PM
RE: Angle Collisions - by OldMoses - 10-22-2022, 12:09 AM
RE: Angle Collisions - by james2464 - 10-22-2022, 10:29 PM
RE: Angle Collisions - by bplus - 10-22-2022, 10:59 PM
RE: Angle Collisions - by justsomeguy - 10-22-2022, 11:45 PM
RE: Angle Collisions - by Pete - 10-23-2022, 12:37 AM
RE: Angle Collisions - by OldMoses - 10-23-2022, 12:46 AM
RE: Angle Collisions - by james2464 - 10-24-2022, 04:57 PM
RE: Angle Collisions - by james2464 - 10-24-2022, 11:14 PM
RE: Angle Collisions - by bplus - 10-25-2022, 12:37 AM
RE: Angle Collisions - by james2464 - 10-25-2022, 03:25 AM
RE: Angle Collisions - by OldMoses - 10-25-2022, 10:47 PM
RE: Angle Collisions - by james2464 - 10-25-2022, 10:52 PM
RE: Angle Collisions - by OldMoses - 10-26-2022, 03:39 AM
RE: Angle Collisions - by james2464 - 10-26-2022, 03:51 PM
RE: Angle Collisions - by OldMoses - 10-26-2022, 04:18 PM
RE: Angle Collisions - by james2464 - 10-26-2022, 08:41 PM
RE: Angle Collisions - by OldMoses - 10-27-2022, 12:33 AM
RE: Angle Collisions - by james2464 - 10-27-2022, 03:36 PM
RE: Angle Collisions - by OldMoses - 10-29-2022, 12:05 AM
RE: Angle Collisions - by james2464 - 10-29-2022, 01:45 AM
RE: Angle Collisions - by james2464 - 10-30-2022, 04:41 PM
RE: Angle Collisions - by bplus - 10-30-2022, 06:16 PM
RE: Angle Collisions - by james2464 - 10-30-2022, 06:25 PM
RE: Angle Collisions - by bplus - 10-30-2022, 06:31 PM
RE: Angle Collisions - by james2464 - 10-30-2022, 06:37 PM
RE: Angle Collisions - by bplus - 10-30-2022, 06:45 PM
RE: Angle Collisions - by james2464 - 10-31-2022, 01:27 AM
RE: Angle Collisions - by bplus - 10-31-2022, 01:52 AM
RE: Angle Collisions - by TempodiBasic - 11-01-2022, 02:38 AM
RE: Angle Collisions - by bplus - 11-01-2022, 11:31 AM
RE: Angle Collisions - by james2464 - 11-01-2022, 04:15 PM
RE: Angle Collisions - by triggered - 11-01-2022, 03:03 AM
RE: Angle Collisions - by james2464 - 11-03-2022, 06:53 PM
RE: Angle Collisions - by OldMoses - 11-04-2022, 12:56 AM



Users browsing this thread: 30 Guest(s)