Cheap Bonkers.bas


' cheap bonkers.bas SmallBASIC 0.12.8 [B+=MGA] 2017-01-07
' from Galileo's Collection, Bonkers is a funky mystery
' but I think it is about colliding particles.
' In this version use spacebar to toggle between clearing
' screen. The orange balls have recently collided.

n = 100 '<<<<<<<<<<<< feel free to play with this number

clrMode = 1
dim x(n), y(n), dx(n), dy(n), c(n)
for
i = 0 to n
x(i) = xmax/n * i
y(i) = ymax/n * i
dx(i) = (rnd*25 +5) * iff(rnd < .5,-1, 1)
dy(i) = (rnd*25 +5) * iff(rnd < .5,-1, 1)
next

while 1
k = inkey
if asc(k) = 32 then clrMode *= -1
if clrMode < 0 then cls
for i = 0 to n
x(i) += dx(i)
if x(i) > xmax then dx(i) *= -1 : x(i) = xmax
if x(i) < 0 then dx(i) *= -1 : x(i) = 0
y(i) += dy(i)
if y(i) > ymax then dy(i) *= -1 : y(i) = ymax
if y(i) < 0 then dy(i) *= -1 : y(i) = 0
if i < n then
for j = i + 1 to n
if sqr((x(i)-x(j))^2 + (y(i)-y(j))^2) < 50 then
dx(i) *= -1 ': dy(i) *= -1
dy(j) *= -1 ': dx(j) *= -1
c(i) = 1 : c(j) = 1
fi
next
fi
for r = 25 to 1 step -1
if c(i) then
color rgb(255-5*r,64-2*r,0)
else
color rgb(255-10*r,255-10*r,255-10*r)
fi
circle x(i), y(i), r filled
next
c(i) = 0
next
showpage
delay 10
wend

One of these days, I would like to work out the proper math for a deflection from a collision.

I remember running into the problem of collision deflection discussing an air hockey game sim, striking a disk with a round paddle, right here at this forum when I first started attending it.

The real bonkers example for 3 balls seems to have it worked out in vectors, hard to follow...

This cheap version just totally faked deflection paths and covered it up with too many balls to follow! ;-))

I had checked out AI books from library, in a word it seems to be all about states.

As for the course suggested, never had much luck with Python. I am like Sheldon, I need closure, everything is off just a bit without a WEND... an official end to things... fi ;-))

A collision study from Yabasic converted to SmallBASIC:
(note: it had a 640 x 512 screen)

'collision study.bas for SmallBASIC [B+=MGA] 2017-01-09
'study this, yabasic code that seems to handle collisions well

REM ==========================================================
REM Adaptation from PS2 version of Yabasic for Galileo 2014/09
REM ==========================================================


ra=30
ball=6
'dim dirs(ball),xs(ball),ys(ball),ssp(ball)
dim xs(ball), ys(ball), dirs(ball)
dim hitang(ball),hit(ball),reang(ball)

for
a=1 to ball
dirs(a)=0
read xs(a),ys(a)
'ssp(a)=0
next a

dirm=30
xm=500
ym=100
msp=5

while 1
delay 20
cls

for a=1 to ball
'xs(a)=xs(a)+cos(rad(dirs(a)))*ssp(a)
'ys(a)=ys(a)+sin(rad(dirs(a)))*ssp(a)
circle xs(a),ys(a),ra

hitang(a)=deg(atan2(xs(a)-xm, ym-ys(a)))+180
hit(a)=sqr((xs(a)-xm)^2+(ys(a)-ym)^2)
reang(a)=((dirs(a)+180)-dirm)*2+dirm-180
if hit(a) < ra*2 then dirm=reang(a)
dirs(a)=hitang(a)-90
next a

xm=xm+cos(rad(dirm))*msp
ym=ym+sin(rad(dirm))*msp
circle xm,ym,ra
if xm < 0+ra then dirm=270-dirm+270
if xm > 640-ra then dirm=90-dirm+90
if ym < 0+ra then dirm=-dirm
if ym > 510-ra then dirm=180-dirm+180
if dirm > 360 then dirm=dirm-360
if dirm < 0 then dirm=dirm+360
wend

data 252,126,396,126,468,252,396,378,252,378,180,252



'collision mouse study.bas 2017-01-10
'now modify the yab version from six posts to one mouse

ra = 30
ba = pi/6 'ball angle in radians
bx = xmax/2 'ball x
by = ymax/2 'ball y
bs = 20 'ball speed
pen on
while 1
cls
mx = pen(4) : my = pen(5) 'mouse x, y

'update paddle px, py unless paddle is right over ball
if sqr((mx-bx)^2+(my-by)^2) > 2*ra then px = mx : py = my
circle px, py, ra, 1, 12 filled

'update ball
if sqr((px-bx)^2+(py-by)^2) < ra*2 then ba = atan2(by-py, bx-px)
bx = bx+cos(ba)*bs
by = by+sin(ba)*bs
circle bx,by,ra,1,15 filled

'handle border and ball angle
if bx < 0+ra then ba = pi-ba
if bx > xmax - ra then ba = pi-ba
if by < 0+ra then ba = -ba
if by > ymax -ra then ba = -ba
if ba > 2*pi then ba = ba-2*pi
if ba < 0 then ba = ba+2*pi
showpage
delay 20
wend


Now the player has more control of where to hit the ball! This affects paddleball and Breakout games too.

Wait, I didn't do the Breakout translation to SmallBASIC yet?


' improved bonkers.bas SmallBASIC 0.12.8 [B+=MGA] 2017-01-10
' improve collision handling

n = 50 '<<<<<<<<<<<< feel free to play with this number
speed = 20
clrMode = 1
dim x(n), y(n), a(n), c(n)
for
i = 1 to n
x(i) = rnd*xmax
y(i) = rnd*ymax
a(i) = rnd*2*pi
next
while 1
k = inkey
if asc(k) = 32 then clrMode *= -1
if clrMode < 0 then cls

for i = 1 to n
for j = 1 to n
if j <> i and c(j) <> 1 then
if sqr((x(i)-x(j))^2 + (y(i)-y(j))^2) < 50 then
a(i) = atan2(y(i)-y(j), x(i)-x(j))
a(j) = atan2(y(j)-y(i), x(j)-x(i))
c(i) = 1 : c(j) = 1
exit for
fi
fi
next
x(i) = x(i)+cos(a(i))*speed
y(i) = y(i)+sin(a(i))*speed

if x(i) < 25 then a(i) = pi-a(i) : x(i) = 25
if x(i) > xmax-25 then a(i) = pi-a(i) : x(i) = xmax - 25
if y(i) < 25 then a(i) = -a(i) : y(i) = 25
if y(i) > ymax-25 then a(i) = -a(i) : y(i) = ymax - 25
if a(i) > 2*pi then a(i) = a(i)-2*pi
if a(i) < 0 then a(i) = a(i)+2*pi

for r = 25 to 1 step -1
if c(i) then
color rgb(255-5*r,64-2*r,0)
else
color rgb(255-10*r,255-10*r,255-10*r)
fi
circle x(i), y(i), r filled
next
c(i) = 0
next
showpage
delay 10
wend


Much better collisions!