Skip to main content
gamedev.net
🔒 Locked

Spring damping

Started by daniel_i_l 0 replies 900+ views
Original Post
daniel_i_l
daniel_i_l
I have a 2D spring between two bodies with damping applied. here's the code:

//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;

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.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
mrbig
mrbig
Your friction code seems sorta fishy to me.
Try commenting it out or replacing it with 'f = -k * relV'.
It might as well be your integration algorithm.
'k' is a relative value, 30 might not seem that big, but it could be.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.