Project

General

Profile

« Previous | Next » 

Revision 3002bef3

Added by Leszek Koltunski over 7 years ago

Move most of the NOISE complications from DynamicND classes to the parent Dynamic class.

View differences:

src/main/java/org/distorted/library/type/Dynamic2D.java
44 44
    float x,y;
45 45
    float vx,vy;
46 46
    }
47
  
48
  private class VectorNoise
49
    {    
50
    float[] nx;
51
    float[] ny;
52
   
53
    VectorNoise()
54
      {
55
      nx = new float[NUM_NOISE]; 
56
      nx[0] = mRnd.nextFloat();
57
      for(int i=1; i<NUM_NOISE; i++) nx[i] = nx[i-1]+mRnd.nextFloat();
58
      float sum = nx[NUM_NOISE-1] + mRnd.nextFloat();
59
      for(int i=0; i<NUM_NOISE; i++) nx[i] /=sum;
60
     
61
      ny = new float[NUM_NOISE];
62
      for(int i=0; i<NUM_NOISE; i++) ny[i] = mRnd.nextFloat()-0.5f;
63
      }
64
    }
65
    
47

  
66 48
  private Vector<VectorCache> vc;
67 49
  private VectorCache tmp1, tmp2;
68 50
   
69 51
  private Vector<Static2D> vv;
70 52
  private Static2D prev, curr, next;
71
 
72
  private Vector<VectorNoise> vn;
73
  private VectorNoise tmpN;
74
  
75
  private float mFactor;
76
 
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78 53

  
79
  synchronized void createNoise()
80
    {
81
    if( vn==null )
82
      {
83
      vn = new Vector<>();
84
      for(int i=0; i<numPoints; i++) vn.add(new VectorNoise());
85
      }
86
    }
87
 
88 54
///////////////////////////////////////////////////////////////////////////////////////////////////
89 55
// no array bounds checking!
90 56
  
......
196 162
    
197 163
    cacheDirty = false;
198 164
    }
199
 
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201 165

  
202
  private float noise(float time,int vecNum)
203
    {
204
    float lower, upper, len;  
205
    float d = time*(NUM_NOISE+1);
206
    int index = (int)d;
207
    if( index>=NUM_NOISE+1 ) index=NUM_NOISE;
208
    tmpN = vn.elementAt(vecNum);
209
   
210
    float x = d-index;
211
    x = x*x*(3-2*x);
212
   
213
    switch(index)
214
      {
215
      case 0        : mFactor = mNoise*tmpN.ny[0]*x;  
216
                      return time + mNoise*(d*tmpN.nx[0]-time);                
217
      case NUM_NOISE: mFactor= mNoise*tmpN.ny[NUM_NOISE-1]*(1-x);
218
                      len = ((float)NUM_NOISE)/(NUM_NOISE+1);
219
                      lower = len + mNoise*(tmpN.nx[NUM_NOISE-1]-len);  
220
                      return (1.0f-lower)*(d-NUM_NOISE) + lower;
221
      default       : float yb = tmpN.ny[index  ];
222
                      float ya = tmpN.ny[index-1];
223
                      mFactor  = mNoise*((yb-ya)*x+ya);
224
   
225
                      len = ((float)index)/(NUM_NOISE+1);
226
                      lower = len + mNoise*(tmpN.nx[index-1]-len);   
227
                      len = ((float)index+1)/(NUM_NOISE+1); 
228
                      upper = len + mNoise*(tmpN.nx[index  ]-len);
229
            
230
                      return (upper-lower)*(d-index) + lower; 
231
      }
232
    }
233
  
234 166
///////////////////////////////////////////////////////////////////////////////////////////////////
235 167
// PUBLIC API 
236 168
///////////////////////////////////////////////////////////////////////////////////////////////////
......
239 171
 */
240 172
  public Dynamic2D()
241 173
    {
242
    vv = new Vector<>();
243
    vc = new Vector<>();
244
    vn = null;
245
    numPoints = 0;
246
    cacheDirty = false;
247
    mMode = MODE_LOOP;
248
    mDuration = 0;
249
    mCount = 0.5f;
250
    mNoise = 0.0f;
174
    this(0,0.5f);
251 175
    }
252 176

  
253 177
///////////////////////////////////////////////////////////////////////////////////////////////////
......
270 194
    mMode = MODE_LOOP;
271 195
    mDuration = duration;
272 196
    mCount = count;
197
    mDimension = 2;
273 198
    }
274 199

  
275 200
///////////////////////////////////////////////////////////////////////////////////////////////////
......
323 248
      {
324 249
      vv.add(v);
325 250
     
326
      if( vn!=null ) vn.add(new VectorNoise());
251
      if( vn!=null ) vn.add(new VectorNoise(2));
327 252
       
328 253
      switch(numPoints)
329 254
        {
......
354 279
      {
355 280
      vv.add(location, v);
356 281
      
357
      if( vn!=null ) vn.add(new VectorNoise());
282
      if( vn!=null ) vn.add(new VectorNoise(2));
358 283
      
359 284
      switch(numPoints)
360 285
        {
......
495 420
                float dx2 = next.x-curr.x;
496 421
                float dy2 = next.y-curr.y;
497 422
   
498
                buffer[offset  ] = dx2*time + curr.x +dy2*mFactor;
499
                buffer[offset+1] = dy2*time + curr.y -dx2*mFactor;
423
                buffer[offset  ] = dx2*time + curr.x +dy2*mFactor[0];
424
                buffer[offset+1] = dy2*time + curr.y -dx2*mFactor[0];
500 425
                }
501 426
              else
502 427
                {
......
554 479
                  float dx2 = (3*tmp1.ax*time+2*tmp1.bx)*time + tmp1.cx;
555 480
                  float dy2 = (3*tmp1.ay*time+2*tmp1.by)*time + tmp1.cy;
556 481
                 
557
                  buffer[offset  ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx +dy2*mFactor;
558
                  buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy -dx2*mFactor;
482
                  buffer[offset  ]= ((tmp1.ax*time+tmp1.bx)*time+tmp1.cx)*time+tmp1.dx +dy2*mFactor[0];
483
                  buffer[offset+1]= ((tmp1.ay*time+tmp1.by)*time+tmp1.cy)*time+tmp1.dy -dx2*mFactor[0];
559 484
                  } 
560 485
                else
561 486
                  {

Also available in: Unified diff