Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jan 17, 2016, 10:29:21 PM (8 years ago)
Author:
landauf
Message:

merged branch cpp11_v3 back to trunk

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/modules/objects/ForceField.cc

    r9945 r11071  
    6767        this->setMassDiameter(0);   //! We allow point-masses
    6868        this->setLength(2000);
    69         this->mode_ = forceFieldMode::tube;
     69        this->mode_ = ForceFieldMode::tube;
    7070       
    7171        this->registerVariables();
     
    115115    void ForceField::tick(float dt)
    116116    {
    117         if(this->mode_ == forceFieldMode::tube)
    118         {
    119             // Iterate over all objects that could possibly be affected by the ForceField.
    120             for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it)
     117        if(this->mode_ == ForceFieldMode::tube)
     118        {
     119            // Iterate over all objects that could possibly be affected by the ForceField.
     120            for (MobileEntity* mobileEntity : ObjectList<MobileEntity>())
    121121            {
    122122                // The direction of the orientation of the force field.
     
    125125
    126126                // Vector from the center of the force field to the object its acting on.
    127                 Vector3 distanceVector = it->getWorldPosition() - (this->getWorldPosition() + (this->halfLength_ * direction));
     127                Vector3 distanceVector = mobileEntity->getWorldPosition() - (this->getWorldPosition() + (this->halfLength_ * direction));
    128128
    129129                // The object is outside a ball around the center with radius length/2 of the ForceField.
     
    132132
    133133                // The distance of the object form the orientation vector. (Or rather the smallest distance from the orientation vector)
    134                 float distanceFromDirectionVector = ((it->getWorldPosition() - this->getWorldPosition()).crossProduct(direction)).length();
     134                float distanceFromDirectionVector = ((mobileEntity->getWorldPosition() - this->getWorldPosition()).crossProduct(direction)).length();
    135135
    136136                // If the object in a tube of radius 'radius' around the direction of orientation.
     
    140140                // Apply a force to the object in the direction of the orientation.
    141141                // The force is highest when the object is directly on the direction vector, with a linear decrease, finally reaching zero, when distanceFromDirectionVector = radius.
    142                 it->applyCentralForce((this->radius_ - distanceFromDirectionVector)/this->radius_ * this->velocity_ * direction);
    143             }
    144         }
    145         else if(this->mode_ == forceFieldMode::sphere)
    146         {
    147             // Iterate over all objects that could possibly be affected by the ForceField.
    148             for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it)
    149             {
    150                 Vector3 distanceVector = it->getWorldPosition() - this->getWorldPosition();
     142                mobileEntity->applyCentralForce((this->radius_ - distanceFromDirectionVector)/this->radius_ * this->velocity_ * direction);
     143            }
     144        }
     145        else if(this->mode_ == ForceFieldMode::sphere)
     146        {
     147            // Iterate over all objects that could possibly be affected by the ForceField.
     148            for (MobileEntity* mobileEntity : ObjectList<MobileEntity>())
     149            {
     150                Vector3 distanceVector = mobileEntity->getWorldPosition() - this->getWorldPosition();
    151151                float distance = distanceVector.length();
    152152                // If the object is within 'radius' distance.
     
    155155                    distanceVector.normalise();
    156156                    // Apply a force proportional to the velocity, with highest force at the origin of the sphere, linear decreasing until reaching a distance of 'radius' from the origin, where the force reaches zero.
    157                     it->applyCentralForce((this->radius_ - distance)/this->radius_ * this->velocity_ * distanceVector);
    158                 }
    159             }
    160         }
    161         else if(this->mode_ == forceFieldMode::invertedSphere)
    162         {
    163             // Iterate over all objects that could possibly be affected by the ForceField.
    164             for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it)
    165             {
    166                 Vector3 distanceVector = this->getWorldPosition() - it->getWorldPosition();
     157                    mobileEntity->applyCentralForce((this->radius_ - distance)/this->radius_ * this->velocity_ * distanceVector);
     158                }
     159            }
     160        }
     161        else if(this->mode_ == ForceFieldMode::invertedSphere)
     162        {
     163            // Iterate over all objects that could possibly be affected by the ForceField.
     164            for (MobileEntity* mobileEntity : ObjectList<MobileEntity>())
     165            {
     166                Vector3 distanceVector = this->getWorldPosition() - mobileEntity->getWorldPosition();
    167167                float distance = distanceVector.length();
    168168                // If the object is within 'radius' distance and no more than 'length' away from the boundary of the sphere.
     
    172172                    distanceVector.normalise();
    173173                    // Apply a force proportional to the velocity, with highest force at the boundary of the sphere, linear decreasing until reaching a distance of 'radius-length' from the origin, where the force reaches zero.
    174                     it->applyCentralForce((distance-range)/range * this->velocity_ * distanceVector);
    175                 }
    176             }
    177         }
    178         else if(this->mode_ == forceFieldMode::newtonianGravity)
    179         {
    180             // Iterate over all objects that could possibly be affected by the ForceField.
    181             for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it)
    182             {
    183                 Vector3 distanceVector = it->getWorldPosition() - this->getWorldPosition();
     174                    mobileEntity->applyCentralForce((distance-range)/range * this->velocity_ * distanceVector);
     175                }
     176            }
     177        }
     178        else if(this->mode_ == ForceFieldMode::newtonianGravity)
     179        {
     180            // Iterate over all objects that could possibly be affected by the ForceField.
     181            for (MobileEntity* mobileEntity : ObjectList<MobileEntity>())
     182            {
     183                Vector3 distanceVector = mobileEntity->getWorldPosition() - this->getWorldPosition();
    184184                float distance = distanceVector.length();
    185185                // If the object is within 'radius' distance and especially further away than massRadius_
     
    197197                   
    198198                    // Note: this so called force is actually an acceleration!
    199                     it->applyCentralForce((-1) * (ForceField::attenFactor_ * ForceField::gravConstant_ * this->getMass()) / (distance * distance) * distanceVector);
    200                 }
    201             }
    202         }
    203         else if(this->mode_ == forceFieldMode::homogen)
    204         {
    205             // Iterate over all objects that could possibly be affected by the ForceField.
    206             for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it)
    207             {
    208                 Vector3 distanceVector = it->getWorldPosition() - this->getWorldPosition();
     199                    mobileEntity->applyCentralForce((-1) * (ForceField::attenFactor_ * ForceField::gravConstant_ * this->getMass()) / (distance * distance) * distanceVector);
     200                }
     201            }
     202        }
     203        else if(this->mode_ == ForceFieldMode::homogen)
     204        {
     205            // Iterate over all objects that could possibly be affected by the ForceField.
     206            for (MobileEntity* mobileEntity : ObjectList<MobileEntity>())
     207            {
     208                Vector3 distanceVector = mobileEntity->getWorldPosition() - this->getWorldPosition();
    209209                float distance = distanceVector.length();
    210210                if (distance < this->radius_ && distance > this->massRadius_)
     
    212212                    // Add a Acceleration in forceDirection_.
    213213                    // Vector3(0,0,0) is the direction, where the force should work.
    214                     it->addAcceleration(forceDirection_ , Vector3(0,0,0));
     214                    mobileEntity->addAcceleration(forceDirection_ , Vector3(0,0,0));
    215215                }
    216216            }
     
    227227    {
    228228        if(mode == ForceField::modeTube_s)
    229             this->mode_ = forceFieldMode::tube;
     229            this->mode_ = ForceFieldMode::tube;
    230230        else if(mode == ForceField::modeSphere_s)
    231             this->mode_ = forceFieldMode::sphere;
     231            this->mode_ = ForceFieldMode::sphere;
    232232        else if(mode == ForceField::modeInvertedSphere_s)
    233             this->mode_ = forceFieldMode::invertedSphere;
     233            this->mode_ = ForceFieldMode::invertedSphere;
    234234        else if(mode == ForceField::modeNewtonianGravity_s)
    235             this->mode_ = forceFieldMode::newtonianGravity;
     235            this->mode_ = ForceFieldMode::newtonianGravity;
    236236
    237237        else if(mode == ForceField::modeHomogen_s)
    238             this->mode_ = forceFieldMode::homogen;
     238            this->mode_ = ForceFieldMode::homogen;
    239239
    240240        else
    241241        {
    242242            orxout(internal_warning) << "Wrong mode '" << mode << "' in ForceField. Setting to 'tube'." << endl;
    243             this->mode_ = forceFieldMode::tube;
     243            this->mode_ = ForceFieldMode::tube;
    244244        }
    245245    }
     
    255255        switch(this->mode_)
    256256        {
    257             case forceFieldMode::tube:
     257            case ForceFieldMode::tube:
    258258                return ForceField::modeTube_s;
    259             case forceFieldMode::sphere:
     259            case ForceFieldMode::sphere:
    260260                return ForceField::modeSphere_s;
    261             case forceFieldMode::invertedSphere:
     261            case ForceFieldMode::invertedSphere:
    262262                return ForceField::modeInvertedSphere_s;
    263             case forceFieldMode::newtonianGravity:
     263            case ForceFieldMode::newtonianGravity:
    264264                return ForceField::modeNewtonianGravity_s;
    265265
    266             case forceFieldMode::homogen:
     266            case ForceFieldMode::homogen:
    267267                return ForceField::modeHomogen_s;
    268268
Note: See TracChangeset for help on using the changeset viewer.