Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Added Newtonian Gravitation to the selection of force fields.

Location:
code/trunk/src/modules/objects
Files:
2 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;
  • code/trunk/src/modules/objects/ForceField.h

    r7801 r8397  
    2424 *   Co-authors:
    2525 *      Damian 'Mozork' Frick
     26 *      Kevin Young
    2627 *
    2728 */
     
    5556            tube, //!< The ForceField has a tube shape.
    5657            sphere, //!< The ForceField has a spherical shape.
    57             invertedSphere //!< The ForceField has a spherical shape but "inverted" behavior.
     58            invertedSphere, //!< The ForceField has a spherical shape but "inverted" behavior.
     59            newtonianGravity //!< The ForceField imitates Newtonian gravitation for use in stellar bodies.
    5860        };
    5961    }
     
    106108
    107109            /**
    108             @brief Set the diameter of the ForceField.
     110            @brief Set the diameter of the ForceField. In mode StellarBody this stands for the outer radius.
    109111            @param diam The diameter to be set.
    110112            */
     
    112114                { this->radius_ = diam/2; }
    113115            /**
    114             @brief Get the diameter of the ForceField.
     116            @brief Get the diameter of the ForceField. In mode StellarBody this stands for the outer radius.
    115117            @return Returns the diameter of the ForceField.
    116118            */
    117119            inline float getDiameter()
    118120                { return this->radius_*2; }
     121       
     122            /**
     123            @brief Set the diameter of the stellar body for the Newtonian ForceField.
     124            @param massDiam The diameter of the stellar body.
     125            */
     126            inline void setMassDiameter(float massDiam)
     127                { this->massRadius_ = massDiam/2; }
     128       
     129            /**
     130            @brief Get the diameter of the stellar body for the Newtonian ForceField.
     131            @return float Returns the diameter of the stellar body.
     132            */
     133            inline float getMassDiameter()
     134                { return this->massRadius_*2; }
    119135
    120136            /**
     
    139155            static const std::string modeSphere_s;
    140156            static const std::string modeInvertedSphere_s;
     157            static const std::string modeNewtonianGravity_s;
    141158
    142159            float velocity_; //!< The velocity of the ForceField.
    143160            float radius_; //!< The radius of the ForceField.
     161            float massRadius_; //!< The radius of the stellar body for the Newtonian ForceField.
    144162            float halfLength_; //!< Half of the length of the ForceField.
    145163            int mode_; //!< The mode of the ForceField.
     164           
     165            //! Gravitational constant for Newtonian ForceFields.
     166            static const float gravConstant_;
     167            //! Attenuation factor for Newtonian ForceFields
     168            static const float attenFactor_;
    146169  };
    147170}
Note: See TracChangeset for help on using the changeset viewer.