Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 3, 2011, 9:46:30 PM (13 years ago)
Author:
youngk
Message:

Added Newtonian Gravitation to the selection of force fields.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/modules/objects/ForceField.cc

    r7801 r8397  
    2323 *      Aurelian Jaggi
    2424 *   Co-authors:
    25  *      ...
     25 *      Kevin Young
    2626 *
    2727 */
     
    4545    /*static*/ const std::string ForceField::modeSphere_s = "sphere";
    4646    /*static*/ const std::string ForceField::modeInvertedSphere_s = "invertedSphere";
     47    /*static*/ const std::string ForceField::modeNewtonianGravity_s = "newtonianGravity";
     48    /*static*/ const float ForceField::gravConstant_ = 6.673e-11;
     49    /*static*/ const float ForceField::attenFactor_ = 1;
    4750
    4851    /**
     
    5861        this->setVelocity(100);
    5962        this->setDiameter(500);
     63        this->setMassDiameter(0);   //! We allow point-masses
    6064        this->setLength(2000);
    6165        this->mode_ = forceFieldMode::tube;
     
    8286        XMLPortParam(ForceField, "velocity", setVelocity, getVelocity, xmlelement, mode).defaultValues(100);
    8387        XMLPortParam(ForceField, "diameter", setDiameter, getDiameter, xmlelement, mode).defaultValues(500);
     88        XMLPortParam(ForceField, "massDiameter", setMassDiameter, getMassDiameter, xmlelement, mode).defaultValues(0);
    8489        XMLPortParam(ForceField, "length", setLength  , getLength  , xmlelement, mode).defaultValues(2000);
    8590        XMLPortParam(ForceField, "mode", setMode, getMode, xmlelement, mode);
     
    9095        registerVariable(this->velocity_, VariableDirection::ToClient);
    9196        registerVariable(this->radius_, VariableDirection::ToClient);
     97        registerVariable(this->massRadius_, VariableDirection::ToClient);
    9298        registerVariable(this->halfLength_, VariableDirection::ToClient);
    9399        registerVariable(this->mode_, VariableDirection::ToClient);
     
    98104    @brief
    99105        A method that is called every tick.
    100         Implements the behavior of teh ForceField.
     106        Implements the behavior of the ForceField.
    101107    @param dt
    102108        The amount of time that elapsed since the last tick.
     
    165171            }
    166172        }
     173        else if(this->mode_ == forceFieldMode::newtonianGravity)
     174        {
     175            // Iterate over all objects that could possibly be affected by the ForceField.
     176            for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it)
     177            {
     178                Vector3 distanceVector = it->getWorldPosition() - this->getWorldPosition();
     179                float distance = distanceVector.length();
     180                // If the object is within 'radius' distance and especially further away than massRadius_
     181                if (distance < this->radius_ && distance > this->massRadius_)
     182                {
     183                    distanceVector.normalise();
     184                    /* Apply a central force that follows the newtownian law of gravity, ie.:
     185                     * F = G * (M*m) / D^2,
     186                     * while M is the mass of the stellar body and m is the mass of the affected object.
     187                     * D is the distance from the center of mass of both bodies
     188                     * and it should be noted that massRadius_ denotes the radius of the stellar body,
     189                     * at which point the force vanishes (you can use this to dictate the size of the body).
     190                     * attenFactor_ weakens the field by a constant factor. The -1 is needed for an attractive force.
     191                     */
     192                   
     193                    // Note: this so called force is actually an acceleration!
     194                    it->applyCentralForce((-1) * (ForceField::attenFactor_ * ForceField::gravConstant_ * this->getMass()) / (distance * distance) * distanceVector);
     195                }
     196            }
     197        }
    167198    }
    168199
     
    181212        else if(mode == ForceField::modeInvertedSphere_s)
    182213            this->mode_ = forceFieldMode::invertedSphere;
     214        else if(mode == ForceField::modeNewtonianGravity_s)
     215            this->mode_ = forceFieldMode::newtonianGravity;
    183216        else
    184217        {
     
    204237            case forceFieldMode::invertedSphere:
    205238                return ForceField::modeInvertedSphere_s;
     239            case forceFieldMode::newtonianGravity:
     240                return ForceField::modeNewtonianGravity_s;
    206241            default:
    207242                return ForceField::modeTube_s;
Note: See TracChangeset for help on using the changeset viewer.