Skip to content

Instantly share code, notes, and snippets.

@tlehman
Created August 19, 2012 00:07
Show Gist options
  • Select an option

  • Save tlehman/3390473 to your computer and use it in GitHub Desktop.

Select an option

Save tlehman/3390473 to your computer and use it in GitHub Desktop.
An integrator from Ben Fry's "Visualizing Data" (based on Hooke's law for springs)
class Integrator {
final float DAMPING = 0.5f;
final float ATTRACTION = 0.2f;
float value;
float vel;
float accel;
float force;
float mass = 1;
float damping = DAMPING;
float attraction = ATTRACTION;
boolean targeting;
float target;
Integrator() { }
Integrator(float value) {
this.value = value;
}
Integrator(float value, float damping, float attraction) {
this.value = value;
this.damping = damping;
this.attraction = attraction;
}
void set(float v) {
value = v;
}
void update() {
if (targeting) {
force += attraction * (target - value);
}
accel = force / mass;
vel = (vel + accel) * damping;
value += vel;
force = 0;
}
void target(float t) {
targeting = true;
target = t;
}
void noTarget() {
targeting = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment