diff --git a/collisiontest.p8 b/collisiontest.p8 new file mode 100644 index 0000000..f85eb69 --- /dev/null +++ b/collisiontest.p8 @@ -0,0 +1,134 @@ +pico-8 cartridge // http://www.pico-8.com +version 42 +__lua__ +bx0=0 +by0=0 +bx1=127 +by1=127 +lx0=0 +ly0=0 +lx1=127 +ly1=127 + +-->8 + +function collides() + local tmin=0 + local tmax=1 + + local ldx=lx1-lx0 + local ldy=ly1-ly0 + + + -- x + if ldx==0 then + local lx=lx0 -- which ==lx1 + if (lx=bx1) return false + else + local tx0=(bx0-lx0)/ldx + local tx1=(bx1-lx0)/ldx + + tmin=max(tmin,min(tx0,tx1)) + tmax=min(tmax,max(tx0,tx1)) + end + + if ldy==0 then + local ly=ly0 -- which ==ly1 + if (ly=by1) return false + else + local ty0=(by0-ly0)/ldy + local ty1=(by1-ly0)/ldy + + tmin=max(tmin,min(ty0,ty1)) + tmax=min(tmax,max(ty0,ty1)) + end + + return tmax>=tmin +end + + +-->8 +function _init() + poke(0x5f2d,1) -- enable mouse +end + +function _bounce_screen(x) + return _bounce(x*128,128) +end +function _bounce(x,mx) + x=x%(mx * 2) + if (x>=mx)return mx-(x-mx) + return x +end + +function _to_halfopen(x0,x1) + -- turn two numbers into a + -- half-open integer range + x0=flr(x0) + x1=flr(x1) + local lo=min(x0,x1) + local hi=max(x0,x1) + if (hi==lo) return lo,hi + return lo,hi-1 +end + +function _update60() + local t=time()/4 + + local bx0_=_bounce_screen(t*1) + local by0_=_bounce_screen(t*2) + local bx1_=_bounce_screen(t*3) + local by1_=_bounce_screen(t*4) + + bx0,bx1=_to_halfopen(bx0_,bx1_) + by0,by1=_to_halfopen(by0_,by1_) + + update_line() + + --[[ + local lx0_=_bounce_screen(t*1.5) + local ly0_=_bounce_screen(t*2.5) + local lx1_=_bounce_screen(t*3.5) + local ly1_=_bounce_screen(t*4.5) + + lx0,lx1=lx0_,lx1_ + ly0,ly1=ly0_,ly1_ + ]]-- +end + +was_down=false +last_mx,last_my=nil,nil + +function update_line() + local mx,my=stat(32),stat(33) + local is_down=stat(34)!=0 + + if is_down then + if was_down then + lx1,ly1=mx,my + else + lx0,ly0=mx,my + end + end + was_down=is_down + last_mx,last_my=mx,my +end +-->8 +function _draw() + cls(0) + rect(bx0,by0,bx1,by1,6) + local lc=2 + if collides() then + lc=8 + end + line(lx0,ly0,lx1,ly1,lc) + + pset(last_mx,last_my,7) +end +__gfx__ +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 +00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000