REVO2700Two ball collisions@ 5  ULucida Grande ULucida Grande WLucida Grande cREVGeneralbreakpointconditions breakpointsbreakpointstatesstackfileversion2.7 Plocal xMin, yMin, xMax, yMax local x,y,vx,vy local R on SetUp put the width of grc "ball1" / 2 into R put the rect of grc "rectangle" into tRect put item 1 of tRect into xMin put item 2 of tRect into yMin put item 3 of tRect into xMax put item 4 of tRect into yMax put 2 into vx[1] put 3 into vy[1] put 1 into vx[2] put -1 into vy[2] put the loc of grc "ball1" into tLoc1 put item 1 of tLoc1 into x[1] put item 2 of tLoc1 into y[1] put the loc of grc "ball2" into tloc2 put item 1 of tloc2 into x[2] put item 2 of tloc2 into y[2] moveBalls end SetUp on moveBalls repeat with i = 1 to 2 put "ball" & i into tName --Increase the coordinates by the velocity add vx[i] to x[i] add vy[i] to y[i] set the loc of grc tName to x[i],y[i] end repeat --Deal with ball on ball collisions if thereIsAparticleCollision (x[1],y[1], x[2],y[2]) then put newVel(x[1],y[1],x[2],y[2],vx[1],vy[1],vx[2],vy[2]) into theNewVelocities put item 1 of theNewVelocities into vx[1] put item 2 of theNewVelocities into vy[1] put item 3 of theNewVelocities into vx[2] put item 4 of theNewVelocities into vy[2] if s < 2*r then --Just a precaution to keep the balls from getting stuck together. --Normally, this won't run. repeat until theDist(x[1],y[1],x[2],y[2]) > 2*r add vx[2] to x[2] add vy[2] to y[2] end repeat end if end if --Deal with wall collisions. repeat with i = 1 to 2 if x[i] < xMin+R or x[i] > xMax-R then subtract vx[i] from x[i] multiply vx[i] by -1 end if if y[i] < yMin+R or y[i] > yMax-R then subtract vy[i] from y[i] multiply vy[i] by -1 end if end repeat if the shiftKey is down then exit moveBalls if the label of button "StartStop" is "Start" then exit moveBalls send "moveBalls" to me in 1 millisec end moveBalls function thereIsAparticleCollision x1,y1,x2,y2 put sqrt ( (x1-x2)^2 + (y1-y2)^2) into tDist if tDist < 2*R then return true else return false end if end thereIsAparticleCollision function newVel xx,yy,xx',yy',vxx,vyy,vxx',vyy' --This assumes an ideal elastic collision. --where each ball exerts a force on the other ball --along the line joining the centers. --They exchange momentum (velocity component) along this line --The momentum (velocity) component perpendicular to this line --is unchange by the collision. put xx,yy into ptMe put xx',yy' into ptOther put theLineAngle(ptMe,ptOther) into tCentersAngle put tCentersAngle into tCa put xx,yy,xx+vxx,yy+vyy into tVelVector put theLineAngle(tVelVector) into tVa put sqrt(vxx'*vxx'+vyy'*vyy') into v' put sqrt(vxx*vxx+vyy*vyy) into v put xx',yy',xx'+vxx',yy'+vyy' into tVelVector' put theLineAngle(tVelVector') into tVa' --Angle of the vVector relative to the line joining centers put tVa - tCa into tVrA put tVa'- tCa into tVrA' put v*cos(tVrA) into vR' put v*sin(tVrA) into vT put v'*cos(tVrA') into vR put v'*sin(tVrA') into vT' put vR*cos(tCa) - vT*sin(tCa) into vxx put vR*sin(tCa) + vT*cos(tCa) into vyy put vR'*cos(tCa) - vT'*sin(tCa) into vxx' put vR'*sin(tCa) + vT'*cos(tCa) into vyy' return vxx,vyy,vxx',vyy' end newVel function theLineAngle p1,p2 --Angle of line defined by the two points p1 and p2 get the paramCount if it is 1 then put item 3 to 4 of p1 into p2 put item 1 to 2 of p1 into p1 end if put item 1 of p2 - item 1 of p1 into dx put item 2 of p2 - item 2 of p1 into dy put atan2(dy,dx) into tAngle return tAngle end theLineAngle function theDist x1,y1,x2,y2 put sqrt ((x1-x2)^2 +(y1-y2)^2) into temp return temp end theDist  cREVGeneralscripteditorvscroll0scripteditorselection53debugParametersdebugEntryHandler M,mouseUp RectangleK8]@3-@_ _T \'̠  cREVGeneral revUniqueID 1332718542338Ball2KD>q'%Yon mouseDown grab me end mouseDown -@{\'̠ h cREVGeneral revUniqueID 1332718557008scripteditorvscroll0scripteditorselection37ball1KDs'%Yon mouseDown grab me end mouseDown -@gGg\'̠ h cREVGeneral revUniqueID 1332718585298scripteditorvscroll0scripteditorselection37 StartStopepon mouseUp if the label of me is "Start" then set the label of me to "Stop" wait 10 millisec with messages setUp else set the label of me to "Start" end if end mouseUp 4R0Start cREVGeneral revUniqueID 1332736056726scripteditorvscroll0scripteditorselection48 Field)` 4: cREVGeneral revUniqueID 1332773893360scripteditorvscroll0  0Click the start/stop button to start/stop, dah. @Black/ ,In an emergency, hit the shift key to stop. @Black+ The balls are dragable. @Black revOnline | RunRev