Changeset 7677 for code/trunk/src/modules/objects/ForceField.cc
- Timestamp:
- Nov 26, 2010, 1:25:56 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/modules/objects/ForceField.cc
r7676 r7677 44 44 /*static*/ const std::string ForceField::modeTube_s = "tube"; 45 45 /*static*/ const std::string ForceField::modeSphere_s = "sphere"; 46 /*static*/ const std::string ForceField::modeInvertedSphere_s = "invertedSphere"; 46 47 47 48 /** … … 79 80 XMLPortParam(ForceField, "velocity", setVelocity, getVelocity, xmlelement, mode).defaultValues(100); 80 81 XMLPortParam(ForceField, "diameter", setDiameter, getDiameter, xmlelement, mode).defaultValues(500); 81 XMLPortParam(ForceField, "length" 82 XMLPortParam(ForceField, "length", setLength , getLength , xmlelement, mode).defaultValues(2000); 82 83 XMLPortParam(ForceField, "mode", setMode, getMode, xmlelement, mode); 83 84 } … … 102 103 103 104 // Vector from the center of the force field to the object its acting on. 104 // TODO: This could probably be simplified.105 105 Vector3 distanceVector = it->getWorldPosition() - (this->getWorldPosition() + (this->halfLength_ * direction)); 106 106 107 // The object is outside of the lengthof the ForceField.107 // The object is outside a ball around the center with radius length/2 of the ForceField. 108 108 if(distanceVector.length() > this->halfLength_) 109 109 continue; … … 137 137 } 138 138 } 139 else if(this->mode_ == forceFieldMode::invertedSphere) 140 { 141 // Iterate over all objects that could possibly be affected by the ForceField. 142 for (ObjectList<MobileEntity>::iterator it = ObjectList<MobileEntity>::begin(); it != ObjectList<MobileEntity>::end(); ++it) 143 { 144 Vector3 distanceVector = this->getWorldPosition() - it->getWorldPosition(); 145 float distance = distanceVector.length(); 146 // If the object is within 'radius' distance and no more than 'length' away from the boundary of the sphere. 147 float range = this->radius_ - this->halfLength_*2; 148 if (distance < this->radius_ && distance > range) 149 { 150 distanceVector.normalise(); 151 // 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. 152 it->applyCentralForce((distance-range)/range * this->velocity_ * distanceVector); 153 } 154 } 155 } 139 156 } 140 157 … … 151 168 else if(mode == ForceField::modeSphere_s) 152 169 this->mode_ = forceFieldMode::sphere; 170 else if(mode == ForceField::modeInvertedSphere_s) 171 this->mode_ = forceFieldMode::invertedSphere; 153 172 else 154 173 { … … 172 191 case forceFieldMode::sphere: 173 192 return ForceField::modeSphere_s; 193 case forceFieldMode::invertedSphere: 194 return ForceField::modeInvertedSphere_s; 174 195 default: 175 196 return ForceField::modeTube_s;
Note: See TracChangeset
for help on using the changeset viewer.