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
130
131
132
133
/*
*  RainFilter.cpp
*
*  Created by Daisuke Nogami on Nov, 05.
*  opyright 2005 Daisuke Nogami [null-null.net]. Some rights reserved.
*/
 
#include "stdafx.h"
#include "RainFilter.h"
 
 
#include "math.h"
#include "time.h"
 
 
////////////////////////////////////////////////////
// RainDrop
////////////////////////////////////////////////////
 
// param
#define GRAVITY 9.80619920
#define DAMPING 0.85
 
RainDrop::RainDrop(){
  init();
}
 
void RainDrop::init(){
  xpos = (float)rand()/RAND_MAX*1024;
  ypos = 768;
  velx = 10-(float)rand()/RAND_MAX*20;
  vely = -1*((float)rand()/RAND_MAX*30+30);
}
 
void RainDrop::move(){
  // move
  velx = DAMPING * velx;
  vely = DAMPING * ( vely - GRAVITY );
  xpos += velx;
  ypos += vely;
 
  // rebound
  if( ypos < 0 || ypos > 768 ){
    init();
  }
  else if( xpos < 0 ){
    xpos = 1024;
  }
  else if( xpos > 1024 ){
    xpos = 0;
  }
}
 
////////////////////////////////////////////////////
// RainFilter
////////////////////////////////////////////////////
 
// rain source power
#define MULT_RAIN_NUM 5
#define HIT_RANGE 30
#define RAIN_SRC_POW_A 1.0
#define RAIN_SRC_POW_B 0.8
 
void RainFilter::sync( float prec_rate ){
  raindrops.clear();
 
  int i;
  int rnum = prec_rate*MULT_RAIN_NUM;
  for( i = 0; i < rnum; i++ ){
    raindrops.push_back( RainDrop() );
  }
}
 
void RainFilter::update(){
  int i;
  RainDrop* rd;
  for( i = 0; i < raindrops.size(); i++ ){
    rd = &raindrops[i];
    rd->move();
  }
}
 
void RainFilter::mappingTo( CellSet* cellset ){
  int x, y, i, idx;
  float lenx, leny, len, nx, ny;
  Cell* tcell;
  RainDrop* rd;
 
  for( i = 0; i < raindrops.size(); i++ ){
 
    rd = &raindrops[i];
 
    for( y = 0; y < cellset->hdiv; y++ ){
      for( x = 0; x < cellset->wdiv; x++ ){
 
        idx = x+y*cellset->wdiv;
 
        // cell A
        tcell = &cellset->cells_a[idx];
        lenx = (tcell->xpos + cellset->xpos) - rd->xpos;
        leny = (tcell->ypos + cellset->ypos) - rd->ypos;
        len = sqrt( lenx*lenx+leny*leny );
        // hit test
        if( len < HIT_RANGE ){
 
          // effect from airstream
          nx = tcell->nextx;
          ny = tcell->nexty;
          rd->velx += nx*0.03; // x axis
          rd->vely += ny*0.03; // y axis
 
          tcell->add( rd->velx*RAIN_SRC_POW_A, rd->vely*RAIN_SRC_POW_A );
        }
 
        // cell B
        tcell = &cellset->cells_b[idx];
        lenx = (tcell->xpos + cellset->xpos) - rd->xpos;
        leny = (tcell->ypos + cellset->ypos) - rd->ypos;
        len = sqrt( lenx*lenx+leny*leny );
        // hit test
        if( len < HIT_RANGE ){
 
          // effect from airstream
          nx = tcell->nextx;
          ny = tcell->nexty;
          rd->velx += nx*0.03; // x axis
          rd->vely += ny*0.03; // y axis
 
          tcell->add( rd->velx*RAIN_SRC_POW_B, rd->vely*RAIN_SRC_POW_B );
        }
      }
    }
  }
}
[トップ] [一覧] [最終更新] [検索] [バックアップ]