示例#1
0
    //----------------------------------------------------------------------------------------------------------------------
    //----------------------------------------------------------------------------------------------------------------------
    ///  Calculates the forces for a particle using leap frog method of integration
    ///  _currentParticle - The particle we are going to calculate the forces for
    ///  _particleIndex - Vector of all our particles if we want to brut force or the neighbour particles for SPH
    ///  _timeStep - the time step between updates
    public void calcForces(particle _currentParticle, List<particle> _particleIndex, float _timeStep)
    {
        float densityCurrent = 0;
        var gravitiy = new Vector3 (0f,-9.8f,0f);
        var pressureGrad = Vector3.zero;
        var viscosity = Vector3.zero;
        var viscosityVector = Vector3.zero;
        var Acceleration = Vector3.zero;
        for(int i = 0; i<_particleIndex.Count;i++){
        // Calculate desity of all particles
            if(_currentParticle!=_particleIndex[i]){
                densityCurrent += _particleIndex[i].getMass()*calcDensityKern(_currentParticle, _particleIndex[i]);
            }
        }
        _currentParticle.setDensity(densityCurrent);
        var pressTemp  = Vector3.zero;
        var visTemp  = Vector3.zero;
        float pressureCurrent, pressureNeighbour, pressure;

        for(int i=0; i<_particleIndex.Count;i++)
        {
            // Calcualate pressure
            pressureCurrent =   m_gasConstant  * (_currentParticle.getDensity() - _currentParticle.getRestDensity());
            pressureNeighbour = m_gasConstant  * (_particleIndex[i].getDensity() - _particleIndex[i].getRestDensity());
            pressure = ((pressureCurrent /(pressureCurrent*pressureCurrent)) + (pressureNeighbour /(pressureNeighbour*pressureNeighbour)))*(_particleIndex[i].getMass());
            pressTemp = calcPressureKern(_currentParticle,_particleIndex[i]);
            //pressTemp*= pressure; //!omfg not an assignment
            pressureGrad +=(pressTemp);
            // Calculate viscosiy vector
            viscosityVector = _particleIndex[i].getVel() -  _currentParticle.getVel();
            viscosityVector *=(_particleIndex[i].getMass());
            viscosityVector /=(_particleIndex[i].getDensity());
            // Calculate viscosiy
            visTemp = calcViscosityKern(_currentParticle,_particleIndex[i]);
            //visTemp = visTemp.cross(viscosityVector);   //!omfg not an assignment
            viscosity +=(visTemp);
        }
        viscosity *=m_viscosityCoefficient;
        //Calcualate external force if the is one
        var Forces = Vector3.zero;
        if(m_externalForceStrenth != 0 && m_externalForceRadius != 0)
        {
            Forces = _currentParticle.getPos() - m_externalForcePos;
            if (Forces.length()<=m_externalForceRadius && Forces.length() > 0)
            {
                //find the direction the force is in
                Forces.Normalize();
                //Forces *=((m_externalForceRadius-Forces.length())/m_externalForceRadius);
                Forces *=(m_externalForceStrenth);
                if(m_externalForcePush == false)
                {

                    Forces =-Forces;
                }
            }
            else
            {
                Forces.set(0f,0f,0f);
            }
        }

        // Calculate our acceleration
        Acceleration = gravitiy - pressureGrad + viscosity + Forces;    //tweak center

        //---------------leap frog integration------------------------
        //Calculate velocity
        var VelHalfBack =   _currentParticle.getVel() -  _currentParticle.getAcc() * _timeStep/2;
        var VelHalfForward = VelHalfBack + Acceleration * _timeStep;
        var Velocity = (VelHalfBack + VelHalfForward) * 0.5f;
        //Calculate our new position
        var Position = _currentParticle.getPos() + VelHalfForward *(_timeStep);

        _currentParticle.setVel(Velocity);
        _currentParticle.setPos(Position);

        //---------------Debuging----------------
        //    Debug.Log("the viscosity is "+"["<<viscosity.m_x+","<<viscosity.m_y+","<<viscosity.m_z+"]");
        //    Debug.Log("the pressure grad is "+"["<<pressureGrad.m_x<<","<<pressureGrad.m_y<<","<<pressureGrad.m_z<<"]");
        //    Debug.Log("the accelleration is "+"["<<Acceleration.m_x<<","<<Acceleration.m_y<<","<<Acceleration.m_z<<"]");
        //    Debug.Log("the velocity is "+"["<<Velocity.m_x+","<<Velocity.m_y<<","<<Velocity.m_z<<"]");
    }