4

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Lenny White
  • 406
  • 4
  • 8

1 Answers1

6

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;
datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Thank you so much, @datenwolf! You're a lifesaver! I'm just beginning learning about coding physics. And so I looked through different implementations of spring systems. But none of them limited the friction to the spring force, so I was quite puzzled. – Lenny White Mar 10 '19 at 00:59
  • @LennyWhite: That's less of a programming problem, but more of a math and physics problem. Let's just say, that I'm a trained physicist, so that fell squarely into my domain :) – datenwolf Mar 10 '19 at 01:24
  • oh wow didn't think I would find an actual physicist here! But now finally I have a working spring joint! I also repurposed this to create a hinge joint. I used `k-value` of 30.0f and damping value of 0.9999f. Seems to work great as well! – Lenny White Mar 10 '19 at 01:49
  • Doesn't this still have a sign error? The damping force should oppose the velocity vector, so I'd expect to see a `-` in there (or for damping to be a negative constant). – Adrian McCarthy Mar 10 '19 at 04:42
  • @AdrianMcCarthy: OP applied the negative sign two further down, so all is good. But yes, if we were dumping all the involved forces into an accumulative variable, then you'd have to place the `-` here. – datenwolf Mar 10 '19 at 09:22
  • @LennyWhite: Actually if you want to implement a rigid joint, it's preferable to implement this as a constraint. Essentially you'd project the force onto the surface the attachment point of the joint can move on, apply the projected force (kind of similar to how you calculate movements on an inclined plane in high school physics). **Or** if you want to do it like the pros, you write down the set of coupled equations that make up the Lagrangian of the system and solve those. Hamiltonian would work as well, but using the Lagrangian requires fewer steps to get the actual equations of movement. – datenwolf Mar 10 '19 at 09:27
  • @datenwolf: Hi, I was revisiting this old topic. Could you please help me understand this better? I haven't had any luck researching this subject and honestly I don't even know exactly where to look. Did you mean projecting force onto the surface the _hanging object_ can move on? Like if I apply an impulse on the hanging object, it's constrained to move along certain path? – Lenny White Apr 06 '19 at 20:08