Hi first post here, iv been taking code for here and there and ive been peicing together this program that creates particles taht collid with each other. Only problem is, im having trouble with the collision algorythims and having the particles bounc back the way there suppose to, they seem to be colliding and passing through each other, can i have any help please thank you very much
here is the code
sync on
sync rate 60
Particles = 50 `The amount of particles that can be on the screen at a time
Rem Create The Particle Arrays
dim ParX#(Particles) : dim ParY#(Particles) `The XY Position of the Particles
dim ParXS#(Particles) : dim ParYS#(Particles) `The XY Speed of the Particles
dim Radius#(Particles)
dim Mass#(Particles)
//dim distance(particles)
Rem The Main Loop
do
Rem Variables for the xy mouse positions and the xy mouse speed
MX# = MouseX()
MY# = MouseY()
//MMX# = MouseMoveX()
//MMY# = MouseMoveY()
Rem Create particles when the user clicks the mouse
if mouseclick() = 1
for i = 1 to 3 `makes three particles at a time
Particle = Particle + 1
if Particle > Particles then Particle = 0
`positions the particles to the position of the mouse
ParX#(Particle) = MX#+(rnd (200))
ParY#(Particle) = MY#+(rnd(200))
ParXS#(Particle) = (rnd(100)-50)/20 `randomizes the x-speed of the particles
ParYS#(Particle) = (rnd(100)-50)/10 `randomizes the y-speed of the particles
Radius#(Particle)=rnd(20)
next i
endif
Rem Draws all of the particles
for i = 1 to Particles
//collision
//initiate array checking same array
for y= 1 to Particles
//Mass#(particle)=Radius#(i)
//calculate positions x,y vs x,y of each array and see if they overlap
//if (ParX#(i) and ParY#(i)) = (ParX#(y) and ParY#(y)) then line ParX#(i),ParY#(i),ParX#(y),ParY#(y)
if ParX#(y) <> ParX#(i)
if ParX#(i) + Radius#(i) + Radius#(y)> ParX#(y) and ParX#(i)< ParX#(y) + Radius#(i)+ Radius#(y) and ParY#(i) + Radius#(i) + Radius#(y) > ParY#(y) and ParY#(i)< ParY#(y) + Radius#(i) + Radius#(y)
line ParX#(i),ParY#(i),ParX#(y),ParY#(y)
newVelX1 = (ParXS#(i) * (Radius#(i) - Radius#(y)) + (2 * Radius#(y) * ParXS#(y))) / (Radius#(i) + Radius#(y))
newVelY1 = (ParYS#(i) * (Radius#(i) - Radius#(y)) + (2 * Radius#(y) * ParYS#(y))) / (Radius#(i) + Radius#(y))
newVelX2 = (ParXS#(y) * (Radius#(y) - Radius#(i)) + (2 * Radius#(i) * ParXS#(i))) / (Radius#(i) + Radius#(y))
newVelY2 = (ParYS#(y) * (Radius#(y) - Radius#(i)) + (2 * Radius#(i) * ParYS#(i))) / (Radius#(i) + Radius#(y))
//collisionPointX = ((ParX#(i) * Radius#(y)) + (ParX#(y) * Radius#(i))) / (Radius#(i) + Radius#(y))
//collisionPointY = ((ParY#(i) * Radius#(y)) + (ParY#(y) * Radius#(i))) / (Radius#(i) + Radius#(y))
//circle collisionPointX,collisionPointY,2
//nvx1= (speedx1+ radiusx)-
ParX#(i) = ParX#(i) + newVelX1 : ParY#(i) = ParY#(i) + newVelY1;
ParX#(y) = ParX#(y) + newVelX2 : ParY#(y) = ParY#(y)+ newVelY2
endif
else
ParX#(i) = ParX#(i) : ParY#(i) = ParY#(i)
ParX#(y) = ParX#(y) : ParY#(y) = ParY#(y)
endif
//distance(i) = sqrt(((ParX#(i) - (ParX#(y))^2) + (ParX#(y) - ParY#(y)^2)))
//if distance(i)<250 then line ParX#(i),ParY#(i),ParX#(y),ParY#(y)
next y
//ink rgb(255,255-ParY#(i)/2,0),0 `Adds color to the particles
circle ParX#(i)+ParXS#(i),ParY#(i)+ParYS#(i),Radius#(i) `draws the particle
ParX#(i) = ParX#(i)+ParXS#(i) `updates the particles x position
ParY#(i) = ParY#(i)+ParYS#(i) `updates the particles y position
//creates mirrored box
if ParX#(i) => 640 then ParX#(i)=1
if ParY#(i) => 480 then ParY#(i)=1
if ParX#(i) <= 0 then ParX#(i)=640
if ParY#(i) <= 0 then ParY#(i)=480
//reduce particle speed
//ParXS#(i)=ParXS#(i)/1.005
//ParYS#(i)=ParYS#(i)/1.005
xpos1=ParX#(i)
ypos1=ParY#(i)
rad=Radius#(i)
round( xpos1,3)
round( ypos1,3)
round( rad,3)
text ParX#(i)+5,ParY#(i)+10,str$(xpos1)
text ParX#(i)+35,ParY#(i)+10,str$(ypos1)
text ParX#(i)+5,ParY#(i)+25,str$(rad)
next i
if inkey$() <> "" then exit `exits the program when the user hits a key
sync
cls `clears the screen
loop
end
function round(n#,r)
n#= n#/r
n= int(n#+n#)-int(n#)
n= n*r
endfunction n