Skip to content

Commit

Permalink
fixed divide by zero in springs and attractors
Browse files Browse the repository at this point in the history
  • Loading branch information
memo committed Jun 13, 2013
1 parent 71d073c commit df0ee5f
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/MSAPhysicsAttraction.h
Expand Up @@ -32,7 +32,7 @@ namespace msa {
T delta = this->_b->getPosition() - this->_a->getPosition();
float deltaLength2 = delta.lengthSquared();

float force = _strength * (this->_b->getMass()) * (this->_a->getMass()) / (deltaLength2 + 0.001); // to avoid divide by zero
float force = deltaLength2 > 0 ? _strength * (this->_b->getMass()) * (this->_a->getMass()) / deltaLength2 : 0;

T deltaForce = delta * force;

Expand Down
2 changes: 1 addition & 1 deletion src/MSAPhysicsParticle.h
Expand Up @@ -117,7 +117,7 @@ namespace msa {
inline ParticleT<T>* ParticleT<T>::setMass(float t) {
if(t==0) t=0.00001f;
_mass = t;
_invMass = 1.0f/t;
_invMass = t > 0 ? 1.0f/t : 0;
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/MSAPhysicsSpring.h
Expand Up @@ -45,7 +45,7 @@ namespace msa {
T delta = this->_b->getPosition() - this->_a->getPosition();
float deltaLength2 = delta.lengthSquared();
float deltaLength = sqrt(deltaLength2); // TODO: fast approximation of square root (1st order Taylor-expansion at a neighborhood of the rest length r (one Newton-Raphson iteration with initial guess r))
float force = _strength * (deltaLength - _restLength) / (deltaLength * (this->_a->getInvMass() + this->_b->getInvMass()));
float force = deltaLength > 0 ? _strength * (deltaLength - _restLength) / (deltaLength * (this->_a->getInvMass() + this->_b->getInvMass())) : 0;


T deltaForce = delta * force;
Expand Down

0 comments on commit df0ee5f

Please sign in to comment.