Advertisement
Guest User

1d mass spring system

a guest
Jun 22nd, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. /*
  2. tick:
  3.  
  4.  
  5.     Step one:  loop through every spring and add the force of the spring.
  6.  
  7.     Step two:   loop through every mass and compute the new position.
  8.  
  9.     Step three:  loop through all springs and, given it's new end point, compute the new forces on the spring.
  10.  
  11.  
  12. */
  13.  
  14. //Code for Spring.  (p and q are the current positions for the mass on the endpoints of the spring.). (p1 and q1 are the positions for last tick)
  15.  
  16.     public float[] go(float p, float q) {
  17.         float distance = p - q;
  18.         float distanceDelta = (p - p1) - (q - q1);
  19.  
  20.         float a = springiness * (Math.abs(distance) - length);
  21.         float b;
  22.         float c;
  23.         if (distance == 0) {
  24.             c = 1;
  25.             b = (damping * distanceDelta * distance) / .0000001f;
  26.         } else {
  27.             b = (damping * distanceDelta * distance) / Math.abs(distance);
  28.             c = distance / Math.abs(distance);
  29.         }
  30.  
  31.         float newForce = -(a + b) * c;
  32.  
  33.         p1 = p;
  34.         q1 = q;
  35.  
  36.         return new float[] { newForce, -newForce };
  37.     }
  38.  
  39.  
  40.  
  41. //Code for Mass
  42. public float go() {
  43.         if (fixed) {
  44.             return defaultPos;
  45.         }
  46.  
  47.         float newForce = runningForce * mass - n2 + 2f * n1;
  48.         n2 = n1;
  49.         n1 = newForce;
  50.         runningForce = 0;
  51.        
  52.        
  53.         return newForce;
  54.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement