Changeset 8397 for code/trunk/src/modules/objects/ForceField.cc
- Timestamp:
- May 3, 2011, 9:46:30 PM (13 years ago)
- File:
-
- 1 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;
Note: See TracChangeset
for help on using the changeset viewer.