1
|
package org.distorted.library;
|
2
|
|
3
|
import java.util.Vector;
|
4
|
|
5
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
6
|
/**
|
7
|
* A 3-dimensional implementation of the Interpolator class to interpolate between a list
|
8
|
* of Float3Ds.
|
9
|
*/
|
10
|
|
11
|
public class Interpolator3D extends Interpolator
|
12
|
{
|
13
|
|
14
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
15
|
// the coefficients of the X(t), Y(t) and Z(t) polynomials: X(t) = ax*T^3 + bx*T^2 + cx*t + dx etc.
|
16
|
// (x,y,z) is the vector tangent to the path.
|
17
|
// (vx,vy,vz) is the original vector from vv (copied here so when interpolating we can see if it is
|
18
|
// still valid and if not - rebuild the Cache
|
19
|
|
20
|
private class VectorCache
|
21
|
{
|
22
|
float ax, bx, cx, dx;
|
23
|
float ay, by, cy, dy;
|
24
|
float az, bz, cz, dz;
|
25
|
|
26
|
float x,y,z;
|
27
|
float vx,vy,vz;
|
28
|
}
|
29
|
|
30
|
private class VectorNoise
|
31
|
{
|
32
|
float[] nx;
|
33
|
float[] ny;
|
34
|
float[] nz;
|
35
|
|
36
|
public VectorNoise()
|
37
|
{
|
38
|
nx = new float[NUM_NOISE];
|
39
|
nx[0] = mRnd.nextFloat();
|
40
|
for(int i=1; i<NUM_NOISE; i++) nx[i] = nx[i-1]+mRnd.nextFloat();
|
41
|
float sum = nx[NUM_NOISE-1] + mRnd.nextFloat();
|
42
|
for(int i=0; i<NUM_NOISE; i++) nx[i] /=sum;
|
43
|
|
44
|
ny = new float[NUM_NOISE];
|
45
|
for(int i=0; i<NUM_NOISE; i++) ny[i] = mRnd.nextFloat()-0.5f;
|
46
|
|
47
|
nz = new float[NUM_NOISE];
|
48
|
for(int i=0; i<NUM_NOISE; i++) nz[i] = mRnd.nextFloat()-0.5f;
|
49
|
}
|
50
|
}
|
51
|
|
52
|
private Vector<VectorCache> vc;
|
53
|
private VectorCache tmp1, tmp2;
|
54
|
|
55
|
private Vector<Float3D> vv;
|
56
|
private Float3D prev, curr, next;
|
57
|
|
58
|
private Vector<VectorNoise> vn;
|
59
|
private VectorNoise tmpN;
|
60
|
|
61
|
private float mFactor1, mFactor2; // used in Noise only. Those are noise factors; 1=noise of the (vec1X,vec1Y,vec1Z) vector; 2=noise of (vec2X,vec2Y,vec2Z)
|
62
|
private float vec1X,vec1Y,vec1Z; // vector perpendicular to v(t) and in the same plane as v(t) and a(t) (for >2 points only, in case of 2 points this is calculated differently)
|
63
|
private float vec2X,vec2Y,vec2Z; // vector perpendicular to v(t0 and to vec1.
|
64
|
|
65
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66
|
|
67
|
synchronized void createNoise()
|
68
|
{
|
69
|
if( vn==null )
|
70
|
{
|
71
|
vn = new Vector<VectorNoise>();
|
72
|
for(int i=0; i<numPoints; i++) vn.add(new VectorNoise());
|
73
|
}
|
74
|
}
|
75
|
|
76
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
77
|
// no array bounds checking!
|
78
|
|
79
|
private void vec(int c)
|
80
|
{
|
81
|
int p = c>0 ? c-1: numPoints-1;
|
82
|
int n = c<numPoints-1 ? c+1: 0;
|
83
|
|
84
|
prev = vv.elementAt(p);
|
85
|
curr = vv.elementAt(c);
|
86
|
next = vv.elementAt(n);
|
87
|
|
88
|
tmp1 = vc.elementAt(c);
|
89
|
|
90
|
float px = curr.x - prev.x;
|
91
|
float py = curr.y - prev.y;
|
92
|
float pz = curr.z - prev.z;
|
93
|
float nx = next.x - curr.x;
|
94
|
float ny = next.y - curr.y;
|
95
|
float nz = next.z - curr.z;
|
96
|
|
97
|
float d = nx*nx+ny*ny+nz*nz;
|
98
|
|
99
|
if( d>0 )
|
100
|
{
|
101
|
float q = (float)Math.sqrt((px*px+py*py+pz*pz)/d);
|
102
|
|
103
|
if( q>1 )
|
104
|
{
|
105
|
tmp1.x = nx+px/q;
|
106
|
tmp1.y = ny+py/q;
|
107
|
tmp1.z = nz+pz/q;
|
108
|
}
|
109
|
else
|
110
|
{
|
111
|
tmp1.x = px+nx*q;
|
112
|
tmp1.y = py+ny*q;
|
113
|
tmp1.z = pz+nz*q;
|
114
|
}
|
115
|
}
|
116
|
else
|
117
|
{
|
118
|
tmp1.x = 0.0f;
|
119
|
tmp1.y = 0.0f;
|
120
|
tmp1.z = 0.0f;
|
121
|
}
|
122
|
}
|
123
|
|
124
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125
|
|
126
|
private void recomputeCache()
|
127
|
{
|
128
|
if( numPoints==1 )
|
129
|
{
|
130
|
tmp1= vc.elementAt(0);
|
131
|
curr= vv.elementAt(0);
|
132
|
|
133
|
tmp1.ax = tmp1.ay = tmp1.az = 0.0f;
|
134
|
tmp1.bx = tmp1.by = tmp1.bz = 0.0f;
|
135
|
tmp1.cx = curr.x;
|
136
|
tmp1.cy = curr.y;
|
137
|
tmp1.cz = curr.z;
|
138
|
tmp1.dx = tmp1.dy = tmp1.dz = 0.0f;
|
139
|
}
|
140
|
else if( numPoints==2 )
|
141
|
{
|
142
|
tmp1= vc.elementAt(0);
|
143
|
tmp2= vc.elementAt(1);
|
144
|
curr= vv.elementAt(0);
|
145
|
next= vv.elementAt(1);
|
146
|
|
147
|
tmp1.ax = tmp1.ay = tmp1.az = 0.0f;
|
148
|
tmp1.bx = tmp1.by = tmp1.bz = 0.0f;
|
149
|
tmp1.cx = next.x - curr.x;
|
150
|
tmp1.cy = next.y - curr.y;
|
151
|
tmp1.cz = next.z - curr.z;
|
152
|
tmp1.dx = curr.x;
|
153
|
tmp1.dy = curr.y;
|
154
|
tmp1.dz = curr.z;
|
155
|
|
156
|
tmp2.ax = tmp2.ay = tmp2.az = 0.0f;
|
157
|
tmp2.bx = tmp2.by = tmp2.bz = 0.0f;
|
158
|
tmp2.cx = curr.x - next.x;
|
159
|
tmp2.cy = curr.y - next.y;
|
160
|
tmp2.cz = curr.z - next.z;
|
161
|
tmp2.dx = next.x;
|
162
|
tmp2.dy = next.y;
|
163
|
tmp2.dz = next.z;
|
164
|
}
|
165
|
else
|
166
|
{
|
167
|
int i, n;
|
168
|
|
169
|
for(i=0; i<numPoints; i++) vec(i);
|
170
|
|
171
|
for(i=0; i<numPoints; i++)
|
172
|
{
|
173
|
n = i<numPoints-1 ? i+1:0;
|
174
|
|
175
|
tmp1= vc.elementAt(i);
|
176
|
tmp2= vc.elementAt(n);
|
177
|
curr= vv.elementAt(i);
|
178
|
next= vv.elementAt(n);
|
179
|
|
180
|
tmp1.vx = curr.x;
|
181
|
tmp1.vy = curr.y;
|
182
|
tmp1.vz = curr.z;
|
183
|
|
184
|
tmp1.ax = 2*curr.x + tmp1.x - 2*next.x + tmp2.x;
|
185
|
tmp1.bx = -3*curr.x - 2*tmp1.x + 3*next.x - tmp2.x;
|
186
|
tmp1.cx = tmp1.x;
|
187
|
tmp1.dx = curr.x;
|
188
|
|
189
|
tmp1.ay = 2*curr.y + tmp1.y - 2*next.y + tmp2.y;
|
190
|
tmp1.by = -3*curr.y - 2*tmp1.y + 3*next.y - tmp2.y;
|
191
|
tmp1.cy = tmp1.y;
|
192
|
tmp1.dy = curr.y;
|
193
|
|
194
|
tmp1.az = 2*curr.z + tmp1.z - 2*next.z + tmp2.z;
|
195
|
tmp1.bz = -3*curr.z - 2*tmp1.z + 3*next.z - tmp2.z;
|
196
|
tmp1.cz = tmp1.z;
|
197
|
tmp1.dz = curr.z;
|
198
|
}
|
199
|
}
|
200
|
|
201
|
cacheDirty = false;
|
202
|
}
|
203
|
|
204
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
205
|
|
206
|
private float noise(float time,int vecNum)
|
207
|
{
|
208
|
float lower, upper, len;
|
209
|
float d = time*(NUM_NOISE+1);
|
210
|
int index = (int)d;
|
211
|
if( index>=NUM_NOISE+1 ) index=NUM_NOISE;
|
212
|
tmpN = vn.elementAt(vecNum);
|
213
|
|
214
|
float t = d-index;
|
215
|
t = t*t*(3-2*t);
|
216
|
|
217
|
switch(index)
|
218
|
{
|
219
|
case 0 : mFactor1 = mNoise*tmpN.ny[0]*t;
|
220
|
mFactor2 = mNoise*tmpN.nz[0]*t;
|
221
|
return time + mNoise*(d*tmpN.nx[0]-time);
|
222
|
case NUM_NOISE: mFactor1= mNoise*tmpN.ny[NUM_NOISE-1]*(1-t);
|
223
|
mFactor2= mNoise*tmpN.nz[NUM_NOISE-1]*(1-t);
|
224
|
len = ((float)NUM_NOISE)/(NUM_NOISE+1);
|
225
|
lower = len + mNoise*(tmpN.nx[NUM_NOISE-1]-len);
|
226
|
return (1.0f-lower)*(d-NUM_NOISE) + lower;
|
227
|
default : float ya,yb;
|
228
|
yb = tmpN.ny[index ];
|
229
|
ya = tmpN.ny[index-1];
|
230
|
mFactor1 = mNoise*((yb-ya)*t+ya);
|
231
|
yb = tmpN.nz[index ];
|
232
|
ya = tmpN.nz[index-1];
|
233
|
mFactor2 = mNoise*((yb-ya)*t+ya);
|
234
|
|
235
|
len = ((float)index)/(NUM_NOISE+1);
|
236
|
lower = len + mNoise*(tmpN.nx[index-1]-len);
|
237
|
len = ((float)index+1)/(NUM_NOISE+1);
|
238
|
upper = len + mNoise*(tmpN.nx[index ]-len);
|
239
|
|
240
|
return (upper-lower)*(d-index) + lower;
|
241
|
}
|
242
|
}
|
243
|
|
244
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
245
|
// v is the speed vector (i.e. position p(t) differentiated by time)
|
246
|
// a is the acceleration vector (differentiate once more)
|
247
|
// now what we are doing is compute vec1{X,Y,Z} to be a vector perpendicular to v and in the same plane as both v and a.
|
248
|
// vec2{X,Y,Z} would be (v)x(vec1).
|
249
|
//
|
250
|
// vec1 = a-delta*v where delta = (v*a)/|v|^2 (see Gram-Schmidt)
|
251
|
|
252
|
private void setUpVectors(float time,VectorCache vc)
|
253
|
{
|
254
|
if( vc!=null )
|
255
|
{
|
256
|
float vx = (3*vc.ax*time+2*vc.bx)*time+vc.cx;
|
257
|
float vy = (3*vc.ay*time+2*vc.by)*time+vc.cy;
|
258
|
float vz = (3*vc.az*time+2*vc.bz)*time+vc.cz;
|
259
|
|
260
|
float ax = 6*vc.ax*time+2*vc.bx;
|
261
|
float ay = 6*vc.ay*time+2*vc.by;
|
262
|
float az = 6*vc.az*time+2*vc.bz;
|
263
|
|
264
|
float v_sq = vx*vx+vy*vy+vz*vz;
|
265
|
float delta = (vx*ax+vy*ay+vz*az)/v_sq;
|
266
|
|
267
|
vec1X = ax-delta*vx;
|
268
|
vec1Y = ay-delta*vy;
|
269
|
vec1Z = az-delta*vz;
|
270
|
|
271
|
vec2X = vy*vec1Z-vz*vec1Y;
|
272
|
vec2Y = vz*vec1X-vx*vec1Z;
|
273
|
vec2Z = vx*vec1Y-vy*vec1X;
|
274
|
|
275
|
float len1 = (float)Math.sqrt(v_sq/(vec1X*vec1X+vec1Y*vec1Y+vec1Z*vec1Z));
|
276
|
float len2 = (float)Math.sqrt(v_sq/(vec2X*vec2X+vec2Y*vec2Y+vec2Z*vec2Z));
|
277
|
|
278
|
vec1X*=len1;
|
279
|
vec1Y*=len1;
|
280
|
vec1Z*=len1;
|
281
|
|
282
|
vec2X*=len2;
|
283
|
vec2Y*=len2;
|
284
|
vec2Z*=len2;
|
285
|
}
|
286
|
else
|
287
|
{
|
288
|
curr = vv.elementAt(0);
|
289
|
next = vv.elementAt(1);
|
290
|
|
291
|
float vx = (next.x-curr.x);
|
292
|
float vy = (next.y-curr.y);
|
293
|
float vz = (next.z-curr.z);
|
294
|
|
295
|
float b = (float)Math.sqrt(vx*vx+vy*vy);
|
296
|
|
297
|
if( b>0.0f )
|
298
|
{
|
299
|
vec1X = vx*vz/b;
|
300
|
vec1Y = vy*vz/b;
|
301
|
vec1Z = -b;
|
302
|
|
303
|
vec2X = vy*vec1Z-vz*vec1Y;
|
304
|
vec2Y = vz*vec1X-vx*vec1Z;
|
305
|
vec2Z = vx*vec1Y-vy*vec1X;
|
306
|
|
307
|
float len2 = (float)Math.sqrt((vx*vx+vy*vy+vz*vz)/(vec2X*vec2X+vec2Y*vec2Y+vec2Z*vec2Z));
|
308
|
|
309
|
vec2X*=len2;
|
310
|
vec2Y*=len2;
|
311
|
vec2Z*=len2;
|
312
|
}
|
313
|
else
|
314
|
{
|
315
|
vec1X = vz;
|
316
|
vec1Y = 0.0f;
|
317
|
vec1Z = 0.0f;
|
318
|
|
319
|
vec2X = 0.0f;
|
320
|
vec2Y = vz;
|
321
|
vec2Z = 0.0f;
|
322
|
}
|
323
|
}
|
324
|
}
|
325
|
|
326
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
327
|
// PUBLIC API
|
328
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
329
|
/**
|
330
|
* Default constructor.
|
331
|
*/
|
332
|
public Interpolator3D()
|
333
|
{
|
334
|
vv = new Vector<Float3D>();
|
335
|
vc = new Vector<VectorCache>();
|
336
|
vn = null;
|
337
|
numPoints = 0;
|
338
|
cacheDirty = false;
|
339
|
mMode = MODE_LOOP;
|
340
|
mDuration = 0;
|
341
|
mCount = 0.5f;
|
342
|
mNoise = 0.0f;
|
343
|
}
|
344
|
|
345
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
346
|
/**
|
347
|
* Returns the location'th Float3D.
|
348
|
*
|
349
|
* @param location the index of the Point we are interested in.
|
350
|
* @return The Float3D, if 0<=location<getNumPoints(), or null otherwise.
|
351
|
*/
|
352
|
public synchronized Float3D getPoint(int location)
|
353
|
{
|
354
|
return (location>=0 && location<numPoints) ? vv.elementAt(location) : null;
|
355
|
}
|
356
|
|
357
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
358
|
/**
|
359
|
* Resets the location'th Point.
|
360
|
*
|
361
|
* @param location the index of the Point we are setting.
|
362
|
* @param x New value of its first float.
|
363
|
*/
|
364
|
public synchronized void setPoint(int location, float x, float y, float z)
|
365
|
{
|
366
|
if( location>=0 && location<numPoints )
|
367
|
{
|
368
|
curr = vv.elementAt(location);
|
369
|
|
370
|
if( curr!=null )
|
371
|
{
|
372
|
curr.set(x,y,z);
|
373
|
cacheDirty=true;
|
374
|
}
|
375
|
}
|
376
|
}
|
377
|
|
378
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
379
|
/**
|
380
|
* Adds a new Float3D to the end of our list of Points to interpolate through.
|
381
|
* <p>
|
382
|
* Only a reference to the Point gets added to the List; this means that one can add a Point
|
383
|
* here, and later on {@link Float3D#set(float,float,float)} it to some new value and the
|
384
|
* change will be seamlessly reflected in the interpolated path.
|
385
|
* <p>
|
386
|
* A Point can be added multiple times.
|
387
|
*
|
388
|
* @param v The Point to add.
|
389
|
*/
|
390
|
public synchronized void add(Float3D v)
|
391
|
{
|
392
|
if( v!=null )
|
393
|
{
|
394
|
vv.add(v);
|
395
|
|
396
|
if( vn!=null ) vn.add(new VectorNoise());
|
397
|
|
398
|
switch(numPoints)
|
399
|
{
|
400
|
case 0: break;
|
401
|
case 1: setUpVectors(0.0f,null);
|
402
|
break;
|
403
|
case 2: vc.add(new VectorCache());
|
404
|
vc.add(new VectorCache());
|
405
|
vc.add(new VectorCache());
|
406
|
break;
|
407
|
default:vc.add(new VectorCache());
|
408
|
}
|
409
|
|
410
|
numPoints++;
|
411
|
cacheDirty = true;
|
412
|
}
|
413
|
}
|
414
|
|
415
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
416
|
/**
|
417
|
* Adds a new Float3D to the location'th place in our List of Points to interpolate through.
|
418
|
*
|
419
|
* @param location Index in our List to add the new Point at.
|
420
|
* @param v The Point to add.
|
421
|
*/
|
422
|
public synchronized void add(int location, Float3D v)
|
423
|
{
|
424
|
if( v!=null )
|
425
|
{
|
426
|
vv.add(location, v);
|
427
|
|
428
|
if( vn!=null ) vn.add(new VectorNoise());
|
429
|
|
430
|
switch(numPoints)
|
431
|
{
|
432
|
case 0: break;
|
433
|
case 1: setUpVectors(0.0f,null);
|
434
|
break;
|
435
|
case 2: vc.add(new VectorCache());
|
436
|
vc.add(new VectorCache());
|
437
|
vc.add(new VectorCache());
|
438
|
break;
|
439
|
default:vc.add(location,new VectorCache());
|
440
|
}
|
441
|
|
442
|
numPoints++;
|
443
|
cacheDirty = true;
|
444
|
}
|
445
|
}
|
446
|
|
447
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
448
|
/**
|
449
|
* Removes all occurrences of Point v from the List of Points to interpolate through.
|
450
|
*
|
451
|
* @param v The Point to remove.
|
452
|
* @return <code>true</code> if we have removed at least one Point.
|
453
|
*/
|
454
|
public synchronized boolean remove(Float3D v)
|
455
|
{
|
456
|
int n = vv.indexOf(v);
|
457
|
boolean found = false;
|
458
|
|
459
|
while( n>=0 )
|
460
|
{
|
461
|
vv.remove(n);
|
462
|
|
463
|
if( vn!=null ) vn.remove(0);
|
464
|
|
465
|
switch(numPoints)
|
466
|
{
|
467
|
case 0:
|
468
|
case 1:
|
469
|
case 2: break;
|
470
|
case 3: vc.removeAllElements();
|
471
|
setUpVectors(0.0f,null);
|
472
|
break;
|
473
|
default:vc.remove(n);
|
474
|
}
|
475
|
|
476
|
numPoints--;
|
477
|
found = true;
|
478
|
n = vv.indexOf(v);
|
479
|
}
|
480
|
|
481
|
if( found )
|
482
|
{
|
483
|
cacheDirty=true;
|
484
|
}
|
485
|
|
486
|
return found;
|
487
|
}
|
488
|
|
489
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
490
|
/**
|
491
|
* Removes a location'th Point from the List of Points we interpolate through.
|
492
|
*
|
493
|
* @param location index of the Point we want to remove.
|
494
|
* @return <code>true</code> if location is valid, i.e. if 0<=location<getNumPoints().
|
495
|
*/
|
496
|
public synchronized boolean remove(int location)
|
497
|
{
|
498
|
if( location>=0 && location<numPoints )
|
499
|
{
|
500
|
vv.removeElementAt(location);
|
501
|
|
502
|
if( vn!=null ) vn.remove(0);
|
503
|
|
504
|
switch(numPoints)
|
505
|
{
|
506
|
case 0:
|
507
|
case 1:
|
508
|
case 2: break;
|
509
|
case 3: vc.removeAllElements();
|
510
|
setUpVectors(0.0f,null);
|
511
|
break;
|
512
|
default:vc.removeElementAt(location);
|
513
|
}
|
514
|
|
515
|
numPoints--;
|
516
|
cacheDirty = true;
|
517
|
return true;
|
518
|
}
|
519
|
|
520
|
return false;
|
521
|
}
|
522
|
|
523
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
524
|
/**
|
525
|
* Removes all Points.
|
526
|
*/
|
527
|
public synchronized void removeAll()
|
528
|
{
|
529
|
numPoints = 0;
|
530
|
vv.removeAllElements();
|
531
|
vc.removeAllElements();
|
532
|
cacheDirty = false;
|
533
|
|
534
|
if( vn!=null ) vn.removeAllElements();
|
535
|
}
|
536
|
|
537
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
538
|
/**
|
539
|
* Writes the results of interpolation between the Points at time 'time' to the passed float buffer.
|
540
|
* <p>
|
541
|
* Since this is a 3-dimensional Interpolator, the resulting interpolated Float3D gets written
|
542
|
* to three locations in the buffer: buffer[offset], buffer[offset+1] and buffer[offset+2].
|
543
|
*
|
544
|
* @param buffer Float buffer we will write the resulting Float3D to.
|
545
|
* @param offset Offset in the buffer where to write the result.
|
546
|
* @param time Time of interpolation. Time=0.0 would return the first Point, Time=0.5 - the last,
|
547
|
* time=1.0 - the first again, and time 0.1 would be 1/5 of the way between the first and the last Points.
|
548
|
*/
|
549
|
public synchronized void interpolate(float[] buffer, int offset, float time)
|
550
|
{
|
551
|
switch(numPoints)
|
552
|
{
|
553
|
case 0: buffer[offset ] = 0.0f;
|
554
|
buffer[offset+1] = 0.0f;
|
555
|
buffer[offset+2] = 0.0f;
|
556
|
break;
|
557
|
case 1: curr = vv.elementAt(0);
|
558
|
buffer[offset ] = curr.x;
|
559
|
buffer[offset+1] = curr.y;
|
560
|
buffer[offset+2] = curr.z;
|
561
|
break;
|
562
|
case 2: curr = vv.elementAt(0);
|
563
|
next = vv.elementAt(1);
|
564
|
|
565
|
if( mMode==MODE_LOOP || mMode==MODE_PATH ) time = (time>0.5f ? 2-2*time : 2*time);
|
566
|
|
567
|
if( vn!=null )
|
568
|
{
|
569
|
time = noise(time,0);
|
570
|
|
571
|
buffer[offset ] = (next.x-curr.x)*time + curr.x + (vec1X*mFactor1 + vec2X*mFactor2);
|
572
|
buffer[offset+1] = (next.y-curr.y)*time + curr.y + (vec1Y*mFactor1 + vec2Y*mFactor2);
|
573
|
buffer[offset+2] = (next.z-curr.z)*time + curr.z + (vec1Z*mFactor1 + vec2Z*mFactor2);
|
574
|
}
|
575
|
else
|
576
|
{
|
577
|
buffer[offset ] = (next.x-curr.x)*time + curr.x;
|
578
|
buffer[offset+1] = (next.y-curr.y)*time + curr.y;
|
579
|
buffer[offset+2] = (next.z-curr.z)*time + curr.z;
|
580
|
}
|
581
|
|
582
|
break;
|
583
|
default:float t = time;
|
584
|
|
585
|
switch(mMode)
|
586
|
{
|
587
|
case MODE_LOOP: time = time*numPoints;
|
588
|
break;
|
589
|
case MODE_PATH: time = (time<=0.5f) ? 2*time*(numPoints-1) : 2*(1-time)*(numPoints-1);
|
590
|
break;
|
591
|
case MODE_JUMP: time = time*(numPoints-1);
|
592
|
break;
|
593
|
}
|
594
|
|
595
|
int vecCurr = (int)time;
|
596
|
time = time-vecCurr;
|
597
|
|
598
|
if( vecCurr>=0 && vecCurr<numPoints )
|
599
|
{
|
600
|
if( cacheDirty ) recomputeCache(); // recompute cache if we have added or remove vectors since last computation
|
601
|
else if( mVecCurr!= vecCurr ) // ...or if we have just passed a vector and the vector we are currently flying to has changed
|
602
|
{
|
603
|
int vecNext;
|
604
|
mVecCurr = vecCurr;
|
605
|
|
606
|
switch(mMode)
|
607
|
{
|
608
|
case MODE_LOOP: vecNext = vecCurr==numPoints-1 ? 0:vecCurr+1;
|
609
|
break;
|
610
|
case MODE_PATH: if( t<0.5f ) vecNext = vecCurr==numPoints-1 ? numPoints-2: vecCurr+1;
|
611
|
else vecNext = vecCurr==0 ? 1 : vecCurr-1;
|
612
|
break;
|
613
|
case MODE_JUMP: vecNext = vecCurr==numPoints-1 ? 1:vecCurr+1;
|
614
|
break;
|
615
|
default : vecNext = 0;
|
616
|
}
|
617
|
|
618
|
next = vv.elementAt(vecNext);
|
619
|
tmp2 = vc.elementAt(vecNext);
|
620
|
|
621
|
if( tmp2.vx!=next.x || tmp2.vy!=next.y || tmp2.vz!=next.z ) recomputeCache();
|
622
|
}
|
623
|
|
624
|
tmp1 = vc.elementAt(vecCurr);
|
625
|
|
626
|
if( vn!=null )
|
627
|
{
|
628
|
time = noise(time,vecCurr);
|
629
|
|
630
|
setUpVectors(time,tmp1);
|
631
|
|
632
|
buffer[offset ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx + (vec1X*mFactor1 + vec2X*mFactor2);
|
633
|
buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy + (vec1Y*mFactor1 + vec2Y*mFactor2);
|
634
|
buffer[offset+2]= ((tmp1.az*time+tmp1.bz)*time+tmp1.cz)*time+tmp1.dz + (vec1Z*mFactor1 + vec2Z*mFactor2);
|
635
|
}
|
636
|
else
|
637
|
{
|
638
|
buffer[offset ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx;
|
639
|
buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy;
|
640
|
buffer[offset+2]= ((tmp1.az*time+tmp1.bz)*time+tmp1.cz)*time+tmp1.dz;
|
641
|
}
|
642
|
|
643
|
break;
|
644
|
}
|
645
|
}
|
646
|
}
|
647
|
|
648
|
}
|
649
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
650
|
//
|