Example #1
0
cocos2d::Vec2 SteeringBehaviors::calculate()
{
    m_vSteeringForce.setPoint(0, 0);

    /**
    * 要知道每一种驱动力的优先级其实是不同的,比如需要首要保证人物之间不能重叠和
    * 撞墙,然后才是arrive和seek
    */
    if (On(WALL_AVOIDANCE) && !accumulateForce(m_vSteeringForce, wallAvoidance()))
    {
        return m_vSteeringForce;
    }

    if (On(SEPARATION) && !accumulateForce(m_vSteeringForce, separation()))
    {
        return m_vSteeringForce;
    }

    // @_@ 后来加入的外部牵引力,一般是不开放的
    if (On(TRACTION) && !accumulateForce(m_vSteeringForce, m_traction))
    {
        return m_vSteeringForce;
    }

    if (On(SEEK) && !accumulateForce(m_vSteeringForce, seek(m_vTarget)))
    {
        return m_vSteeringForce;
    }

    if (On(ARRIVE) && !accumulateForce(m_vSteeringForce, arrive(m_vTarget)))
    {
        return m_vSteeringForce;
    }

    if (On(PURSUIT) && !accumulateForce(m_vSteeringForce, pursuit(m_targetId)))
    {
        return m_vSteeringForce;
    }

    auto tmpFormation       =   m_pOwner->getTeam()->getTeamFormation();
    auto tmpFormationPosId  =   m_pOwner->getMovingEntity().getFormationPosId();
    if (On(KEEP_FORMATION) && !accumulateForce(m_vSteeringForce, keepFormation(tmpFormation, tmpFormationPosId)))
    {
        return m_vSteeringForce;
    }

    return m_vSteeringForce;
}
Example #2
0
void HistogramOnGrid::calculate( const unsigned& current, MultiValue& myvals, std::vector<double>& buffer, std::vector<unsigned>& der_list ) const {
    if( addOneKernelAtATime ) {
        plumed_dbg_assert( myvals.getNumberOfValues()==2 && !wasforced );
        std::vector<double> der( dimension );
        for(unsigned i=0; i<dimension; ++i) der[i]=myvals.getDerivative( 1, i );
        accumulate( getAction()->getPositionInCurrentTaskList(current), myvals.get(0), myvals.get(1), der, buffer );
    } else {
        plumed_dbg_assert( myvals.getNumberOfValues()==dimension+2 );
        std::vector<double> point( dimension );
        double weight=myvals.get(0)*myvals.get( 1+dimension );
        for(unsigned i=0; i<dimension; ++i) point[i]=myvals.get( 1+i );

        // Get the kernel
        unsigned num_neigh;
        std::vector<unsigned> neighbors;
        std::vector<double> der( dimension );
        KernelFunctions* kernel=getKernelAndNeighbors( point, num_neigh, neighbors );
        // If no kernel normalize the vector so von misses distribution is calculated correctly
        if( !kernel ) {
            double norm=0;
            for(unsigned j=0; j<dimension; ++j) norm += point[j]*point[j];
            norm=sqrt( norm );
            for(unsigned j=0; j<dimension; ++j) point[j] = point[j] / norm;
        }

        if( !kernel && getType()=="flat" ) {
            plumed_dbg_assert( num_neigh==1 );
            accumulate( neighbors[0], weight, 1.0, der, buffer );
        } else {
            double totwforce=0.0;
            std::vector<double> intforce( 2*dimension, 0.0 );
            std::vector<Value*> vv( getVectorOfValues() );

            double newval;
            std::vector<double> xx( dimension );
            for(unsigned i=0; i<num_neigh; ++i) {
                unsigned ineigh=neighbors[i];
                if( inactive( ineigh ) ) continue ;
                getGridPointCoordinates( ineigh, xx );
                for(unsigned j=0; j<dimension; ++j) vv[j]->set(xx[j]);
                if( kernel ) {
                    newval = kernel->evaluate( vv, der, true );
                } else {
                    // Evalulate dot product
                    double dot=0;
                    for(unsigned j=0; j<dimension; ++j) {
                        dot+=xx[j]*point[j];
                        der[j]=xx[j];
                    }
                    // Von misses distribution for concentration parameter
                    newval = von_misses_norm*exp( von_misses_concentration*dot );
                    // And final derivatives
                    for(unsigned j=0; j<dimension; ++j) der[j] *= von_misses_concentration*newval;
                }
                accumulate( ineigh, weight, newval, der, buffer );
                if( wasForced() ) {
                    accumulateForce( ineigh, weight, der, intforce );
                    totwforce += myvals.get( 1+dimension )*newval*forces[ineigh];
                }
            }
            if( wasForced() ) {
                unsigned nder = getAction()->getNumberOfDerivatives();
                unsigned gridbuf = getNumberOfBufferPoints()*getNumberOfQuantities();
                for(unsigned j=0; j<dimension; ++j) {
                    for(unsigned k=0; k<myvals.getNumberActive(); ++k) {
                        // Minus sign here as we are taking derivative with respect to position of center of kernel NOT derivative wrt to
                        // grid point
                        unsigned kder=myvals.getActiveIndex(k);
                        buffer[ bufstart + gridbuf + kder ] -= intforce[j]*myvals.getDerivative( j+1, kder );
                    }
                }
                // Accumulate the sum of all the weights
                buffer[ bufstart + gridbuf + nder ] += myvals.get(0);
                // Add the derivatives of the weights into the force -- this is separate loop as weights of all parts are considered together
                for(unsigned k=0; k<myvals.getNumberActive(); ++k) {
                    unsigned kder=myvals.getActiveIndex(k);
                    buffer[ bufstart + gridbuf + kder ] += totwforce*myvals.getDerivative( 0, kder );
                    buffer[ bufstart + gridbuf + nder + 1 + kder ] += myvals.getDerivative( 0, kder );
                }
            }
            delete kernel;
            for(unsigned i=0; i<dimension; ++i) delete vv[i];
        }
    }
}