I wanted to figure out how to get box collision and found out it was really simple. So I threw this together, it took 2 days cause of trying to make out how to do it with many boxes. Well anyways sorry for the bad remarking:
19 LINER:
Rem Project: Boxes
Rem Created: Monday, December 06, 2010
set window off:set display mode 800,600,32:sync on:sync rate 0:hide mouse
boxes=100:maxwidth=20:maxheight=20
dim x(boxes):dim y(boxes):dim width(boxes):dim height(boxes):dim direct(boxes):dim wrongdir(boxes)
x(1)=mousex():y(1)=mousey():width(1)=44:height(1)=44
for n=2 to boxes:x(n)=rnd(800)-(maxwidth/2):y(n)=rnd(600)-(maxheight/2):width(n)=rnd(maxwidth-10)+10:height(n)=rnd(maxheight-10)+10:for n2=1 to boxes:if n<>n2:collision=0:if x(n)<=x(n2) and x(n)+width(n)-1>=x(n2):if y(n)<=y(n2) and y(n)+height(n)-1>=y(n2):collision=1:else:if y(n)>=y(n2) and y(n)<=y(n2)+height(n2)-1:collision=1:endif:endif:else:if x(n)>=x(n2) and x(n)<=x(n2)+width(n2)-1:if y(n)<=y(n2) and y(n)+height(n)-1>=y(n2):collision=1:else:if y(n)>=y(n2) and y(n)<=y(n2)+height(n2)-1:collision=1:endif:endif:endif:endif:if collision=1:n=n-1:endif:endif:next n2:next n
DO
time=timer()-oldtime:if time>9:gosub movement:endif:gosub pasteimages:sync:cls
LOOP
movement:
oldtime=timer():for n=1 to boxes:for n2=1 to 4:direct(n)=0:next n2:next n:mx=mousex():my=mousey():mmx=mousemovex():mmy=mousemovey():x(1)=x(1)+mmx:y(1)=y(1)+mmy:if mmx<>0 or mmy<>0:direct(1)=1:endif :flag=0
while flag=0
flag=1
for n=1 to boxes:if direct(n)>0:for n2=2 to boxes:if n<>n2:collision=0:if x(n)<=x(n2) and x(n)+width(n)-1>=x(n2):if y(n)<=y(n2) and y(n)+height(n)-1>=y(n2):collision=1:else:if y(n)>=y(n2) and y(n)<=y(n2)+height(n2)-1:collision=1:endif:endif:else:if x(n)>=x(n2) and x(n)<=x(n2)+width(n2)-1:if y(n)<=y(n2) and y(n)+height(n)-1>=y(n2):collision=1:else:if y(n)>=y(n2) and y(n)<=y(n2)+height(n2)-1:collision=1:endif:endif:endif:endif:if collision=1:if direct(n)=1:x(n2)=x(n2)+mmx:y(n2)=y(n2)+mmy:direct(n2)=1:endif:endif:endif:next n2:direct(n)=0:direct(n)=0:direct(n)=0:direct(n)=0:endif:next n
for n=1 to boxes:if direct(n)=1:flag=0:endif:next n
endwhile
return
pasteimages:
for n=1 to boxes:ink rgb(150,0,0),0:box x(n),y(n),x(n)+width(n),y(n)+height(n):ink rgb(200,0,0),0:box x(n)+2,y(n)+2,x(n)+width(n)-2,y(n)+height(n)-2:next n
return
UNCOMPRESSED:
Rem Project: Boxes
Rem Created: Monday, December 06, 2010
set window off
set display mode 800,600,32
sync on
sync rate 0
hide mouse
boxes=100 : `EDIT!
maxwidth=20 : `EDIT!
maxheight=20 : `EDIT!
dim x(boxes)
dim y(boxes)
dim width(boxes)
dim height(boxes)
dim direct(boxes)
dim wrongdir(boxes)
x(1)=mousex()
y(1)=mousey()
width(1)=44 : `EDIT!
height(1)=44 : `EDIT!
for n=2 to boxes
x(n)=rnd(800)-(maxwidth/2)
y(n)=rnd(600)-(maxheight/2)
width(n)=rnd(maxwidth-10)+10
height(n)=rnd(maxheight-10)+10
for n2=1 to boxes
if n<>n2
collision=0
if x(n)<=x(n2) and x(n)+width(n)-1>=x(n2)
if y(n)<=y(n2) and y(n)+height(n)-1>=y(n2)
collision=1
else
if y(n)>=y(n2) and y(n)<=y(n2)+height(n2)-1
collision=1
endif
endif
else
if x(n)>=x(n2) and x(n)<=x(n2)+width(n2)-1
if y(n)<=y(n2) and y(n)+height(n)-1>=y(n2)
collision=1
else
if y(n)>=y(n2) and y(n)<=y(n2)+height(n2)-1
collision=1
endif
endif
endif
endif
if collision=1
n=n-1
endif
endif
next n2
next n
DO
time=timer()-oldtime
if time>9
gosub movement
endif
gosub pasteimages
sync:cls
LOOP
movement:
oldtime=timer()
for n=1 to boxes
for n2=1 to 4
direct(n)=0
next n2
next n
mx=mousex()
my=mousey()
mmx=mousemovex()
mmy=mousemovey()
x(1)=x(1)+mmx
y(1)=y(1)+mmy
if mmx<>0 or mmy<>0
direct(1)=1
endif
flag=0
while flag=0
flag=1
for n=1 to boxes
if direct(n)>0
for n2=2 to boxes
if n<>n2
collision=0
if x(n)<=x(n2) and x(n)+width(n)-1>=x(n2)
if y(n)<=y(n2) and y(n)+height(n)-1>=y(n2)
collision=1
else
if y(n)>=y(n2) and y(n)<=y(n2)+height(n2)-1
collision=1
endif
endif
else
if x(n)>=x(n2) and x(n)<=x(n2)+width(n2)-1
if y(n)<=y(n2) and y(n)+height(n)-1>=y(n2)
collision=1
else
if y(n)>=y(n2) and y(n)<=y(n2)+height(n2)-1
collision=1
endif
endif
endif
endif
if collision=1
if direct(n)=1
x(n2)=x(n2)+mmx
y(n2)=y(n2)+mmy
direct(n2)=1
endif
endif
endif
next n2
direct(n)=0
direct(n)=0
direct(n)=0
direct(n)=0
endif
next n
for n=1 to boxes
if direct(n)=1 then flag=0
next n
endwhile
return
pasteimages:
for n=1 to boxes
ink rgb(150,0,0),0
box x(n),y(n),x(n)+width(n),y(n)+height(n)
ink rgb(200,0,0),0
box x(n)+2,y(n)+2,x(n)+width(n)-2,y(n)+height(n)-2
next n
return
COLLISION CODE:
if x(1)<=x(2) and x(1)+width(2)-1>=x(2)
if y(1)<=y(2) and y(1)+height(1)-1>=y(2)
collision=1
else
if y(1)>=y(2) and y(1)<=y(2)+height(2)-1
collision=1
endif
endif
else
if x(1)>=x(2) and x(1)<=x(2)+width(2)-1
if y(1)<=y(2) and y(1)+height(n)-1>=y(2)
collision=1
else
if y(1)>=y(2) and y(1)<=y(2)+height(2)-1
collision=1
endif
endif
endif
endif
OFF SUBJECT:
[Working on a game right now that will be a WIP soon(Its nothing great lol)]