fileRainFilter.cpp
  0
  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
/*
*  RainFilter.cpp
*
*  Created by Daisuke Nogami on Sep, 05.
*  opyright 2005 Daisuke Nogami [null-null.net]. Some rights reserved.
*/
 
#include "stdafx.h"
#include "RainFilter.h"
 
// param
#define GRAVITY 9.80619920
#define DAMPING 0.85
 
#define HIT_RANGE 30
 
inline void RainFilter::initRaindrop( int id ){
  xpos[id] = (float)rand()/RAND_MAX*1024;
  ypos[id] = 768;
  velx[id] = 10-(float)rand()/RAND_MAX*20;
  vely[id] = -1*((float)rand()/RAND_MAX*30+30);
}
 
RainFilter::RainFilter(){
  raindrop_num = 0;
 
  g = GRAVITY;
  damp = DAMPING;
}
 
RainFilter::~RainFilter(){
  delete []this->xpos;
  delete []this->ypos;
  delete []this->velx;
  delete []this->vely;
  this->xpos = 0;
  this->ypos = 0;
  this->velx = 0;
  this->vely = 0;
}
 
void RainFilter::sync( float prec_rate ){
  this->raindrop_num = prec_rate*5;
 
  // initialize
  delete []this->xpos;
  delete []this->ypos;
  delete []this->velx;
  delete []this->vely;
  this->xpos = 0;
  this->ypos = 0;
  this->velx = 0;
  this->vely = 0;
 
  xpos = new float[raindrop_num];
  ypos = new float[raindrop_num];
  velx = new float[raindrop_num];
  vely = new float[raindrop_num];
  
  int i;
  for( i = 0; i < raindrop_num; i++ ){
    initRaindrop( i );
  }
}
 
void RainFilter::update(){
  int i;
  for( i = 0; i < raindrop_num; i++ ){
    velx[i] = damp * velx[i];
    xpos[i] += velx[i];
    vely[i] = damp * ( vely[i] - g );
    ypos[i] += vely[i];
    if( ypos[i] < 0 || ypos[i] > 768 ){
      initRaindrop( i );
    }
    else if( xpos[i] < 0 ){
      xpos[i] = 1024;
    }
    else if( xpos[i] > 1024 ){
      xpos[i] = 0;
    }
  }
}
 
void RainFilter::mappingTo( CellSet* cellset ){
  int x, y, i;
  float lenx, leny, len;
 
  // rain source power
  float rainsrc_pow_b = 0.8; // to cell B
 
  for( y = 0; y < cellset->hdiv; y++ ){
    for( x = 0; x < cellset->wdiv; x++ ){
      for( i = 0; i < raindrop_num; i++ ){
 
        // cell A
        lenx = cellset->cells_a[x+y*cellset->wdiv].xpos+cellset->xpos-xpos[i];
        leny = cellset->cells_a[x+y*cellset->wdiv].ypos+cellset->ypos-ypos[i];
        len = sqrt( lenx*lenx+leny*leny );
        // hit test
        if( len < HIT_RANGE ){
 
          // airstream
          float nx = cellset->cells_a[x+y*cellset->wdiv].nextx;
          float ny = cellset->cells_a[x+y*cellset->wdiv].nexty;
          velx[i] += nx*0.02; // x axis
          vely[i] += ny*0.02; // y axis
 
          cellset->cells_a[x+y*cellset->wdiv].add( velx[i], vely[i] );
        }
 
        // cell B
        lenx = cellset->cells_b[x+y*cellset->wdiv].xpos+cellset->xpos-xpos[i];
        leny = cellset->cells_b[x+y*cellset->wdiv].ypos+cellset->ypos-ypos[i];
        len = sqrt( lenx*lenx+leny*leny );
        // hit test
        if( len < HIT_RANGE ){
 
          // airstream
          float nx = cellset->cells_a[x+y*cellset->wdiv].nextx;
          float ny = cellset->cells_a[x+y*cellset->wdiv].nexty;
          velx[i] += nx*0.02; // x axis
          vely[i] += ny*0.02; // y axis
 
          cellset->cells_b[x+y*cellset->wdiv].add( velx[i]*rainsrc_pow_b, vely[i]*rainsrc_pow_b );
        }
      }
    }
  }
}
[トップ] [一覧] [最終更新] [検索] [バックアップ]