take mass into account
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 25 Feb 2020 19:00:06 +0000 (21:00 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 25 Feb 2020 19:00:06 +0000 (21:00 +0200)
tools/ropesim/src/main.c
tools/ropesim/src/ropesim.c

index 3c616f6..f300a57 100644 (file)
@@ -64,11 +64,11 @@ int main(int argc, char **argv)
        return 0;
 }
 
-#define ROPE_MASSES                    5
+#define ROPE_MASSES                    10
 #define ROPE_SPRINGS           (ROPE_MASSES - 1)
-#define ROPE_LEN                       0.6f
-#define ROPE_MASSES_MASS       0.1f
-#define ROPE_K                         80.0f
+#define ROPE_LEN                       0.8f
+#define ROPE_MASSES_MASS       0.01f
+#define ROPE_K                         180.0f
 
 int init(void)
 {
@@ -106,6 +106,7 @@ int init(void)
        }
 
        rsim_init(&rsim);
+       rsim.damping = 0.3;
        ropes_tail = 0;
 
        /* anchor points on the inner gimbal */
@@ -243,7 +244,7 @@ void display(void)
        cmesh_draw(mesh_gin);
        glPopMatrix();
 
-       cmesh_draw(mesh_suz);
+       /*cmesh_draw(mesh_suz);*/
 
        glPointSize(7);
        glBegin(GL_POINTS);
index 12c70c4..869d4e9 100644 (file)
@@ -35,15 +35,11 @@ static void step(struct rsim_world *rsim, struct rsim_rope *rope, float dt)
 {
        int i;
        float len, fmag;
-       cgm_vec3 npos, force, dir;
+       cgm_vec3 npos, faccel, 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;
@@ -58,9 +54,13 @@ static void step(struct rsim_world *rsim, struct rsim_rope *rope, float dt)
                }
                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->mass[0]->f.x += dir.x * fmag / spr->mass[0]->m;
+               spr->mass[0]->f.y += dir.y * fmag / spr->mass[0]->m;
+               spr->mass[0]->f.z += dir.z * fmag / spr->mass[0]->m;
+
+               spr->mass[1]->f.x -= dir.x * fmag / spr->mass[1]->m;
+               spr->mass[1]->f.y -= dir.y * fmag / spr->mass[1]->m;
+               spr->mass[1]->f.z -= dir.z * fmag / spr->mass[1]->m;
 
                spr++;
        }
@@ -73,7 +73,11 @@ static void step(struct rsim_world *rsim, struct rsim_rope *rope, float dt)
                }
 
                /* add constant forces to accumulated mass force */
-               cgm_vadd(&mass->f, &force);
+               cgm_vadd(&mass->f, &rsim->grav);
+
+               faccel = rsim->extforce;
+               cgm_vscale(&faccel, 1.0f / mass->m);
+               cgm_vadd(&mass->f, &rsim->extforce);
 
                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;