- Timestamp:
- May 3, 2011, 9:46:30 PM (14 years ago)
- Location:
- code/trunk/src/modules/objects
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/modules/objects/ForceField.cc
r7801 r8397 23 23 * Aurelian Jaggi 24 24 * Co-authors: 25 * ...25 * Kevin Young 26 26 * 27 27 */ … … 45 45 /*static*/ const std::string ForceField::modeSphere_s = "sphere"; 46 46 /*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; 47 50 48 51 /** … … 58 61 this->setVelocity(100); 59 62 this->setDiameter(500); 63 this->setMassDiameter(0); //! We allow point-masses 60 64 this->setLength(2000); 61 65 this->mode_ = forceFieldMode::tube; … … 82 86 XMLPortParam(ForceField, "velocity", setVelocity, getVelocity, xmlelement, mode).defaultValues(100); 83 87 XMLPortParam(ForceField, "diameter", setDiameter, getDiameter, xmlelement, mode).defaultValues(500); 88 XMLPortParam(ForceField, "massDiameter", setMassDiameter, getMassDiameter, xmlelement, mode).defaultValues(0); 84 89 XMLPortParam(ForceField, "length", setLength , getLength , xmlelement, mode).defaultValues(2000); 85 90 XMLPortParam(ForceField, "mode", setMode, getMode, xmlelement, mode); … … 90 95 registerVariable(this->velocity_, VariableDirection::ToClient); 91 96 registerVariable(this->radius_, VariableDirection::ToClient); 97 registerVariable(this->massRadius_, VariableDirection::ToClient); 92 98 registerVariable(this->halfLength_, VariableDirection::ToClient); 93 99 registerVariable(this->mode_, VariableDirection::ToClient); … … 98 104 @brief 99 105 A method that is called every tick. 100 Implements the behavior of t ehForceField.106 Implements the behavior of the ForceField. 101 107 @param dt 102 108 The amount of time that elapsed since the last tick. … … 165 171 } 166 172 } 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 } 167 198 } 168 199 … … 181 212 else if(mode == ForceField::modeInvertedSphere_s) 182 213 this->mode_ = forceFieldMode::invertedSphere; 214 else if(mode == ForceField::modeNewtonianGravity_s) 215 this->mode_ = forceFieldMode::newtonianGravity; 183 216 else 184 217 { … … 204 237 case forceFieldMode::invertedSphere: 205 238 return ForceField::modeInvertedSphere_s; 239 case forceFieldMode::newtonianGravity: 240 return ForceField::modeNewtonianGravity_s; 206 241 default: 207 242 return ForceField::modeTube_s; -
code/trunk/src/modules/objects/ForceField.h
r7801 r8397 24 24 * Co-authors: 25 25 * Damian 'Mozork' Frick 26 * Kevin Young 26 27 * 27 28 */ … … 55 56 tube, //!< The ForceField has a tube shape. 56 57 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. 58 60 }; 59 61 } … … 106 108 107 109 /** 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. 109 111 @param diam The diameter to be set. 110 112 */ … … 112 114 { this->radius_ = diam/2; } 113 115 /** 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. 115 117 @return Returns the diameter of the ForceField. 116 118 */ 117 119 inline float getDiameter() 118 120 { 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; } 119 135 120 136 /** … … 139 155 static const std::string modeSphere_s; 140 156 static const std::string modeInvertedSphere_s; 157 static const std::string modeNewtonianGravity_s; 141 158 142 159 float velocity_; //!< The velocity of the ForceField. 143 160 float radius_; //!< The radius of the ForceField. 161 float massRadius_; //!< The radius of the stellar body for the Newtonian ForceField. 144 162 float halfLength_; //!< Half of the length of the ForceField. 145 163 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_; 146 169 }; 147 170 }
Note: See TracChangeset
for help on using the changeset viewer.