C:\> Rostislav Persion's Projects

.:: Spring Mass Simulator ::.
Spring Mass Simulator




This is a spring mass system. In theory, you can simulated anything in classical physics using a spring mass system.



















SOFTWARE WRITTEN IN C#
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:   
   6:  namespace ParticleBounce
   7:  {
   8:      class Mass
   9:      {
  10:   
  11:          public bool Fixed = false;
  12:   
  13:          public double x = 0;
  14:          public double y = 0;
  15:          public double vx = 0;
  16:          public double vy = 0;
  17:          public double ax = 0;
  18:          public double ay = 0;
  19:          public double fx = 0;
  20:          public double fy = 0;
  21:   
  22:          public double mass = 10;
  23:          public double diameter = 20;
  24:   
  25:          public Queue<Spring> Springs = new Queue<Spring>();
  26:          public Queue<Collider> Colliders = new Queue<Collider>();
  27:   
  28:   
  29:          public void ConnectTo(Mass mass2, double k, double MaxLengthFactor)
  30:          {
  31:              Spring newSpring = new Spring();
  32:              newSpring.k = k;
  33:              newSpring.node = mass2;
  34:              newSpring.length = Mass.Distance(this, mass2);
  35:              newSpring.maxlength = MaxLengthFactor * newSpring.length;
  36:              Springs.Enqueue(newSpring);
  37:          }
  38:   
  39:          public void CollideWith(Mass mass2, double k)
  40:          {
  41:              Collider newCollider = new Collider();
  42:              newCollider.k = k;
  43:              newCollider.node = mass2;
  44:              newCollider.length = (this.diameter/2) + (mass2.diameter/2);
  45:              Colliders.Enqueue(newCollider);
  46:          }
  47:   
  48:          public void Move(double dt,double gravity)
  49:          {
  50:              if (!Fixed)
  51:              {
  52:                  ax = fx / mass;
  53:                  ay = (fy / mass) + gravity;
  54:                  vx += (ax * dt);
  55:                  vy += (ay * dt);
  56:                  x += (vx * dt);
  57:                  y += (vy * dt);
  58:              }
  59:   
  60:              fx = 0;
  61:              fy = 0;
  62:   
  63:          }
  64:   
  65:          public void Forces()
  66:          {
  67:              foreach (Spring sp in Springs)
  68:              {
  69:                  if (Mass.Distance(this, sp.node) >= sp.maxlength)
  70:                  {
  71:                      sp.broken = true;
  72:                  }
  73:                  if (!sp.broken)
  74:                  {
  75:                      this.fx += (((Mass.Distance(this, sp.node) - sp.length) * sp.k) / sp.length) * (sp.node.x - this.x);
  76:                      this.fy += (((Mass.Distance(this, sp.node) - sp.length) * sp.k) / sp.length) * (sp.node.y - this.y);
  77:                      sp.node.fx += (((Mass.Distance(this, sp.node) - sp.length) * sp.k) / sp.length) * (this.x - sp.node.x);
  78:                      sp.node.fy += (((Mass.Distance(this, sp.node) - sp.length) * sp.k) / sp.length) * (this.y - sp.node.y);
  79:                  }
  80:              }
  81:          }
  82:          
  83:          public void Collide()
  84:          {
  85:              foreach (Collider cl in Colliders)
  86:              {
  87:                  if (Mass.Distance(this, cl.node) <= ((this.diameter / 2) + (cl.node.diameter / 2)))
  88:                  {
  89:                      this.fx += (((Mass.Distance(this, cl.node) - cl.length) * cl.k) / cl.length) * (cl.node.x - this.x);
  90:                      this.fy += (((Mass.Distance(this, cl.node) - cl.length) * cl.k) / cl.length) * (cl.node.y - this.y);
  91:                      cl.node.fx += (((Mass.Distance(this, cl.node) - cl.length) * cl.k) / cl.length) * (this.x - cl.node.x);
  92:                      cl.node.fy += (((Mass.Distance(this, cl.node) - cl.length) * cl.k) / cl.length) * (this.y - cl.node.y);
  93:                  }
  94:              }
  95:          }
  96:   
  97:          public static double Distance(Mass mass1, Mass mass2)
  98:          {
  99:              return Math.Sqrt(Math.Pow(mass1.x - mass2.x, 2) + Math.Pow(mass1.y - mass2.y, 2));
 100:          }
 101:      }
 102:   
 103:      class Spring
 104:      {
 105:          public double k=1;
 106:          public double length=1;
 107:          public double maxlength = 2;
 108:          public bool broken = false;
 109:          public Mass node;
 110:      }
 111:   
 112:      class Collider
 113:      {
 114:          public double k=1;
 115:          public double length=1;
 116:          public Mass node;
 117:      }
 118:   
 119:  }