Created
August 19, 2012 00:07
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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