1. Enclosure file: EINAR.EX

--  einar.ex: collision detection along a straight line
--  jbabor at paradise.net.nz
--  98-08-25

include graphics.e
include image.e
include get.e

object junk
sequence LEVEL

function nowall(integer x,integer y,integer x2,integer y2)
    integer d,dx,dy,ai,bi,xi,yi,temp
    x=floor(x/20)
    x2=floor(x2/20)
    y=floor(y/20)
    y2=floor(y2/20)
    if y=y2 then        -- horizontal line
        if x>x2 then
            temp=x
            x=x2
            x2=temp
        end if
        for i=x to x2 do
            if LEVEL[y+1][i+1] then return 0 end if
        end for
    else
        if x<x2 then
            xi=1
            dx=x2-x
        else
            xi=-1
            dx=x-x2
        end if
        if y<y2 then
            yi=1
            dy=y2-y
        else
            yi=-1
            dy=y-y2
        end if
        if LEVEL[y+1][x+1] then return 0 end if
        if dx>dy then
            ai=2*(dy-dx)
            bi=2*dy
            d=bi-dx
            while x!=x2 do
                if d>=0 then
                    y=y+yi
                    d=d+ai
                else
                    d=d+bi
                end if
                x=x+xi
                if LEVEL[y+1][x+1] then return 0 end if
            end while
        else
            ai=2*(dx-dy)
            bi=2*dx
            d=bi-dy
            while y!=y2 do
                if d>=0 then
                    x=x+xi
                    d=d+ai
                else
                    d=d+bi
                end if
                y=y+yi
                if LEVEL[y+1][x+1] then return 0 end if
            end while
        end if
    end if
    return 1
end function

-- test ------------------------------------------------------------------------

LEVEL=repeat(repeat(0,16),10)   -- 1:20 scale model of screen
for i=1 to 16 do LEVEL[6][i]=1 end for
for i=1 to 10 do LEVEL[i][9]=1 end for

junk=graphics_mode(19)
polygon(8,1,{{0,0},{319,0},{319,199},{0,199}})
display_image({0,0},LEVEL)
position(3,1)

draw_line(12,{{50,50},{100,130}})
text_color(12)
? nowall(50,50,100,130)

draw_line(13,{{50,150},{159,130}})
text_color(13)
? nowall(50,150,159,130)

draw_line(14,{{60,50},{200,50}})
text_color(14)
? nowall(60,50,200,50)

draw_line(15,{{190,190},{300,140}})
text_color(15)
? nowall(190,190,300,140)


junk=wait_key()
junk=graphics_mode(-1)

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu