-
Notifications
You must be signed in to change notification settings - Fork 2
/
Walker.pde
116 lines (99 loc) · 2.67 KB
/
Walker.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class Walker {
float height;
PVector head, shoulder, waist;
PVector[][] arms;
PVector[][] legs;
float neckLength = 0.13;
float upperBodyLength = 0.52;
float[] armLengths = {
0.24, 0.25
};
float[] legLengths = {
0.28, 0.3
};
float[] params = {
0, // arm1
0.35,
0,
-0.01, // arm2
0.12,
-HALF_PI * 0.18,
0.58, // leg1
0.32,
-HALF_PI * 0.05,
0.5 - 0.288, // leg2
0.26,
HALF_PI * 0.27
};
Walker(float height) {
this.height = height;
neckLength *= height;
upperBodyLength *= height;
head = new PVector();
shoulder = new PVector();
waist = new PVector();
arms = new PVector[2][2];
legs = new PVector[2][2];
for (int i=0; i<2; i++) {
for (int j=0; j<2; j++) {
arms[i][j] = new PVector();
legs[i][j] = new PVector();
}
armLengths[i] *= height;
legLengths[i] *= height;
}
update(0.0);
}
float arm1(int side, float phase) {
phase += side * 0.5 + params[0];
return HALF_PI + sin(phase * TWO_PI) * HALF_PI * params[1] + params[2];
}
float arm2(int side, float phase) {
phase += side * 0.5 + params[3];
return sin(phase * TWO_PI) * HALF_PI * params[4] + params[5];
}
float leg1(int side, float phase) {
phase += side * 0.5 + params[6];
return HALF_PI + sin(phase * TWO_PI) * HALF_PI * params[7] + params[8];
}
float leg2(int side, float phase) {
phase += side * 0.5 + params[9];
return sin(phase * TWO_PI) * HALF_PI * params[10] + params[11];
}
float calcYOffset(float phase) {
return max(legs[0][1].y, legs[1][1].y);
}
void setBasicPositions() {
head.set(0, 0);
shoulder.set(0, neckLength);
waist.set(0, upperBodyLength);
}
void update(float phase) {
float angle;
setBasicPositions();
for (int i=0; i<2; i++) {
angle = arm1(i, phase);
arms[i][0].set(cos(angle) * armLengths[0], sin(angle) * armLengths[0]);
arms[i][0].add(shoulder);
angle += arm2(i, phase);
arms[i][1].set(cos(angle) * armLengths[1], sin(angle) * armLengths[1]);
arms[i][1].add(arms[i][0]);
angle = leg1(i, phase);
legs[i][0].set(cos(angle) * legLengths[0], sin(angle) * legLengths[0]);
legs[i][0].add(waist);
angle += leg2(i, phase);
legs[i][1].set(cos(angle) * legLengths[1], sin(angle) * legLengths[1]);
legs[i][1].add(legs[i][0]);
}
float yoffset = calcYOffset(phase);
head.sub(0, yoffset, 0);
shoulder.sub(0, yoffset, 0);
waist.sub(0, yoffset, 0);
for (int i=0; i<2; i++) {
for (int j=0; j<2; j++) {
arms[i][j].sub(0, yoffset, 0);
legs[i][j].sub(0, yoffset, 0);
}
}
}
}