void SPHSystem::compNearForce(Particle& p,Vec2i cellPos){ Vec2i nearPos; for(int m = -1; m <= 1; m++) for(int n = -1; n <= 1; n++){ nearPos.x = cellPos.x + n; nearPos.y = cellPos.y + m; if(nearPos.x<0||nearPos.x>=gridSize.x||nearPos.y<0||nearPos.y>=gridSize.y) continue; list<Particle>& np = *cells[nearPos.x][nearPos.y].pList; if(!np.empty()){ for (list<Particle>::iterator it = np.begin(); it != np.end(); it++){ Vec2f distVec = p.pos - it->pos; float dist2 = distVec.LengthSquared(); if(dist2 < kernel_radius*kernel_radius && dist2 > INF){ float dist = sqrt(dist2); float V = mass / it->dens; float tempForce = V * (p.pres + it->pres) * spiky(dist); p.acc = p.acc - distVec*tempForce/dist; Vec2f relVel; relVel = it->ev-p.ev; tempForce = V * viscosity * visco(dist); p.acc = p.acc + relVel*tempForce; } } } } }
void SPHSystem::compNearDensPressure(Particle& p, Vec2i cellPos){ Vec2i nearPos; p.dens = 0.0f; p.pres = 0.0f; for(int m = -1; m <= 1; m++) for(int n = -1; n <= 1; n++){ nearPos.x = cellPos.x + m; nearPos.y = cellPos.y + n; if(nearPos.x<0||nearPos.x>=gridSize.x||nearPos.y<0||nearPos.y>=gridSize.y) continue; list<Particle>& np = *cells[nearPos.x][nearPos.y].pList; if(np.empty()) continue; for (list<Particle>::iterator it = np.begin(); it != np.end(); it++){ Vec2f distVec = it->pos - p.pos; float dist = distVec.LengthSquared(); //—омнительный код if(dist>=kernel_radius*kernel_radius) continue; p.dens = p.dens + mass * poly6(dist); } } p.pres = (pow(p.dens / restDensity, 7) - 1) * stiffness; }
void SPHSystem::compForce() { Particle *p; Particle *np; Vec2i cellPos; Vec2i nearPos; uint hash; for(uint k=0; k<numParticle; k++) { p = &(particles[k]); p->acc = Vec2f(0.0f, 0.0f); cellPos = calcCellPos(p->pos); for(int i=-1; i<=1; i++) { for(int j=-1; j<=1; j++) { nearPos.x = cellPos.x + i; nearPos.y = cellPos.y + j; hash = calcCellHash(nearPos); if(hash == 0xffffffff) continue; np = cells[hash].head; while(np != NULL) { Vec2f distVec = p->pos - np->pos; float dist2 = distVec.LengthSquared(); if(dist2 < kernel*kernel && dist2 > INF) { float dist = sqrt(dist2); float V = mass / p->dens; float tempForce = V * (p->pres+np->pres) * spiky(dist); p->acc = p->acc - distVec*tempForce/dist; Vec2f relVel; relVel = np->ev-p->ev; tempForce = V * viscosity * visco(dist); p->acc = p->acc + relVel*tempForce; } np = np->next; } } } p->acc = p->acc/p->dens+gravity; } }
void SPHSystem::compDensPressure() { Particle *p; Particle *np; Vec2i cellPos; Vec2i nearPos; uint hash; for(uint k=0; k<numParticle; k++) { p = &(particles[k]); p->dens = 0.0f; p->pres = 0.0f; cellPos = calcCellPos(p->pos); for(int i=-1; i<=1; i++) { for(int j=-1; j<=1; j++) { nearPos.x = cellPos.x + i; nearPos.y = cellPos.y + j; hash = calcCellHash(nearPos); if(hash == 0xffffffff) continue; np = cells[hash].head; while(np != NULL) { Vec2f distVec = np->pos - p->pos; float dist2 = distVec.LengthSquared(); if(dist2<INF || dist2>=kernel*kernel) { np = np->next; continue; } p->dens = p->dens + mass * poly6(dist2); np = np->next; } } } p->dens = p->dens + mass*poly6(0.0f); p->pres = (pow(p->dens / restDensity, 7) - 1) * stiffness; } }