# Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

### DarkBASIC Professional Discussion / Circle Collision

Message
Posted: 6th Apr 2016 14:58 Edited at: 6th Apr 2016 18:23
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 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

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

//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)
line ParX#(i),ParY#(i),ParX#(y),ParY#(y)
//circle collisionPointX,collisionPointY,2

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

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)
round( xpos1,3)
round( ypos1,3)
text ParX#(i)+5,ParY#(i)+10,str\$(xpos1)
text ParX#(i)+35,ParY#(i)+10,str\$(ypos1)
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
Posted: 6th Apr 2016 18:24
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 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

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

//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)
line ParX#(i),ParY#(i),ParX#(y),ParY#(y)
//circle collisionPointX,collisionPointY,2

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

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)
round( xpos1,3)
round( ypos1,3)
text ParX#(i)+5,ParY#(i)+10,str\$(xpos1)
text ParX#(i)+35,ParY#(i)+10,str\$(ypos1)
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
Posted: 6th Apr 2016 18:59
Since you seem to be a new user you will need to wait for your posts to be moderated before they will be seen, so just wait a bit longer before posting again.