-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimator.pde
63 lines (51 loc) · 1.11 KB
/
Animator.pde
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
static class Animator {
static List<Animator> allAnimators; /* looks like we can use generics after al */
final float attraction = 0.2;
final float reached_threshold = 10e-3;
float value;
float target;
float oldtarget;
boolean targeting;
float velocity;
Animator()
{
if (allAnimators == null) allAnimators = new ArrayList();
allAnimators.add(this);
targeting = false;
}
Animator(float value_)
{
this();
value = value_;
}
void close()
{
allAnimators.remove(this);
}
void set(float value_)
{
value = value_;
}
void target(float target_)
{
if (target_ != target) oldtarget = target;
target = target_;
targeting = (target != value);
}
void update()
{
if (!targeting) return;
float a = attraction * (target - value);
velocity = (velocity + a) / 2;
value += velocity;
if (abs(target - value) < reached_threshold) {
value = target;
targeting = false;
velocity = 0;
}
}
static void updateAll()
{
for (Animator animator : allAnimators) animator.update();
}
}