hello, i got the collisions to work but i am having minor problems such as certain balls getting stuck somtimes, i have loops to prevent this but they dont work efficiently.
Any propositions?
ive cleaned up the code and made it more visible.
sync on
sync rate 60
particles=15
//declaring variables postion and speed x and y of particles
dim ptcls(particles)
dim xpos#(particles)
dim ypos#(particles)
dim xspeed#(particles)
dim yspeed#(particles)
dim radius#(particles)
dim speed#(particles)
do
//get mouse position
mx=mousex()+rnd(250)
my=mousey()+ rnd(250)
// properties
if mouseclick()=1
for t=1 to 1
p=p+1
if p > particles then p=0
xspeed#(p) = ((rnd(100)-50)/20)/2
yspeed#(p) = ((rnd(100)-50)/10)/2
radius#(p) = rnd(10)+3
xpos#(p) = rnd(640)
ypos#(p) = rnd(420)
next t
endif
// get particle number
for n=1 to particles
ptcls(n)=n
next n
for i=1 to particles
//
//draws circle
drawcircle(xpos#(i) + xspeed#(i),ypos#(i) + yspeed#(i),radius#(i))
//updates positions
xpos#(i)=xpos#(i)+xspeed#(i)
ypos#(i)=ypos#(i)+yspeed#(i)
//gravity effect on speed
//xspeed#(i)=xspeed#(i)/1.002
//yspeed#(i)=yspeed#(i)/1.002
//creates mirrored box
if xpos#(i) > 640 then xpos#(i)=1
if ypos#(i) > 480 then ypos#(i)=1
if xpos#(i) < 1 then xpos#(i)=640
if ypos#(i) < 1 then ypos#(i)=480
for y =1 to i
//keeps balls moving
speed1=sqrt(xspeed#(i)^2+yspeed#(i)^2)
speed2=sqrt(xspeed#(y)^2+yspeed#(y)^2)
if speed1=0
xspeed#(i)= xspeed#(i) + (rnd(3)-2)/2
yspeed#(i)= yspeed#(i) + (rnd(3)-2)/2
endif
if speed2=0
xspeed#(y)= xspeed#(y) + (rnd(3)-2)/2
yspeed#(y)= yspeed#(y) + (rnd(3)-2)/2
endif
//calculates if circles are at touching distance
dis= sqrt(((xpos#(i)-xpos#(y))^2) +((ypos#(i)-ypos#(y))^2))
//draws line between colliding circles
if dis <= (radius#(i)+radius#(y)) and xpos#(i)<>xpos#(y) and ypos#(i)<> ypos#(y)
line xpos#(i),ypos#(i),xpos#(y),ypos#(y)
// indicates point of collision with a tiny circle
collisionx = ((xpos#(i) * radius#(y)) + (xpos#(y) * radius#(i))) / (radius#(i) + radius#(y))
collisiony = ((ypos#(i) * radius#(y)) + (ypos#(y) * radius#(i))) / (radius#(i) + radius#(y))
circle collisionx, collisiony,2
// seperates balls if they stay stuck
if dis <= (radius#(i)+radius#(y)) and speed1= speed2 or (speed1=0 and speed2 =0 and dis <= (radius#(i)+radius#(y)))
if radius#(i)<=radius#(y)
xspeed#(i)= (rnd(10)-5)
yspeed#(i)= (rnd(10)-5)
endif
if radius#(i) >= radius#(y)
xspeed#(y)= (rnd(10)-5)
yspeed#(y)= (rnd(10)-5)
endif
endif
endif
// keeps ball speed below 100
if sqrt(xspeed#(i)^2+yspeed#(i)^2)=>3
xspeed#(i)= (rnd(3)-2)
yspeed#(i)= (rnd(3)-2)
endif
remstart
if sqrt(xspeed#(y)^2+yspeed#(y)^2)=>3
xspeed#(y)= xspeed#(y) - (rnd(3)-2)
yspeed#(y)= yspeed#(y) - (rnd(3)-2)
endif
remend
//collision response
// new velocities etc...
if dis <= (radius#(i)+radius#(y)) and xpos#(i)+ypos#(i)+radius#(i) <>xpos#(y)+ypos#(y)+radius#(y)
//and ypos#(i)<> ypos#(y)
cc= radius#(i)+radius#(y)
newVelX1 = (xspeed#(i) * (radius#(i) - radius#(y)) + (2 * radius#(y) * xspeed#(y))) / (cc)
newVelY1 = (yspeed#(i) * (radius#(i) - radius#(y)) + (2 * radius#(y) * yspeed#(y))) / (cc)
newVelX2 = (xspeed#(y) * (radius#(y) - radius#(i)) + (2 * radius#(i) * xspeed#(i))) / (cc)
newVelY2 = (yspeed#(y) * (radius#(y) - radius#(i)) + (2 * radius#(i) * yspeed#(i))) / (cc)
xspeed#(i) = newVelX1;
xspeed#(y) = newVelX2;
yspeed#(i) = newVelY1;
yspeed#(y) = newVelY2
xpos#(i) = xpos#(i) + newVelX1
ypos#(i) = ypos#(i) + newVelY1
xpos#(y) = xpos#(y) + newVelX2
ypos#(y) = ypos#(y) + newVelY2
endif
next y
//displays text
xp1=xpos#(i)
yp1=ypos#(i)
rad=radius#(i)
tspeed=sqrt( xspeed#(i)^2 + yspeed#(i)^2)
round( xp1,3)
round( yp1,3)
round( rad,3)
text xpos#(i)+5,ypos#(i)+10,str$(xp1)
text xpos#(i)+35,ypos#(i)+10,str$(yp1)
text xpos#(i)+5,ypos#(i)+25,str$(rad)
text xpos#(i)+25,ypos#(i)+25,str$(tspeed)
text xpos#(i)-15,ypos#(i)+10,str$(ptcls(i))
next i
remstart
for s=1 to particles
s=s+1
ts=ts+25
text 0+ts,0,str$(xspeed#(s))
text 25+ts,0,str$(yspeed#(s))
next s
remend
sync
cls
loop
end
//drawcircle function
function drawcircle(x,y,r)
circle x,y,r
endfunction
//round function
function round(n#,r)
n#= n#/r
n= int(n#+n#)-int(n#)
n= n*r
endfunction n