Thanks GrahamK I will give it a look through, Well here is the current state of the collisions there doesn't seem to be any bugs in here still keepin the balls speed under 1 radius per frame but to tell you the truth I dont think it needs to be any faster than this. At full speed the pack breaks preatty well and scatters them all around the table.
controls
left/right = aim
down = increase power
up = reduce power
X = shoot
open window 640, 512
window origin "cc"
holes = 5
dim hx(holes), hy(holes)
data -280,-140, 000,-140, 280,-140
data -280, 140, 000, 140, 280, 140
for a = 0 to holes read hx(a), hy(a) next
colrs = 3
dim red(colrs), grn(colrs), blu(colrs)
data 255,255,255, 255,255,000, 255,000,000, 100,100,100
for a = 0 to colrs read red(a), grn(a), blu(a) next
fric = 0.998
damp = 0.991
bump = 0.950
radi = 12
hradi = radi*1.5
sum_radi = (radi+radi)^2
sum_hradi = hradi^2
minx = -270
miny = -130
maxx = 270
maxy = 130
label setup
balls = 15
redim posx(balls), posy(balls)
redim dirx(balls), diry(balls)
redim colr(balls)
restore setup_balls
for a = 0 to balls
colr(a) = mod(a,2)+1
dirx(a) = 0
diry(a) = 0
read posx(a), posy(a)
next
dirx(0) = ran(10)
colr(0) = 0
colr(balls) = 3
motionless = balls+1
label main
setdispbuf draw
draw = 1 - draw
setdrawbuf draw
clear window
setrgb 1, 000, 100, 000
fill box minx-radi, miny-radi to maxx+radi, maxy+radi
setrgb 1, 255,255,255
for a = 0 to holes circle hx(a), hy(a), hradi next
for a = 0 to balls
for b = 1.0 to 0.1 step -0.1
setrgb 1, red(colr(a))*(1-b), grn(colr(a))*(1-b), blu(colr(a))*(1-b)
fill circle posx(a), posy(a), radi*b
next
next
if (motionless > balls) then
ctrl(peek("port1"))
else
force = 0
angle = 0
motionless = 0
collisions()
fi
goto main
sub ctrl(c)
if (and(c, 32) <> 0) then
angle = angle + pi / 4096
elsif (and(c,128) <> 0) then
angle = angle - pi / 4096
fi
if (and(c, 16) <> 0) then
force = force - 0.01
if (force < 0) force = 0
elsif (and(c, 64) <> 0) then
force = force + 0.01
if (force > radi) force = radi
fi
if (and(c,16384) <> 0) then
dirx(0) = cos(angle)*force
diry(0) = sin(angle)*force
motionless = 1
else
x = posx(0)
y = posy(0)
cs = cos(angle)*force
sn = sin(angle)*force
ct = cos(angle+pi/2)
tn = sin(angle+pi/2)
x1 = x - cs*10
y1 = y - sn*10
x2 = x1 - cos(angle)*200
y2 = y1 - sin(angle)*200
setrgb 1, 255, 255, 255
line x,y to x+cs*30,y+sn*30
setrgb 1, 255, 220, 180
fill triangle x1+ct*2,y1+tn*2 to x2+ct*5,y2+tn*5 to x2-ct*5,y2-tn*5
fill triangle x1+ct*2,y1+tn*2 to x1-ct*2,y1-tn*2 to x2-ct*5,y2-tn*5
fi
end sub
sub collisions()
for a = 0 to balls
if (dirx(a)^2 + diry(a)^2 > 0.0001) then
dxa = dirx(a)*fric
dya = diry(a)*fric
pxa = posx(a)
pya = posy(a)
for b = 0 to balls
if (a <> b) then
adj = posx(b) - pxa
opp = posy(b) - pya
dist = adj^2 + opp^2
if (dist < sum_radi) then
dist = sqrt(dist)
nx = adj / dist
ny = opp / dist
cx = (pxa+posx(b))/2
cy = (pya+posy(b))/2
pxa = cx - nx*radi
pya = cy - ny*radi
posx(b) = cx + nx*radi
posy(b) = cy + ny*radi
product = ((dirx(b)-dxa)*nx + (diry(b)-dya)*ny) * damp
sx = nx * product
sy = ny * product
dxa = dxa + sx
dya = dya + sy
dirx(b) = dirx(b) - sx
diry(b) = diry(b) - sy
fi
fi
next
pxa = pxa + dxa
pya = pya + dya
if (pxa < minx) then
pxa = minx
dxa = -dxa*bump
dya = dya*bump
elsif (pxa > maxx) then
pxa = maxx
dxa = -dxa*bump
dya = dya*bump
fi
if (pya < miny) then
pya = miny
dxa = dxa*bump
dya = -dya*bump
elsif (pya > maxy) then
pya = maxy
dxa = dxa*bump
dya = -dya*bump
fi
posx(a) = pxa
posy(a) = pya
dirx(a) = dxa
diry(a) = dya
for b = 0 to holes
adj = pxa - hx(b)
opp = pya - hy(b)
if (adj^2 + opp^2 < sum_hradi) then
col = colr(a)
for c = a to balls-1
posx(c) = posx(c+1)
posy(c) = posy(c+1)
dirx(c) = dirx(c+1)
diry(c) = diry(c+1)
colr(c) = colr(c+1)
next
posx(balls) = pxa
posy(balls) = pya
dirx(balls) = dxa
diry(balls) = dya
colr(balls) = col
balls = balls - 1
fi
next
else motionless = motionless + 1
fi
next
end sub
label setup_balls
data -140,000
data 100,000, 120,010, 140,-20, 120,-10, 140,020
data 160,-30, 160,010, 160,030, 180,000, 160,-10
data 180,-40, 180,-20, 180,040, 180,020, 140,000