-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathPositionable.pde
executable file
·418 lines (365 loc) · 8.39 KB
/
Positionable.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
* Manipulable object: translate, rotate, scale, flip h/v
*/
abstract class Positionable extends Position implements Drawable {
// HELPER FUNCTION FOR JAVASCRIPT
void jsupdate() {
if(monitoredByJavaScript && javascript != null) {
javascript.updatedPositionable(this); }}
/**
* We track two frames for computational purposes,
* such as performing boundary collision detection.
*/
Position previous = new Position();
/**
* Boundaries this positionable is attached to.
*/
ArrayList<Boundary> boundaries;
/**
* Decals that are drawn along with this positionable,
* but do not contribute to any overlap or collision
* detection, nor explicitly interact with things.
*/
ArrayList<Decal> decals;
// shortcut variable that tells us whether
// or not this positionable needs to perform
// boundary collision checks
boolean inMotion = false;
/**
* Cheap constructor
*/
Positionable() {
boundaries = new ArrayList<Boundary>();
decals = new ArrayList<Decal>();
}
/**
* Set up a manipulable object
*/
Positionable(float _x, float _y, float _width, float _height) {
this();
x = _x;
y = _y;
width = _width;
height = _height;
ox = width/2;
oy = -height/2;
}
/**
* change the position, absolute
*/
void setPosition(float _x, float _y) {
x = _x;
y = _y;
previous.x = x;
previous.y = y;
aFrameCount = 0;
direction = -1;
jsupdate();
}
/**
* Attach this positionable to a boundary.
*/
void attachTo(Boundary b) {
boundaries.add(b);
}
/**
* Check whether this positionable is
* attached to a specific boundary.
*/
boolean isAttachedTo(Boundary b) {
return boundaries.contains(b);
}
/**
* Detach this positionable from a
* specific boundary.
*/
void detachFrom(Boundary b) {
boundaries.remove(b);
}
/**
* Detach this positionable from all
* boundaries that it is attached to.
*/
void detachFromAll() {
boundaries.clear();
}
/**
* attach a Decal to this positionable.
*/
void addDecal(Decal d) {
decals.add(d);
d.setOwner(this);
}
/**
* detach a Decal from this positionable.
*/
void removeDecal(Decal d) {
decals.remove(d);
}
/**
* detach all Decal from this positionable.
*/
void removeAllDecals() {
decals.clear();
}
/**
* change the position, relative
*/
void moveBy(float _x, float _y) {
x += _x;
y += _y;
previous.x = x;
previous.y = y;
aFrameCount = 0;
jsupdate();
}
/**
* check whether this Positionable is moving. If it's not,
* it will not be boundary-collision-evalutated.
*/
void verifyInMotion() {
inMotion = (ix!=0 || iy!=0 || fx!=0 || fy!=0 || ixA!=0 || iyA !=0);
}
/**
* set the impulse for this object
*/
void setImpulse(float x, float y) {
ix = x;
iy = y;
jsupdate();
verifyInMotion();
}
/**
* set the impulse coefficient for this object
*/
void setImpulseCoefficients(float fx, float fy) {
ixF = fx;
iyF = fy;
jsupdate();
verifyInMotion();
}
/**
* add to the impulse for this object
*/
void addImpulse(float _ix, float _iy) {
ix += _ix;
iy += _iy;
jsupdate();
verifyInMotion();
}
/**
* Update which direction this positionable is
* "looking at".
*/
void setViewDirection(float dx, float dy) {
if(dx!=0 || dy!=0) {
direction = atan2(dy,dx);
if(direction<0) {
direction+=2*PI; }}
}
/**
* collisions may force us to stop object's movement.
*/
void stop() {
ix = 0;
iy = 0;
jsupdate();
}
/**
* Set the external forces acting on this actor
*/
void setForces(float _fx, float _fy) {
fx = _fx;
fy = _fy;
jsupdate();
verifyInMotion();
}
/**
* Augment the external forces acting on this actor
*/
void addForces(float _fx, float _fy) {
fx += _fx;
fy += _fy;
jsupdate();
verifyInMotion();
}
/**
* set the uniform acceleration for this object
*/
void setAcceleration(float ax, float ay) {
ixA = ax;
iyA = ay;
aFrameCount = 0;
jsupdate();
verifyInMotion();
}
/**
* Augment the accelleration for this object
*/
void addAccelleration(float ax, float ay) {
ixA += ax;
iyA += ay;
jsupdate();
verifyInMotion();
}
/**
* set the translation to be the specified x/y values.
*/
void setTranslation(float x, float y) {
ox = x;
oy = y;
jsupdate();
}
/**
* set the scale to uniformly be the specified value.
*/
void setScale(float s) {
sx = s;
sy = s;
jsupdate();
}
/**
* set the scale to be the specified x/y values.
*/
void setScale(float x, float y) {
sx = x;
sy = y;
jsupdate();
}
/**
* set the rotation to be the specified value.
*/
void setRotation(float _r) {
r = _r % (2*PI);
jsupdate();
}
/**
* flip this object horizontally.
*/
void setHorizontalFlip(boolean _hflip) {
if(hflip!=_hflip) { ox = -ox; }
for(Decal d: decals) { d.setHorizontalFlip(_hflip); }
hflip = _hflip;
jsupdate();
}
/**
* flip this object vertically.
*/
void setVerticalFlip(boolean _vflip) {
if(vflip!=_vflip) { oy = -oy; }
for(Decal d: decals) { d.setVerticalFlip(_vflip); }
vflip = _vflip;
jsupdate();
}
/**
* set this object's visibility
*/
void setVisibility(boolean _visible) {
visible = _visible;
jsupdate();
}
/**
* mark object static or animated
*/
void setAnimated(boolean _animated) {
animated = _animated;
jsupdate();
}
/**
* get the previous x coordinate.
*/
float getPrevX() { return previous.x + previous.ox; }
/**
* get the previous y coordinate.
*/
float getPrevY() { return previous.y + previous.oy; }
/**
* get the current x coordinate.
*/
float getX() { return x + ox; }
/**
* get the current y coordinate.
*/
float getY() { return y + oy; }
/**
* Set up the coordinate transformations
* and then call whatever implementation
* of "drawObject" exists.
*/
void draw(float vx, float vy, float vw, float vh) {
// Draw, if visible
if (visible && drawableFor(vx,vy,vw,vh)) {
pushMatrix();
applyTransforms();
drawObject();
for(Decal d: decals) { d.draw(); }
popMatrix();
}
// Update position for next the frame,
// based on impulse and force.
if(animated) { update(); }
}
/**
* must be implemented by subclasses,
* to indicate whether this object is
* visible in this viewbox.
*/
abstract boolean drawableFor(float vx, float vy, float vw, float vh);
/**
* Update all the position parameters.
* If fixed is not null, it is the boundary
* we just attached to, and we cannot detach
* from it on the same frame.
*/
void update() {
// cache frame information
previous.copyFrom(this);
// work external forces into our current impulse
addImpulse(fx,fy);
// work in impulse coefficients (typically, drag)
ix *= ixF;
iy *= iyF;
// not on a boundary: unrestricted motion,
// so make sure the acceleration factor exists.
if(boundaries.size()==0) { aFrameCount++; }
// we're attached to one or more boundaries, so we
// are subject to (compound) impulse redirection.
else {
aFrameCount = 0;
float[] redirected = new float[]{ix, iy};
for(int b=boundaries.size()-1; b>=0; b--) {
Boundary boundary = boundaries.get(b);
if(!boundary.disabled) {
redirected = boundary.redirectForce(this, redirected[0], redirected[1]);
}
if(boundary.disabled || !boundary.supports(this)) {
detachFrom(boundary);
continue;
}
}
ix = redirected[0];
iy = redirected[1];
}
// Not unimportant: cutoff resolution.
if(abs(ix) < 0.01) { ix = 0; }
if(abs(iy) < 0.01) { iy = 0; }
// update the physical position
x += ix + (aFrameCount * ixA);
y += iy + (aFrameCount * iyA);
}
/**
* Reset this positional to its previous state
*/
void rewind() {
copyFrom(previous);
jsupdate();
}
// implemented by subclasses
abstract void drawObject();
// mostly for debugging purposes
String toString() {
return width+"/"+height + "\n" +
"current: " + super.toString() + "\n" +
"previous: " + previous.toString() + "\n";
}
}