BoatWave.cpp
上传用户:henghua
上传日期:2007-11-14
资源大小:7655k
文件大小:5k
- #include "BoatWave.h"
- CBoatWave::CBoatWave(void)
- {
- }
- CBoatWave::~CBoatWave(void)
- {
- }
- // 初始化构造函数,获取参数具体值,尤其是8个角点的空间坐标值
- CBoatWave::CBoatWave(float boatx, float boatz){
- width = w_size;
- height = h_size;
- actualw = float(act_w_size);
- actualh = float(act_h_size);
- centersz = float(center_size);
- corner00 = D3DXVECTOR2(boatx - actualw/2, boatz - actualh/2);
- corner01 = D3DXVECTOR2(boatx + actualw/2, boatz - actualh/2);
- corner10 = D3DXVECTOR2(boatx - actualw/2, boatz + actualh/2);
- corner11 = D3DXVECTOR2(boatx + actualw/2, boatz + actualh/2);
- center00 = D3DXVECTOR2(boatx - centersz/2, boatz - centersz/2);
- center01 = D3DXVECTOR2(boatx + centersz/2, boatz - centersz/2);
- center10 = D3DXVECTOR2(boatx - centersz/2, boatz + centersz/2);
- center11 = D3DXVECTOR2(boatx + centersz/2, boatz + centersz/2);
- int i,j,t;
- for(t=0; t<3; t++)
- for(i=0; i<width; i++)
- for(j=0; j<height; j++)
- {
- waveHeight[t][i][j]=0;
- }
- pre0 = 0;
- pre1 = 1;
- cur = 2;
- c = 8;
- A = 0.8;
- e = 0.96;
- }
- // 在(posx,posz)处对水面产生激励
- void CBoatWave::Stimulate(float posx, float posz, float v)
- {
- int i, j;
- float px =posx ;
- float pz = posz ;
-
- FloatToInt(px, pz, i, j);
- float temp= (5.0f + ((FLOAT)rand()/(FLOAT)RAND_MAX ) ) * v /5; // 激励的强弱取决于船的行进速度
- for(int u=-3;u<=3;u++)
- for(int v=-3;v<=3;v++)
- waveHeight[cur][i+u][j+v] = temp;
- }
- // 根据二维波的偏微分方程模拟水面波的运动
- void CBoatWave::MakeWave()
- {
- int temp;
- temp = cur, cur = pre0, pre0 = pre1, pre1 =temp;
- float h11, h01, h21, h10, h12;
- float acc;
- int i,j;
- for(i=0; i<width; i++)
- for(j=0; j<height; j++)
- {
- if(i-1 < 0)
- h01 = 0;
- else
- h01 = waveHeight[pre1][i-1][j];
- if(i+1 >= width)
- h21 = 0;
- else
- h21 = waveHeight[pre1][i+1][j];
- if(j-1 < 0)
- h10 = 0;
- else
- h10 = waveHeight[pre1][i][j-1];
- if(j+1 >= height)
- h12 = 0;
- else
- h12 = waveHeight[pre1][i][j+1];
- h11 = waveHeight[pre1][i][j];
- acc = c * (h01 + h21 + h10 + h12 - 4 * h11);
- waveHeight[cur][i][j] = (1+e) * h11 - e * waveHeight[pre0][i][j] + 0.5 * acc * dt * dt;
- }
- }
- // 由Float型转换为int型,以方便数组采样
- void CBoatWave::FloatToInt(float &fx, float &fy, int &ix, int &iy)
- {
- ix = int( (fx-corner00.x)/actualw * width ) & (width-1);
- iy = int( (fy-corner00.y)/actualh * height ) & (height-1);
- }
- // 整体平移函数:根据船的位置更新corner00,01,10,11和center00,01,10,11以及waveHeight[][][],使船始终处于尾迹场的中心区域内
- void CBoatWave::GridTranslate(float boatx, float boatz)
- {
- int i,j,k;
- int dd = w_size/4;
- float tt = (float)center_size/2.0;
- if(boatx > center11.x)
- {
- for(i=0; i<width; i++)
- for(j=0; j<height; j++)
- {
- if(i+dd<width)
- {
- waveHeight[0][i][j]=waveHeight[0][i+dd][j];
- waveHeight[1][i][j]=waveHeight[1][i+dd][j];
- waveHeight[2][i][j]=waveHeight[2][i+dd][j];
- }
- else
- {
- waveHeight[0][i][j]=0;
- waveHeight[1][i][j]=0;
- waveHeight[2][i][j]=0;
- }
- }
- corner00.x += tt, corner01.x += tt, corner10.x += tt, corner11.x += tt;
- center00.x += tt, center01.x += tt, center10.x += tt, center11.x += tt;
- }
- else
- {
- if(boatx < center00.x)
- {
- for(i=width-1; i>=0; i--)
- for(j=0; j<height; j++)
- {
- if(i-dd>=0)
- {
- waveHeight[0][i][j]=waveHeight[0][i-dd][j];
- waveHeight[1][i][j]=waveHeight[1][i-dd][j];
- waveHeight[2][i][j]=waveHeight[2][i-dd][j];
- }
- else
- {
- waveHeight[0][i][j]=0;
- waveHeight[1][i][j]=0;
- waveHeight[2][i][j]=0;
- }
- }
- corner00.x -= tt, corner01.x -= tt, corner10.x -= tt, corner11.x -= tt;
- center00.x -= tt, center01.x -= tt, center10.x -= tt, center11.x -= tt;
- }
- else
- {
- if(boatz > center11.y)
- {
- for(i=0; i<width; i++)
- for(j=0; j<height; j++)
- {
- if(j+dd<width)
- {
- waveHeight[0][i][j]=waveHeight[0][i][j+dd];
- waveHeight[1][i][j]=waveHeight[1][i][j+dd];
- waveHeight[2][i][j]=waveHeight[2][i][j+dd];
- }
- else
- {
- waveHeight[0][i][j]=0;
- waveHeight[1][i][j]=0;
- waveHeight[2][i][j]=0;
- }
- }
- corner00.y += tt, corner01.y += tt, corner10.y += tt, corner11.y += tt;
- center00.y += tt, center01.y += tt, center10.y += tt, center11.y += tt;
- }
- else
- {
- if(boatz < center00.y)
- {
- for(i=0; i<width; i++)
- for(j=height-1; j>=0; j--)
- {
- if(j-dd>=0)
- {
- waveHeight[0][i][j]=waveHeight[0][i][j-dd];
- waveHeight[1][i][j]=waveHeight[1][i][j-dd];
- waveHeight[2][i][j]=waveHeight[2][i][j-dd];
- }
- else
- {
- waveHeight[0][i][j]=0;
- waveHeight[1][i][j]=0;
- waveHeight[2][i][j]=0;
- }
- }
- corner00.y -= tt, corner01.y -= tt, corner10.y -= tt, corner11.y -= tt;
- center00.y -= tt, center01.y -= tt, center10.y -= tt, center11.y -= tt;
- }
- }
- }
- }
- }
- // 获取高度
- float CBoatWave::GetHeight(float x, float y)
- {
- int ix,iy;
- this->FloatToInt(x, y, ix, iy);
- if(ix<0 || ix>=width || iy<0 || iy>=height)
- return 0.0f;
- else
- return waveHeight[cur][ix][iy];
- }
- // 调用此函数每桢更新尾迹场的数据
- void CBoatWave::Update(float x, float z, float v)
- {
-
- Stimulate(x, z, v);
- MakeWave();
- GridTranslate(x, z);
- }