public void applyForce() { // force is proportional to the diff between restLen and current idst // a vector from A --> B Vector diff = new Vector(endA.pos, endB.pos); // compute the current distance float dist = diff.getMagnitude(); // compute dx, which is what the force depends on float dx = Math.abs(dist - restLen); // a vector containing just the direction component of A --> B Vector dir = diff.copy().normalize(); Vector force = dir.copy().scale(-K * dx, -K * dx); if (restLen < getDistance()) { // forces go INWARDS endB.addForce(force); endA.addForce(force.reverse()); } else { // forces go OUTWARDS endA.addForce(force); endB.addForce(force.reverse()); } }
public void applyForce() { float r = getDistance(); // make sure r is always >= 1 r = max(1, r); // compute the magnitude of the coulombs force float mag = K / (r * r); // normalize to extract direction, then scale by mag Vector force = new Vector(endA.pos, endB.pos).normalize().scale(mag, mag); // apply to end points endB.addForce(force); endA.addForce(force.reverse()); }