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 updatePosition(float dt) { if (fixed) { netForce.reset(); // Shouldn't accumulate forces if fixed return; } updateAcceleration(dt); updateVelocity(dt); Point prev = new Point(pos.x, pos.y); pos.add(vel.copy().scale(dt, dt)); ensureInBounds(); }
// f = m * a --> a = f / m private void updateAcceleration(float dt) { // println("node = " + id + ", netforce = " + netForce); Vector prev = acc; Float f1 = new Float(netForce.x); Float f2 = new Float(netForce.y); if (f1.isNaN(f1) || f2.isNaN(f2)) { netForce = new Vector(); } float scale = 1.0f / mass; this.acc = netForce.copy().scale(scale, scale); if (id.equals("*.1.0-10")) { // println("NewAcc = " + acc); // System.exit(1); } // reset netForce for next time netForce.reset(); }
private void updateVelocity(float dt) { Vector prev = vel.copy(); vel.add(acc.scale(dt, dt)); }