]> git.cworth.org Git - akamaru/blobdiff - akamaru.c
Add new Spacer constraint and a 'dock' model.
[akamaru] / akamaru.c
index 8dc477cc0f2ee4b7c10edb92f2828ef2102a2333..ae01a27223ba5896eefe5ec53fed9b25c33f0849 100644 (file)
--- a/akamaru.c
+++ b/akamaru.c
  *   corrections at the end instead of meaning as it goes.
  */
 
-#include <gtk/gtk.h>
-#include <cairo.h>
-#include <cairo-xlib.h>
-#include <gdk/gdkx.h>
+#include <glib.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/time.h>
+#include <stdarg.h>
 #include <math.h>
 
 #include "akamaru.h"
 
 const double elasticity = 0.7;
+const double friction = 1;
+const double gravity = 20;
+
+void
+object_init (Object *object, double x, double y, double mass)
+{
+  object->position.x = x;
+  object->position.y = y;
+  object->previous_position.x = x;
+  object->previous_position.y = y;
+  object->mass = mass;
+}
+
+void
+spring_init (Spring *spring, Object *a, Object *b, double length)
+{
+  spring->a = a;
+  spring->b = b;
+  spring->length = length;
+}
+
+void
+stick_init (Stick *stick, Object *a, Object *b, double length)
+{
+  stick->a = a;
+  stick->b = b;
+  stick->length = length;
+}
+
+void
+string_init (String *string, Object *a, Object *b, double length)
+{
+  string->a = a;
+  string->b = b;
+  string->length = length;
+}
+
+void
+offset_spring_init (OffsetSpring *spring, Object *a, Object *b,
+                   double dx, double dy)
+{
+  spring->a = a;
+  spring->b = b;
+  spring->dx = dx;
+  spring->dy = dy;
+}
+
+void
+spacer_init (Spacer *spacer, Object *a, Object *b, double length)
+{
+  spacer->a = a;
+  spacer->b = b;
+  spacer->length = length;
+}
 
 void
 polygon_init (Polygon *p, int num_points, ...)
@@ -48,7 +99,7 @@ polygon_init (Polygon *p, int num_points, ...)
   p->normals = g_new (Vector, p->num_points);
   /* Compute outward pointing normals.  p->normals[i] is the normal
    * for the edged between p->points[i] and p->points[i + 1]. */
- for (i = 0; i < p->num_points; i++) {
 for (i = 0; i < p->num_points; i++) {
     j = (i + 1) % p->num_points;
     dx = p->points[j].x - p->points[i].x;
     dy = p->points[j].y - p->points[i].y;
@@ -88,6 +139,7 @@ model_fini (Model *model)
     g_free (model->offsets[i].objects);
   g_free (model->springs);
   g_free (model->offset_springs);
+  g_free (model->spacers);
   for (i = 0; i < model->num_polygons; i++)
     g_free (model->polygons[i].points);
   g_free (model->polygons);
@@ -101,11 +153,18 @@ model_accumulate_forces (Model *model)
   int i;
   double x, y, dx, dy, distance, displacement;
   Point middle;
-  Vector u;
+  Vector u, v;
 
   for (i = 0; i < model->num_objects; i++) {
+    /* Gravity */
     model->objects[i].force.x = 0;
-    model->objects[i].force.y = 3 * model->objects[i].mass;
+    model->objects[i].force.y = gravity * model->objects[i].mass;
+
+    /* Friction */
+    v.x = model->objects[i].position.x - model->objects[i].previous_position.x;
+    v.y = model->objects[i].position.y - model->objects[i].previous_position.y;
+    model->objects[i].force.x -= v.x * friction;
+    model->objects[i].force.y -= v.y * friction;
   }
 
   for (i = 0; i < model->num_springs; i++) {
@@ -142,6 +201,15 @@ model_accumulate_forces (Model *model)
     model->offset_springs[i].b->force.x -= dx * model->k;
     model->offset_springs[i].b->force.y -= dy * model->k;
   }
+
+  for (i = 0; i < model->num_objects; i++) {
+    double f = 
+      model->objects[i].force.x * model->objects[i].force.x +
+      model->objects[i].force.y * model->objects[i].force.y;
+
+    if (f > 100000000)
+      abort();
+  }
 }
 
 static void
@@ -157,9 +225,9 @@ model_integrate (Model *model, double step)
     y = o->position.y;
     
     o->position.x =
-      x + 0.9 * (x - o->previous_position.x) + o->force.x * step * step;
+      x + (x - o->previous_position.x) + o->force.x * step * step;
     o->position.y =
-      y + 0.9 * (y - o->previous_position.y) + o->force.y * step * step;
+      y + (y - o->previous_position.y) + o->force.y * step * step;
 
     o->previous_position.x = x;
     o->previous_position.y = y;
@@ -300,6 +368,22 @@ model_constrain (Model *model)
     model->strings[i].b->position.y = y + dy * (1 - fraction);
   }
 
+  /* Spacer constraints. */
+  for (i = 0; i < model->num_spacers; i++) {
+    x = model->spacers[i].a->position.x;
+    y = model->spacers[i].a->position.y;
+    dx = model->spacers[i].b->position.x - x;
+    dy = model->spacers[i].b->position.y - y;
+    distance = estimate_distance (dx, dy, model->spacers[i].length);
+    if (distance > model->spacers[i].length)
+      continue;
+    fraction = (distance - model->spacers[i].length) / distance / 2;
+    model->spacers[i].a->position.x = x + dx * fraction;
+    model->spacers[i].a->position.y = y + dy * fraction;
+    model->spacers[i].b->position.x = x + dx * (1 - fraction);
+    model->spacers[i].b->position.y = y + dy * (1 - fraction);
+  }
+
   /* Stick constraints. */
   for (i = 0; i < model->num_sticks; i++) {
     x = model->sticks[i].a->position.x;
@@ -330,8 +414,7 @@ model_step (Model *model, double delta_t)
 
   model_accumulate_forces (model);
   model_integrate (model, delta_t);
-
-  for (i = 0; i < 50; i++)
+  for (i = 0; i < 2; i++)
     model_constrain (model);
 
   model->theta += delta_t;