class Spring { float stiffness;// = 0.5; // alters the force of the spring float damping;// = 0.5;// add some friction float mass;// = 2.0; float springLength = MAX_DIST;//30; ////////////// Spring () { } Spring (float s, float d, float m) { stiffness = s; damping = d; mass = m; } ////////////// // 1.input the CURRENT target position to 'spring towards' (i.e. the previous ball in array) // 2.calculate distnace (dx, dy) relative to the current postion of the ball // 3.calculate angle // 4.calculate where the object SHOULD move to (using springLength) (new target postion to maintain a distance from) // 5.calculate force and acceleration // 6. // // input 2 ball objects - rather than just their locations - so we can get their loc and vel//// void applySpring (Ball ball, Ball prevball, boolean recalc) { // tx, ty // calculate target (using springLength); // find distance between the locs of two ball locs Vector3D d = ball.loc.sub (ball.loc, prevball.loc); float distance = d.magnitude(); //println(int (d.magnitude())); // find angle float angle = d.heading2D (); //println (degrees (angle)); /////////////////////////////////////////// /// recalculate target positions float tx; float ty; if (recalc) { tx = prevball.loc.x+ cos (angle) * springLength; ty = prevball.loc.y + sin (angle) * springLength; } else { tx = prevball.loc.x; ty = prevball.loc.y; } // calculate force float xforce = stiffness * (tx - ball.loc.x); float yforce = stiffness * (ty - ball.loc.y); /// use mass to influence acceleration ///// float xAcc = xforce / mass; float yAcc = yforce / mass; // velocity = friction * (velocity + springforce); // ball.vel.x = damping * (ball.vel.x + xAcc); ball.vel.y = damping * (ball.vel.y + yAcc); //xvel = damping * (xvel + xAcc); //yvel = damping * (yvel + yAcc); // update location // ball.loc.add (ball.vel); } ///////////////// void moveBacktoTarget (Vector3D loc, Vector3D targ) { // find distance float distance = loc.distance (targ, loc); //println (distance); } }