ropes are doing something
authorJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 1 Jan 2020 07:15:49 +0000 (09:15 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 1 Jan 2020 07:15:49 +0000 (09:15 +0200)
tools/ropesim/src/main.c
tools/ropesim/src/ropesim.c
tools/ropesim/src/ropesim.h

index 2ccd695..6345e45 100644 (file)
@@ -68,7 +68,7 @@ int main(int argc, char **argv)
 #define ROPE_SPRINGS           (ROPE_MASSES - 1)
 #define ROPE_LEN                       1.0f
 #define ROPE_MASSES_MASS       0.1f
-#define ROPE_K                         0.5f
+#define ROPE_K                         80.0f
 
 int init(void)
 {
@@ -180,6 +180,8 @@ void update(long tmsec, float dt)
 
                dbgvec[i] = apt0;
                rope->masses[0].p = apt0;
+
+               rope = rope->next;
        }
 
        rsim_step(&rsim, dt);
@@ -245,10 +247,19 @@ void display(void)
        glPushAttrib(GL_ENABLE_BIT);
        glDisable(GL_LIGHTING);
        glLineWidth(2);
+       glPointSize(5);
 
        rope = rsim.ropes;
        while(rope) {
                glBegin(GL_LINE_STRIP);
+               glColor3f(0.2, 1, 0.2);
+               for(i=0; i<rope->num_masses; i++) {
+                       glVertex3f(rope->masses[i].p.x, rope->masses[i].p.y, rope->masses[i].p.z);
+               }
+               glEnd();
+
+               glBegin(GL_POINTS);
+               glColor3f(1, 0.2, 0.2);
                for(i=0; i<rope->num_masses; i++) {
                        glVertex3f(rope->masses[i].p.x, rope->masses[i].p.y, rope->masses[i].p.z);
                }
index 2c2dcd8..12c70c4 100644 (file)
@@ -8,7 +8,7 @@ int rsim_init(struct rsim_world *rsim)
 {
        rsim->ropes = 0;
        cgm_vcons(&rsim->grav, 0, -9.807, 0);
-       rsim->damping = 0.995;
+       rsim->damping = 0.5;
        return 0;
 }
 
@@ -34,34 +34,59 @@ int rsim_add_rope(struct rsim_world *rsim, struct rsim_rope *rope)
 static void step(struct rsim_world *rsim, struct rsim_rope *rope, float dt)
 {
        int i;
-       cgm_vec3 npos, f, force;
-       float inv_damp = rsim->damping == 0.0f ? 1.0f : 1.0f / rsim->damping;
+       float len, fmag;
+       cgm_vec3 npos, force, dir;
+       float inv_damp = rsim->damping == 0.0f ? 1.0f : (1.0f - rsim->damping);
        struct rsim_mass *mass = rope->masses;
+       struct rsim_spring *spr = rope->springs;
 
        force.x = rsim->grav.x + rsim->extforce.x;
        force.y = rsim->grav.y + rsim->extforce.y;
        force.z = rsim->grav.z + rsim->extforce.z;
 
+       /* accumulate forces from springs */
+       for(i=0; i<rope->num_springs; i++) {
+               dir = spr->mass[1]->p;
+               cgm_vsub(&dir, &spr->mass[0]->p);
+
+               len = cgm_vlength(&dir);
+               if(len != 0.0f) {
+                       float s = 1.0f / len;
+                       dir.x *= s;
+                       dir.y *= s;
+                       dir.z *= s;
+               }
+               fmag = (len - spr->rest_len) * spr->k;
+
+               cgm_vscale(&dir, fmag);
+               cgm_vadd(&spr->mass[0]->f, &dir);
+               cgm_vsub(&spr->mass[1]->f, &dir);
+
+               spr++;
+       }
+
+       /* update masses */
        for(i=0; i<rope->num_masses; i++) {
                if(mass->fixed) {
                        mass++;
                        continue;
                }
 
-               npos = mass->p;
+               /* add constant forces to accumulated mass force */
+               cgm_vadd(&mass->f, &force);
 
+               mass->v.x = (mass->v.x - mass->v.x * inv_damp * dt) + mass->f.x * dt;
+               mass->v.y = (mass->v.y - mass->v.y * inv_damp * dt) + mass->f.y * dt;
+               mass->v.z = (mass->v.z - mass->v.z * inv_damp * dt) + mass->f.z * dt;
+
+               /* zero out the accumulated forces for next iter */
+               mass->f.x = mass->f.y = mass->f.z = 0.0f;
+
+               npos = mass->p;
                npos.x = mass->p.x + mass->v.x * dt;
                npos.y = mass->p.y + mass->v.y * dt;
                npos.z = mass->p.z + mass->v.z * dt;
 
-               f.x = force.x;/* + spring forces */
-               f.y = force.y;
-               f.z = force.z;
-
-               mass->v.x = ((mass->v.x - mass->v.x * inv_damp) + f.x) * dt;
-               mass->v.y = ((mass->v.y - mass->v.y * inv_damp) + f.y) * dt;
-               mass->v.z = ((mass->v.z - mass->v.z * inv_damp) + f.z) * dt;
-
                /* TODO collisions */
 
                mass->p = npos;
index f75e71b..7c56567 100644 (file)
@@ -6,6 +6,7 @@
 struct rsim_mass {
        cgm_vec3 p;
        cgm_vec3 v;
+       cgm_vec3 f;
        float m;
        int fixed;