-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
163 lines (131 loc) · 3.12 KB
/
run.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
int bot = document.getElementById('sources').offsetHeight;
int top = document.getElementById('header').offsetHeight;
int cnvW = window.innerWidth;
int cnvH = window.innerHeight - bot - top;
document.getElementById('printData').style.top = top;
Mover m;
float r = 50.0; //radius of blob
float strokeW = 10;
float edge = r-(strokeW/2);
int X, Y; // for initial positioning
int s; // stabilization
float nX, nY, nZ, gA, gB, gG, arA, arB, arG;
String dataLink = 'data/data.txt';
void setup() {
size(cnvW, cnvH);
background(10);
strokeWeight(strokeW);
frameRate(15);
X = width / 2;
Y = height / 2;
smooth();
m = new Mover(X, Y);
m.display(r);
}
void draw() {
r = r + sin(frameCount / 4);
noStroke();
fill(10, 40);
rect(0, 0, width, height);
iphoneControl();
}
void iphoneControl() {
String[] lines = loadStrings(dataLink);
if (lines.length > 0) {
String[] data = split(lines[0], ",");
nX = float(data[0]);
nY = float(data[1]);
nZ = float(data[2]);
//r = abs(nZ)*10; // possibly for 3D effect?
gA = float(data[3]);
gB = float(data[4]);
gG = float(data[5]);
arA = float(data[6]) / 100;
arB = float(data[7]) / 100;
arG = float(data[8]) / 100;
s = int(data[9]);
printData(nX, nY, nZ, gA, gB, gG, arA, arB, arG, s);
PVector moved = new PVector(nX, nY);
PVector accel = new PVector(0,0);
if (moved != m.loc) {
PVector accel = new PVector(arA,arB);
}
else {
PVector accel = new PVector(0,0);
}
if (s == 1) {
PVector accel = new PVector(0,0);
m.vel = accel;
}
m.applyForce(accel);
m.move(moved);
m.update();
m.display(r);
m.checkEdges();
}
else {
m.display(r);
}
}
class Mover {
PVector loc;
PVector vel;
PVector acc;
float mass;
Mover(float x, float y) {
loc = new PVector(x, y);
vel = new PVector(0, 0);
acc = new PVector(0, 0);
mass = 10;
}
void applyForce(PVector force) {
PVector f = PVector.div(force, mass);
acc.add(f);
}
void move(PVector move) {
loc.add(move);
}
void update() {
vel.add(acc);
loc.add(vel);
acc.mult(0);
}
void display(float r) {
fill(121, 0, 184);
stroke(255);
ellipse(loc.x, loc.y, r, r);
}
void checkEdges() {
if (loc.x > width-edge) {
vel.x *= -1;
loc.x = width-edge;
} else if (loc.x < edge) {
vel.x *= -1;
loc.x = edge;
}
if (loc.y > height-edge) {
vel.y *= -1;
loc.y = height-edge;
} else if (loc.y < edge) {
vel.y *= -1;
loc.y = edge;
}
}
}
void printData(float x, float y, float z, float a, float b, float g, float alph, float bet, float gam, int stab) {
divTxt = "<p>X : " + x + "</p>";
divTxt += "<p>Y : " + y + "</p>";
divTxt += "<p>Z : " + z + "</p>";
divTxt += "<p>A : " + a + "</p>";
divTxt += "<p>B : " + b + "</p>";
divTxt += "<p>G : " + g + "</p>";
divTxt += "<p>arA : " + alph + "</p>";
divTxt += "<p>arB : " + bet + "</p>";
divTxt += "<p>arG : " + gam + "</p>";
if (stab == 1) { divTxt += "<p>Stabilized!</p>"; }
document.getElementById("printData").innerHTML = divTxt;
}
/* notes for later, gator:
http://ditio.net/2008/11/04/php-string-to-hex-and-hex-to-string-functions/
http://www.html5rocks.com/en/mobile/touch/
*/