I implemented a spring particle system in OpenGL, where a particle B is constrained to A, with given offset distance. Particle B is affected by spring force and gravity. Here is the implementation:
vec3 velocity;
float k = 1.0f;
float damping = 0.1f;
dt = 0.01f;
void ImplementSpring(vec3 &Apos, vec3 &Bpos, float offsetDistance) {
vec3 dir = Apos-Bpos;
vec3 normdir = normalize(dir);
float currentDistance = length(dir);
//Forces
vec3 gravity = vec3(0, -1, 0)*dt;
vec3 spring = normdir*(currentDistance-offsetDistance)*k*dt;
vec3 dampingForce = velocity*damping;
//Calculate velocity
vec3 acceleration = (gravity+spring-dampingForce)/particleMass;
velocity += acceleration;
Bpos += velocity;
}
void main() {
ImplementSpring(vec3(0, 0, 0), vec3(0, -3, 0), 4);
}
Now if I increase damping, the spring gets more stiff, but the gravity does also get affected by damping/friction. So the particle basically falls down in "slow motion", with high damping values.
How can I edit the script, to make the spring more stiff, but keep the gravity unaffected?
Edit: I'm using glm math library for my calculations.
Edit 2: If damping
is changed to high value, like 0.9
, you can see the problem, as the particle will be very slow to get affected by gravity.
To limit the friction to the force created by the spring, project the velocity vector in the normalized direction of the spring, then apply the damping in only that direction, i.e.
vec3 dampingForce = dot(velocity,normdir)*normdir*damping;