-
Notifications
You must be signed in to change notification settings - Fork 0
/
Integrator.java
78 lines (56 loc) · 1.33 KB
/
Integrator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Modifed version of Ben Fry's integrator for interpolating between two values
public class Integrator {
static final float ATTRACTION = 0.1f; // formerly 0.2f
static final float DAMPING = 0.5f; // formerly 0.5f
float value = 0;
float vel = 0;
float accel = 0;
float force = 0;
float mass = 1;
float damping;
float attraction;
boolean targeting;
float target;
public Integrator() {
this.value = 0;
this.damping = DAMPING;
this.attraction = ATTRACTION;
}
public Integrator(float value) {
this.value = value;
this.damping = DAMPING;
this.attraction = ATTRACTION;
}
public Integrator(float value, float attraction, float damping) {
this.value = value;
this.damping = damping;
this.attraction = attraction;
}
public void set(float v) {
value = v;
}
public float get() {
return value;
}
public void setAttraction(float a) {
attraction = a;
}
public void setDamping(float d) {
damping = d;
}
public boolean update() {
if (targeting) {
force += attraction * (target - value);
accel = force / mass;
vel = (vel + accel) * damping;
value += vel;
force = 0;
return (vel > 0.0001f);
}
return false;
}
public void target(float t) {
targeting = true;
target = t;
}
}