Original Post
I have a 2D spring between two bodies with damping applied. here's the code: Is this code right? for some reason whenever i made the damping force big (k=30, and b=5 for example) the springs acted really wierd and sometimes exploded. what could the reason for this be? I'm using euler but for two bodies, one spring and a low k it doesn't seem to be the problem. Thanks.
//get the states of the two bodies
const State state1 = FirstBody->m_MyState;
const State state2 = SecondBody->m_MyState;
//their relative velocity
const Vector2D RelativeVelocity = state1.Velocity - state2.Velocity;
//the positions of the bodies
Vector2D FirstBodyWorldPos = FirstBodyPosition;
Vector2D SecondBodyWorldPos = SecondBodyPosition;
//the distance between the positions
double distance = Vec2DDistance(FirstBodyWorldPos, SecondBodyWorldPos);
//the direction from the second to the first
Vector2D Direction = FirstBodyWorldPos - SecondBodyWorldPos;
//normalize
Direction /= distance;
//calculate the force
double SpringForce = Spring.SpringConstant*(distance - Spring.RestLength);
//the damping force
double DampingForce = Spring.Damping*RelativeVelocity.Dot(Direction);
//the force as a vector
Vector2D Force = (SpringForce + DampingForce)*Direction;
//apply the forces to the bodies
FirstBody->TotalForce -= Force;
SecondBody->TotalForce += Force;