Hi everyone...
I'm trying to implement Verlet integration into my mass spring system and I'm getting strange results.
Whenever the grid is disturbed it keep's accumulating force and never comes to rest (and depending on the K_coef value sometimes get's unstable and totally breaks).
Here's some code snippets:
void Spring::ComputeForce(float k_coef)
{
float intensity;
float delta;
Vector force_dir;
Vector dv;
float damping = 0;
//get the distance
float dx = nodeB->pos.x-nodeA->pos.x;
float dy = nodeB->pos.y-nodeA->pos.y;
CurrDistance = (float) sqrtf ((dx*dx)+(dy*dy));
force_dir.x = dx;
force_dir.y = dy;
force_dir.x /= CurrDistance;
force_dir.y /= CurrDistance;
delta = CurrDistance-restLength;
//0.5 is the K value
intensity = 0.5*delta;
ComputedForce.x = force_dir.x*(intensity);
ComputedForce.y = force_dir.y*(intensity);
nodeA->force+=ComputedForce;
nodeB->force-=ComputedForce;
}
here's the verlet integration part (this is called for every node in the system):
resultant = 0;
// move nodes away from ship (this causes disturbance as it moves nodes away from each other
Vector temp(node_curr->GetPosition().x - ShipPos.x,
node_curr->GetPosition().y - ShipPos.y,
node_curr->GetPosition().z - 0);
if (sqrtf(temp.x * temp.x + temp.y * temp.y + temp.z * temp.z) < 30 )
{
SpringComputeSingleForce(&force_tmp, &node_curr->pos, &ShipPos, k_coef, 0);
resultant.x -= 5*force_tmp.x;
resultant.y -= 5*force_tmp.y;
}
node_curr->force += resultant;
// Verlet part
temp = node_curr->pos;
node_curr->pos.x += node_curr->pos.x - node_curr->LastPos.x + node_curr->force.x *dt*dt;
node_curr->pos.y += node_curr->pos.y - node_curr->LastPos.y + node_curr->force.y *dt*dt;
node_curr->LastPos = temp;
}
Now, my question is basically what mechanism is supposed to keep forces from gaining force? why doesn't my system behave like it does when i use euler integration, where it goes to back to it's original position with no problems?
Any insight would be much appreacited!
Ehud.