TGC Codebase Backup



Elastic Simulation by Hamish McHaggis

26th Sep 2003 16:02
Summary

A simulation of beads on a piece of elastic...



Description

Move the mouse around while holding a bead to make the elastic bounce around the screen, and off walls.

This is the second version of my code. In this version you can hold any bead and fix/release the beads in the air by pressing 'f'. You can do some fun stuff with this extra option.

Feel free to change the default settings (the constant commands) as see what happens, I'd recommend you change them in small steps ;).

.EXE BELOW...



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `Elastic Physics Demo
`By Hamish McHaggis
`26/9/03
`This code demonstrates how to simulate the properties of elastic.  I saw it first on a javascript mouse
`trail, I have recreated it without looking at any of the javascript code.

`The amount of gravity acting on the beads
#CONSTANT GRAVITY 0.04
`The decay of the X movement of the beads, 1=no decay, 0=complete decay
#CONSTANT XDECAY 0.97
`The decay of the Y movement of the beads, 1=no decay, 0=complete decay
#CONSTANT YDECAY 0.97
`The number of beads on the string take into account that each bead weighs the beads above it down
#CONSTANT NUMBEADS 10
`The elasticity of the X movement of the beads
#CONSTANT XELASTICITY 0.02
`The elasticity of the Y movement of the beads
#CONSTANT YELASTICITY 0.02
`The amount the bead movement decays when it hits a wall, 1=no decay, 0=complete decay
#CONSTANT WALLDECAY 0.9
`The between beads, before the string tenses and elasticity acts
#CONSTANT SLACKNESS 1
`The diameter of the beads
#CONSTANT BEADSIZE 6

`Screen mode
screenWidth as word = 800
screenHeight as word = 600
set display mode screenWidth,screenHeight,16

beadDist as float
beadDistX as float
beadDistY as float
distRatioX as float
distRatioY as float

sync on
sync rate 60

type beadType
   X as float
   Y as float
   xMove as float
   yMove as float
endtype

dim bead(NUMBEADS) as beadType

position mouse 0,0

do
   `Draw background
   ink rgb(255,255,255),0
   box 0,0,screenWidth,screenHeight

   `Position parent bead
   bead(1).X=mousex()
   bead(1).Y=mousey()
   if bead(1).X-(BEADSIZE+10)/2<0 then bead(1).X=(BEADSIZE+10)/2
   if bead(1).X+(BEADSIZE+10)/2>screenWidth then bead(1).X=screenWidth-(BEADSIZE+10)/2
   if bead(1).Y-(BEADSIZE+10)/2<0 then bead(1).Y=(BEADSIZE+10)/2
   if bead(1).Y+(BEADSIZE+10)/2>screenHeight then bead(1).Y=screenHeight-(BEADSIZE+10)/2

   `Draw parent bead
   ink rgb(0,0,0),0
   circle bead(1).X,bead(1).Y,BEADSIZE

   `Loop though other beads
   for x=2 to NUMBEADS
      `Work out X and Y distance between the bead and the one before it
      beadDistX=bead(x).X-bead(x-1).X
      beadDistY=bead(x).Y-bead(x-1).Y
      `Work out total distance
      beadDist=sqrt(beadDistX^2+beadDistY^2)

      `If the beads are far enough apart, decrease the movement to create elasticity
      if beadDist>SLACKNESS
         dec bead(x).xMove,XELASTICITY*beadDistX
         dec bead(x).yMove,YELASTICITY*beadDistY
      endif

      `If bead is not last bead
      if x<>NUMBEADS
         `Work out distances between the bead and the one after it
         beadDistX=bead(x).X-bead(x+1).X
         beadDistY=bead(x).Y-bead(x+1).Y
         beadDist=sqrt(beadDistX^2+beadDistY^2)

         `If the beads are far enough apart, decrease the movement to create elasticity
         if beadDist>1
            dec bead(x).xMove,XELASTICITY*beadDistX
            dec bead(x).yMove,YELASTICITY*beadDistY
         endif
      endif

      `Decay the movement of the beads to simulate loss of energy
      bead(x).xMove=bead(x).xMove*XDECAY
      bead(x).yMove=bead(x).yMove*YDECAY

      `Apply gravity to bead movement
      inc bead(x).yMove,GRAVITY

      `Move beads
      inc bead(x).X,bead(x).xMove
      inc bead(x).Y,bead(x).yMove

      `If the beads hit a wall, make them bounce off of it
      if bead(x).X-(BEADSIZE+10)/2<0
         bead(x).X=+(BEADSIZE+10)/2
         bead(x).xMove=-bead(x).xMove*WALLDECAY
      endif
      if bead(x).X+(BEADSIZE+10)/2>screenWidth
         bead(x).X=screenWidth-(BEADSIZE+10)/2
         bead(x).xMove=-bead(x).xMove*WALLDECAY
      endif
      if bead(x).Y-(BEADSIZE+10)/2<0
         bead(x).yMove=-bead(x).yMove*WALLDECAY
         bead(x).Y=(BEADSIZE+10)/2
      endif
      if bead(x).Y+(BEADSIZE+10)/2>screenHeight
         bead(x).yMove=-bead(x).yMove*WALLDECAY
         bead(x).Y=screenHeight-(BEADSIZE+10)/2
      endif

      `Draw bead and string from it to the one before it
      ink rgb(0,0,0),0
      circle bead(x).X,bead(x).Y,BEADSIZE
      ink rgb(200,100,0),0
      line bead(x).X,bead(x).Y,bead(x-1).X,bead(x-1).Y
   next x

   sync
   cls
loop