# This is based off the gravity demo, # # # This demo also shows an example of some of the physical simulation # features of breve. It features a ball rolling down a set of stairs # over and over, changing the starting conditions each time. # @use Mobile. @use PhysicalControl. @use Stationary. @use Real. Controller Gravity. PhysicalControl : Gravity { + variables: theBall (object). + to init: # This demo is actually so fast that we're going to intentionally # slow it down by setting the integration step to be very very small. # This will also increase the accuracy of the simulation. self set-integration-step to .0001. # creating the block that is to be broken up new Step create at (0.5, 0, 0) size (5.0, 0.01, 5). 1 new Balls. self point-camera at (1.0, -.8, -.6) from (3.5, 1.1, 5.0). self enable-shadow-volumes. self add-menu named "Reset Ball" for-method "reset-ball". + to reset-ball: theBall reset. } Stationary : Step { + variables: loc(vector). sizeVec(vector). st(string). stepShape (object). + to create at location (vector) size sizeVector (vector): stepShape = (new Cube init-with size sizeVector). self handle-collisions with-type "Ball" with-method "split". self set-shape to stepShape. self move to location. loc = location. sizeVec = sizeVector. + to split: # I know having all these variables is really messy sorry... xV(float). yV(float). zV(float). xVa(float). yVa(float). zVa(float). xVal(float). yVal(float). zVal(float). xValn(float). yValn(float). zValn(float). xVal = loc{0}. yVal = loc{1}. zVal = loc{2}. xV = sizeVec{0}/4. yV = sizeVec{1} . zV = sizeVec{2}/4. xVa = sizeVec{0}/2. yVa = sizeVec{1} . zVa = sizeVec{2}/2. xValn = xVal - xV - 0.01 . zValn = zVal - zV - 0.01 . xVal =xVal + xV +0.01. zVal =zVal + zV +0.01. if xV > 0.01: # prevents infinately small pieces. { new Step create at (xVal,yVal,zVal) size (xVa,yVa,zVa). new Step create at (xValn,yVal,zVal) size (xVa,yVa,zVa). new Step create at (xVal,yVal,zValn) size (xVa,yVa,zVa). new Step create at (xValn,yVal,zValn) size (xVa,yVa,zVa). } free self. # delete old block } Mobile : Ball (aka Balls) { + to iterate: # The iterate method is called each timestep... # if the Y component (height) of our position is less than -20, # then the ball has passed the steps and is continuing to fall, # so we'll reset the ball with new starting conditions. if (self get-location)::y < -2.0: self reset. + to init: self set-shape to (new Sphere init-with radius .05 + random[ .15 ] ). self enable-physics. self reset. + to reset: # vary initial conditions slightly each time self set-color to random[(1, 1, 1)]. self move to ( -.9, .5, -1 ) + random[ ( 1.5, .8, 2 ) ]. self set-velocity to (random[2.0], random[2.0], 0). }