29 #include "BulletDynamics/Dynamics/btRigidBody.h"
33 Muscle2P::Muscle2P(btRigidBody * body1,
38 double dampingCoefficient) :
42 m_dampingCoefficient(dampingCoefficient)
47 m_restLength = pos1.distance(pos2);
48 m_prevLength = m_restLength;
51 btVector3 Muscle2P::calculateAndApplyForce(
double dt)
53 btVector3 force(0.0, 0.0, 0.0);
54 double magnitude = 0.0;
55 const btVector3 dist =
56 anchor2->getWorldPosition() - anchor1->getWorldPosition();
59 const double currLength = dist.length();
60 const btVector3 unitVector = dist / currLength;
61 const double stretch = currLength - m_restLength;
63 magnitude = m_coefK * stretch;
65 const double deltaStretch = currLength - m_prevLength;
66 m_velocity = deltaStretch / dt;
68 m_damping = m_dampingCoefficient * m_velocity;
70 if (abs(magnitude) * 1.0 < abs(m_damping))
73 (m_damping > 0.0 ? magnitude * 1.0 : -magnitude * 1.0);
76 magnitude += m_damping;
79 std::cout <<
"Length: " << dist.length() <<
" rl: " << m_restLength <<std::endl;
82 if (dist.length() > m_restLength)
84 force = unitVector * magnitude;
92 m_prevLength = currLength;
95 btVector3 point1 = this->anchor1->getRelativePosition();
96 this->anchor1->attachedBody->activate();
97 this->anchor1->attachedBody->applyForce(force,point1);
99 btVector3 point2 = this->anchor2->getRelativePosition();
100 this->anchor2->attachedBody->activate();
101 this->anchor2->attachedBody->applyForce(-force,point2);
106 void Muscle2P::setRestLength(
const double newRestLength)
110 assert(newRestLength > 0.0);
112 m_restLength = newRestLength;
115 const double Muscle2P::getRestLength()
const
120 const btScalar Muscle2P::getActualLength()
const
122 const btVector3 dist =
123 this->anchor2->getWorldPosition() - this->anchor1->getWorldPosition();
124 return dist.length();
127 const double Muscle2P::getTension()
const
129 double tension = (getActualLength() - m_restLength) * m_coefK;
130 tension = (tension < 0.0) ? 0.0 : tension;
134 Muscle2P::~Muscle2P()
137 std::cout <<
"Destroying Muscle2P" << std::endl;
145 muscleAnchor::muscleAnchor()
149 muscleAnchor::muscleAnchor(btRigidBody * body,
150 btVector3 worldPos) :
154 attachedRelativeOriginalPosition(attachedBody->getWorldTransform().inverse() *
160 muscleAnchor::~muscleAnchor()
168 btVector3 muscleAnchor::getRelativePosition()
170 const btTransform tr = attachedBody->getWorldTransform();
171 const btVector3 worldPos = tr * attachedRelativeOriginalPosition;
172 return worldPos-this->attachedBody->getCenterOfMassPosition();
175 btVector3 muscleAnchor::getWorldPosition()
177 const btTransform tr = attachedBody->getWorldTransform();
178 return tr * attachedRelativeOriginalPosition;
Definitions of classes Muscle2P and MuscleAnchor.