Alvaro Cassinelli
/
skinGames_II
save loops
Diff: classSpring.cpp
- Revision:
- 0:df6fdd9b99f0
diff -r 000000000000 -r df6fdd9b99f0 classSpring.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/classSpring.cpp Tue Dec 02 04:39:15 2014 +0000 @@ -0,0 +1,45 @@ +#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; +}