Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Circle-line collision
#4
(04-16-2024, 03:59 PM)Marcus Wrote: I wrote a quick test for how collisions between moving objects (player and enemies) and the static walls should work in that 3d thing I'm working on. In the game/engine I will just check for collisions with walls close to the moving objects (a uniform grid will contain information about which walls passes through each cell). But maybe someone may still find this test useful for their own purposes:

Code:
set window "Collision", 640, 480
set redraw off

lines = []
randomize 11
for i = 1 to 8
    ln = [rnd(640), rnd(480), rnd(640), rnd(480)]
    dx = ln[2] - ln[0]; dy = ln[3] - ln[1]
    ln[6] = sqr(dx*dx + dy*dy)
    ln[4] = dx/ln[6]
    ln[5] = dy/ln[6]
    lines[sizeof(lines)] = ln
next

obj = Object(320, 240, 16)

while not keydown(KEY_ESCAPE, true)
    'obj.x = mousex(); obj.y = mousey()
    if keydown(KEY_LEFT)  obj.x = obj.x - 4
    if keydown(KEY_RIGHT)  obj.x = obj.x + 4
    if keydown(KEY_UP)  obj.y = obj.y - 4
    if keydown(KEY_DOWN)  obj.y = obj.y + 4   
    PushOut(obj, lines)
   
    set color 0, 0, 0
    cls
    set color 255, 255, 255
    DrawLines(lines)
    obj.Draw()
   
    set caret width(primary)/2, 0
    center "Use the arrow keys to move the circle around"
   
    redraw
    fwait 60
wend

function DrawLines(lines)
    foreach ln in lines  draw line ln[0], ln[1], ln[2], ln[3]
endfunc

function Object(x, y, r)
    return [x: x, y: y, r: r, rsqr: r*r,
            Draw: function(); draw ellipse .x, .y, .r, .r, false; endfunc]
endfunc

function PushOut(obj, lines)
    ' Let all lines push the object around a maximum of 10 times. This is not a recommended
    ' approach, just for testing.
    tests = 10
    for i = 1 to tests
        col = false
        foreach ln in lines
            dp = max(0, min(ln[6], (obj.x - ln[0])*ln[4] + (obj.y - ln[1])*ln[5]))
            px = ln[0] + dp*ln[4]; py = ln[1] + dp*ln[5]
            dx = obj.x - px; dy = obj.y - py
            d = dx*dx + dy*dy
            if d < obj.rsqr
                k = 1/sqr(d)
                obj.x = px + dx*k*obj.r
                obj.y = py + dy*k*obj.r
                col = true
            endif
        next
        if not col break
    next
endfunc

This is pretty much how I did it in the GLOOM library for n6 too, if my memory is correct (source code since long gone).

Your wish is my command....


.zip   gloom.zip (Size: 27.34 KB / Downloads: 12)

I hope this helps...

J
Logic is the beginning of wisdom.
Reply


Messages In This Thread
Circle-line collision - by Marcus - 04-16-2024, 03:59 PM
RE: Circle-line collision - by kevin - 04-16-2024, 05:30 PM
RE: Circle-line collision - by Marcus - 04-16-2024, 06:40 PM
RE: Circle-line collision - by johnno56 - 04-16-2024, 07:09 PM
RE: Circle-line collision - by 1micha.elok - 04-17-2024, 04:17 AM
RE: Circle-line collision - by johnno56 - 04-17-2024, 07:01 AM
RE: Circle-line collision - by Marcus - 04-17-2024, 09:24 AM
RE: Circle-line collision - by johnno56 - 04-17-2024, 09:37 AM
RE: Circle-line collision - by Marcus - 04-18-2024, 04:03 PM
RE: Circle-line collision - by 1micha.elok - 04-19-2024, 02:12 AM

Forum Jump:


Users browsing this thread: 4 Guest(s)