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 collisions

Author
Message
Damondu83
8
Years of Service
User Offline
Joined: 6th Apr 2016
Location:
Posted: 6th Apr 2016 18:47
i am trying to make a particle system where the particles collid with each other in a convincing fashion but cant seem to get the particles to collid in the right matter, here is my code can anyone tell me what i am doing wrong? i believe it is because the arrays check within the arrays all the positions of the particles then update a collision so the system responds by making particles inside each other without having a chance to update the collision..

ps moderators, i tried editing the other post and ended up losing it sorry for reposting again

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

//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

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


//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#(y) <> ParX#(i) and ParY#(y) <> ParY#(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

//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
Damondu83
8
Years of Service
User Offline
Joined: 6th Apr 2016
Location:
Posted: 8th Apr 2016 18:59
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


Login to post a reply

Server time is: 2024-04-20 05:42:12
Your offset time is: 2024-04-20 05:42:12