![](/media/cache/profiles/68a15b5278e4f7c4c056df9d5f1d3b1f.jpg.50x50_q85.jpg)
save loops
classSpring.cpp
- Committer:
- mbedalvaro
- Date:
- 2014-12-02
- Revision:
- 0:df6fdd9b99f0
File content as of revision 0:df6fdd9b99f0:
#include "classSpring.h" #include "myVectorClass.h" //--------------------------------------------------------------------- spring::spring(){ massA = NULL; massB = NULL; } //--------------------------------------------------------------------- void spring::update(){ if ((massA == NULL) || (massB == NULL)){ return; } vector2Df pta = massA->pos; vector2Df ptb = massB->pos; float theirDistance = (pta - ptb).length(); float springForce = (springiness * (distance - theirDistance)); vector2Df frcToAdd = (pta-ptb).normalize() * springForce; massA->addForce(frcToAdd); massB->addForce(-frcToAdd); //massA->totalForce.x+=frcToAdd.x;massA->totalForce.y+=frcToAdd.y; //massB->totalForce.x-=frcToAdd.x;massB->totalForce.y-=frcToAdd.y; } void spring::assymetricUpdate(){ // only second mass suffers a force if ((massA == NULL) || (massB == NULL)){ return; } vector2Df pta = massA->pos; vector2Df ptb = massB->pos; float theirDistance = (pta - ptb).length(); float springForce = (springiness * (distance - theirDistance)); vector2Df frcToAdd = (pta-ptb).normalize() * springForce; //massA->addForce(frcToAdd); massB->addForce(-frcToAdd); //massA->totalForce.x+=frcToAdd.x;massA->totalForce.y+=frcToAdd.y; //massB->totalForce.x-=frcToAdd.x;massB->totalForce.y-=frcToAdd.y; }