I wrote this about a year ago trying to get realistic physics from balls.
To make the balls decelerate quicker then decrease the cof# value.
There is alot of code but its only the last half that handles the physics, hope it helps.
sync rate 40
sync on
autocam off
backdrop on
screenwidth = 320
halfwidth = screenwidth/2
screenheight = 220
halfheight = screenheight/2
maxspeed = 8
cof# =0.5
numballs=19
dim ball#(numballs,9)
x=0
y=1
xv=2
yv=3
mass=4
radius=5
oldx=6
oldy=7
grad = 8
c=9
randomize timer()
for lp = 0 to numballs
ball#(lp,x) = rnd(screenwidth)-halfwidth
ball#(lp,y) = rnd(screenheight)-halfheight
ball#(lp,xv) = rnd(maxspeed)
ball#(lp,yv) = rnd(maxspeed)
ball#(lp,mass) = 1
ball#(lp,radius) = 8
ball#(lp,oldx) = ball#(lp,x)
ball#(lp,oldy) = ball#(lp,y)
make object sphere lp+1, ball#(lp,radius)
color object lp+1, rgb(rnd(255),rnd(255),rnd(255))
next lp
move camera -200
do
color backdrop rgb(0,0,0)
mx = mousemovex()/16
my = 0-mousemovey()/16
for lp=0 to numballs
ball#(lp,oldx) = ball#(lp,x)
ball#(lp,oldy) = ball#(lp,y)
if abs(mx) > abs(ball#(lp,xv)) then ball#(lp,xv) = ball#(lp,xv) + mx
if abs(my) > abs(ball#(lp,yv)) then ball#(lp,yv) = ball#(lp,yv) + my
ball#(lp,x) = ball#(lp,x) + ball#(lp,xv)
ball#(lp,y) = ball#(lp,y) + ball#(lp,yv)
if ball#(lp,x) >= halfwidth then ball#(lp,xv) = 0-ball#(lp,xv): ball#(lp,x) = halfwidth
if ball#(lp,x) <= 0-halfwidth then ball#(lp,xv) = 0-ball#(lp,xv): ball#(lp,x) = 0-halfwidth
if ball#(lp,y) >= halfheight then ball#(lp,yv) = 0-ball#(lp,yv): ball#(lp,y) = halfheight
if ball#(lp,y) <= 0-halfheight then ball#(lp,yv) = 0-ball#(lp,yv): ball#(lp,y) = 0-halfheight
position object lp+1, ball#(lp,x), ball#(lp,y), 0
next lp
for ball_a = 0 to numballs
for ball_b = 0 to numballs
if ball_a <> ball_b
gosub collision
endif
next ball_b
next ball_a
sync
loop
end
collision:
nabx# = (ball#(ball_b,x) - ball#(ball_a,x))
naby# = (ball#(ball_b,y) - ball#(ball_a,y))
length# = sqrt(nabx#*nabx# + naby#*naby#)
if length# <= (ball#(ball_a,radius)/2 + ball#(ball_b, radius)/2)
if length#=0 then length# = sqrt(1)
rem find the collision point on the 2 vectors doesnot consider velocity
naby#=naby#/length#
nabx#=nabx#/length#
rem get perp. vector
rem does reversing taby# as well produce the opposite vector?
rem it sure does provided the x's and y's match unlike perp.
tabx# = 0-naby#
taby# = nabx#
rem compute initial velocities
vain# = ball#(ball_a,xv)*nabx# + ball#(ball_a,yv)*naby#
vbin# = ball#(ball_b,xv)*nabx# + ball#(ball_b,yv)*naby#
rem compute velocities after collision
ma# = ball#(ball_a,mass)
mb# = ball#(ball_b,mass)
vafn# = (mb#*vbin#*(cof#+1) + vain*(ma# - cof# * mb#)) / (ma# + mb#)
vbfn# = (ma#*vain#*(cof#+1) - vbin*(ma# - cof# * mb#)) / (ma# + mb#)
vaft# =ball#(ball_a,xv)*tabx# + ball#(ball_a,yv)*taby#
vbft# =ball#(ball_b,xv)*tabx# + ball#(ball_b,yv)*taby#
rem translate final velocity into coord system
rem sotre final spped
ball#(ball_a,xv) =vafn#*nabx# + vaft#*tabx#
ball#(ball_a,yv) =vafn#*naby# + vaft#*taby#
ball#(ball_b,xv) =vbfn#*nabx# + vbft#*tabx#
ball#(ball_b,yv) =vbfn#*naby# + vbft#*taby#
rem update coords
ball#(ball_a,x) = ball#(ball_a,x) + ball#(ball_a,xv)
ball#(ball_a,y) = ball#(ball_a,y) + ball#(ball_a,yv)
ball#(ball_b,x) = ball#(ball_b,x) + ball#(ball_b,xv)
ball#(ball_b,y) = ball#(ball_b,y) + ball#(ball_b,yv)
endif
return