#include <sys/time.h>
#include <math.h>
-const double ground_friction = 0.1, ground_level = 400;
-const double box_left = 200, box_top = 200, box_bottom = 210;
-const double elasticity = 0.7;
-const double edge_fuzz = 1;
+const double ground_level = 500;
+const double elasticity = 0.8;
typedef struct _xy_pair Point;
typedef struct _xy_pair Vector;
typedef struct _Object Object;
typedef struct _Stick Stick;
typedef struct _String String;
+typedef struct _Spring Spring;
+typedef struct _OffsetSpring OffsetSpring;
typedef struct _Polygon Polygon;
typedef struct _Offset Offset;
typedef struct _Model Model;
};
struct _Offset {
+ int num_objects;
+ Object **objects;
+ int dx, dy;
+};
+
+struct _Spring {
+ Object *a, *b;
+ int length;
+};
+
+struct _OffsetSpring {
Object *a, *b;
int dx, dy;
};
String *strings;
int num_offsets;
Offset *offsets;
+ int num_springs;
+ Spring *springs;
+ int num_offset_springs;
+ OffsetSpring *offset_springs;
int num_polygons;
Polygon *polygons;
double k;
polygon_init_rectangle (&model->polygons[4], 300, 320, 400, 350);
-
model->num_polygons = num_polygons;
}
model->objects[i].position.y = random() % 200 + 20;
model->objects[i].previous_position.x = random() % 200 + 20;
model->objects[i].previous_position.y = random() % 200 + 20;
+ model->objects[i].mass = 1;
if (i + 1 < num_objects) {
model->sticks[i * 2].a = &model->objects[i];
model->objects[i].position.y = 40 + i * stick_length;
model->objects[i].previous_position.x = 200;
model->objects[i].previous_position.y = 40 + i * stick_length;
+ model->objects[i].mass = 1;
if (i + 1 < num_objects) {
model->sticks[i].a = &model->objects[i];
model->num_objects = num_objects;
model->sticks = g_new (Stick, num_sticks);
model->num_sticks = num_sticks;
- model->offsets = g_new (Offset, num_ropes - 1);
- model->num_offsets = num_ropes - 1;
+ model->offsets = g_new (Offset, 1);
+ model->num_offsets = 1;
model_init_polygons (model);
+ model->offsets[0].num_objects = num_ropes;
+ model->offsets[0].objects = g_new (Object *, num_ropes);
+ model->offsets[0].dx = rope_offset;
+ model->offsets[0].dy = 0;
+
for (i = 0; i < num_ropes; i++) {
for (j = 0; j < num_rope_objects; j++) {
x = 200 + i * rope_offset;
model->objects[index].position.y = y;
model->objects[index].previous_position.x = x;
model->objects[index].previous_position.y = y;
+ model->objects[i].mass = 1;
if (j + 1 < num_rope_objects) {
stick_index = i * (num_rope_objects - 1) + j;
}
}
- if (i + 1 < num_ropes) {
- model->offsets[i].a = &model->objects[i * num_rope_objects];
- model->offsets[i].b = &model->objects[(i + 1) * num_rope_objects];
- model->offsets[i].dx = rope_offset;
- model->offsets[i].dy = 0;
- }
+ model->offsets[0].objects[i] = &model->objects[i * num_rope_objects];
}
model->anchor_object = NULL;
static void
model_init_grid (Model *model)
{
- const int num_ropes = 10;
- const int num_rope_objects = 10;
+ const int num_ropes = 4;
+ const int num_rope_objects = 4;
const int num_objects = num_ropes * num_rope_objects;
const int num_strings = num_ropes * (num_rope_objects - 1) +
(num_ropes - 1) * num_rope_objects;
- const int string_length = 10;
- const int rope_offset = 10;
+ const int string_length = 20;
+ const int rope_offset = 20;
double x, y;
int i, j, index, string_index;
model->num_objects = num_objects;
model->strings = g_new (String, num_strings);
model->num_strings = num_strings;
- model->offsets = g_new (Offset, num_ropes - 1);
- model->num_offsets = num_ropes - 1;
+ model->offsets = g_new (Offset, 1);
+ model->num_offsets = 1;
model_init_polygons (model);
+ model->offsets[0].num_objects = num_ropes;
+ model->offsets[0].objects = g_new (Object *, num_ropes);
+ model->offsets[0].dx = rope_offset;
+ model->offsets[0].dy = 0;
+
for (i = 0; i < num_ropes; i++) {
for (j = 0; j < num_rope_objects; j++) {
x = 200 + i * rope_offset;
model->objects[index].position.y = y;
model->objects[index].previous_position.x = x;
model->objects[index].previous_position.y = y;
+ model->objects[index].mass = 1;
if (i + 1 < num_ropes) {
string_index = i * num_rope_objects + j;
}
}
- if (i + 1 < num_ropes) {
- model->offsets[i].a = &model->objects[i * num_rope_objects];
- model->offsets[i].b = &model->objects[(i + 1) * num_rope_objects];
- model->offsets[i].dx = rope_offset;
- model->offsets[i].dy = 0;
- }
+ model->offsets[0].objects[i] = &model->objects[i * num_rope_objects];
}
model->anchor_object = NULL;
}
+static void
+model_init_molecule (Model *model)
+{
+ const int num_objects = 8;
+ const int num_springs = num_objects * 2;
+ const int spring_length = 50;
+ int i;
+
+ memset (model, 0, sizeof *model);
+ model->objects = g_new (Object, num_objects);
+ model->num_objects = num_objects;
+ model->springs = g_new (Spring, num_springs);
+ model->num_springs = num_springs;
+ model->k = 0.2;
+
+ for (i = 0; i < num_objects; i++) {
+ model->objects[i].position.x = 200 + i * 20;
+ model->objects[i].position.y = 200;
+ model->objects[i].previous_position.x = 200 + i * 20;
+ model->objects[i].previous_position.y = 200;
+ model->objects[i].mass = 0;
+ }
+
+ for (i = 0; i < num_objects; i++) {
+ model->springs[i * 2].a = &model->objects[i];
+ model->springs[i * 2].b = &model->objects[(i + 1) % num_objects];
+ model->springs[i * 2].length = spring_length;
+ model->springs[i * 2 + 1].a = &model->objects[i];
+ model->springs[i * 2 + 1].b = &model->objects[(i + 2) % num_objects];
+ model->springs[i * 2 + 1].length = spring_length;
+ }
+}
+
+
+static void
+model_init_wobbly (Model *model)
+{
+ const int width = 6, height = 6;
+ const int num_objects = width * height;
+ const int num_offset_springs = (width - 1) * height + width * (height - 1);
+ const int distance = 30;
+ double x, y;
+ int i, j, object_index, spring_index;
+
+ memset (model, 0, sizeof *model);
+ model->objects = g_new (Object, num_objects);
+ model->num_objects = num_objects;
+ model->offset_springs = g_new (OffsetSpring, num_offset_springs);
+ model->num_offset_springs = num_offset_springs;
+ model->k = 0.6;
+
+ model_init_polygons (model);
+
+ object_index = 0;
+ spring_index = 0;
+ for (i = 0; i < width; i++) {
+ for (j = 0; j < height; j++) {
+ x = 200 + i * distance;
+ y = 40 + j * distance;
+ model->objects[object_index].position.x = x;
+ model->objects[object_index].position.y = y;
+ model->objects[object_index].previous_position.x = x;
+ model->objects[object_index].previous_position.y = y;
+ model->objects[object_index].mass = 0.3;
+
+ if (i + 1 < width) {
+ model->offset_springs[spring_index].a = &model->objects[object_index];
+ model->offset_springs[spring_index].b = &model->objects[object_index + height];
+ model->offset_springs[spring_index].dx = distance;
+ model->offset_springs[spring_index].dy = 0;
+ spring_index++;
+ }
+
+ if (j + 1 < height) {
+ model->offset_springs[spring_index].a = &model->objects[object_index];
+ model->offset_springs[spring_index].b = &model->objects[object_index + 1];
+ model->offset_springs[spring_index].dx = 0;
+ model->offset_springs[spring_index].dy = distance;
+ spring_index++;
+ }
+
+ object_index++;
+ }
+ }
+}
+
+
static void
model_fini (Model *model)
{
model_accumulate_forces (Model *model)
{
int i;
+ double x, y, dx, dy, distance, displacement;
+ Point middle;
+ Vector u;
for (i = 0; i < model->num_objects; i++) {
model->objects[i].force.x = 0;
- model->objects[i].force.y = 3;
+ model->objects[i].force.y = 3 * model->objects[i].mass;
+ }
+
+ for (i = 0; i < model->num_springs; i++) {
+ x = model->springs[i].a->position.x;
+ y = model->springs[i].a->position.y;
+ dx = model->springs[i].b->position.x - x;
+ dy = model->springs[i].b->position.y - y;
+ distance = sqrt (dx * dx + dy * dy);
+ u.x = dx / distance;
+ u.y = dy / distance;
+ displacement = distance - model->springs[i].length;
+ model->springs[i].a->force.x += u.x * model->k * displacement;
+ model->springs[i].a->force.y += u.y * model->k * displacement;
+ model->springs[i].b->force.x -= u.x * model->k * displacement;
+ model->springs[i].b->force.y -= u.y * model->k * displacement;
+ }
+
+ for (i = 0; i < model->num_offset_springs; i++) {
+ middle.x =
+ (model->offset_springs[i].a->position.x +
+ model->offset_springs[i].b->position.x) / 2;
+ middle.y =
+ (model->offset_springs[i].a->position.y +
+ model->offset_springs[i].b->position.y) / 2;
+
+ x = middle.x - model->offset_springs[i].dx / 2;
+ y = middle.y - model->offset_springs[i].dy / 2;
+
+ dx = x - model->offset_springs[i].a->position.x;
+ dy = y - model->offset_springs[i].a->position.y;
+
+ model->offset_springs[i].a->force.x += dx * model->k;
+ model->offset_springs[i].a->force.y += dy * model->k;
+ model->offset_springs[i].b->force.x -= dx * model->k;
+ model->offset_springs[i].b->force.y -= dy * model->k;
}
}
}
}
+static void
+model_constrain_offset (Model *model, Offset *offset)
+{
+ double x, y;
+ int i;
+
+ x = 0;
+ y = 0;
+ for (i = 0; i < offset->num_objects; i++) {
+ x += offset->objects[i]->position.x;
+ y += offset->objects[i]->position.y;
+ }
+
+ x = x / offset->num_objects - offset->dx * (offset->num_objects - 1) / 2;
+ y = y / offset->num_objects - offset->dy * (offset->num_objects - 1) / 2;
+
+ for (i = 0; i < offset->num_objects; i++) {
+ offset->objects[i]->position.x = x + offset->dx * i;
+ offset->objects[i]->position.y = y + offset->dy * i;
+ }
+}
+
static void
model_constrain (Model *model)
{
model->anchor_object->previous_position.y = model->anchor_position.y;
}
- /* Offset constraints. */
- for (i = 0; i < model->num_offsets; i++) {
- x = (model->offsets[i].a->position.x + model->offsets[i].b->position.x) / 2;
- y = (model->offsets[i].a->position.y + model->offsets[i].b->position.y) / 2;
- model->offsets[i].a->position.x = x - model->offsets[i].dx / 2;
- model->offsets[i].a->position.y = y - model->offsets[i].dy / 2;
- model->offsets[i].b->position.x = x + model->offsets[i].dx / 2;
- model->offsets[i].b->position.y = y + model->offsets[i].dy / 2;
- }
-
/* String constraints. */
for (i = 0; i < model->num_strings; i++) {
x = model->strings[i].a->position.x;
model->sticks[i].b->position.y = y + dy * (1 - fraction);
}
+ /* Offset constraints. */
+ for (i = 0; i < model->num_offsets; i++)
+ model_constrain_offset (model, &model->offsets[i]);
+
/* Polygon constraints. */
for (i = 0; i < model->num_polygons; i++)
model_constrain_polygon (model, &model->polygons[i]);
model_accumulate_forces (model);
model_integrate (model, delta_t);
- for (i = 0; i < 100; i++)
+ for (i = 0; i < 50; i++)
model_constrain (model);
model->theta += delta_t;
Model *model,
Color *color)
{
- int i;
+ int i, j;
cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.5);
cairo_new_path (cr);
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
for (i = 0; i < model->num_offsets; i++) {
+ for (j = 0; j < model->offsets[i].num_objects; j++) {
+ cairo_line_to (cr,
+ model->offsets[i].objects[j]->position.x,
+ model->offsets[i].objects[j]->position.y);
+ }
+ cairo_stroke (cr);
+ }
+
+}
+
+static void
+draw_springs (cairo_t *cr,
+ Model *model,
+ Color *color)
+{
+ int i;
+
+ cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
+ cairo_new_path (cr);
+ cairo_set_line_width (cr, 2);
+ cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
+ cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
+
+ for (i = 0; i < model->num_springs; i++) {
cairo_move_to (cr,
- model->offsets[i].a->position.x,
- model->offsets[i].a->position.y);
+ model->springs[i].a->position.x,
+ model->springs[i].a->position.y);
cairo_line_to (cr,
- model->offsets[i].b->position.x,
- model->offsets[i].b->position.y);
+ model->springs[i].b->position.x,
+ model->springs[i].b->position.y);
+ }
+
+ cairo_stroke (cr);
+}
+
+static void
+draw_offset_springs (cairo_t *cr,
+ Model *model,
+ Color *color)
+{
+ int i;
+
+ cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
+ cairo_new_path (cr);
+ cairo_set_line_width (cr, 2);
+ cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
+ cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
+
+ for (i = 0; i < model->num_offset_springs; i++) {
+ cairo_move_to (cr,
+ model->offset_springs[i].a->position.x,
+ model->offset_springs[i].a->position.y);
+ cairo_line_to (cr,
+ model->offset_springs[i].b->position.x,
+ model->offset_springs[i].b->position.y);
}
cairo_stroke (cr);
{
int i;
+ cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4);
for (i = 0; i < model->num_objects; i++) {
+ cairo_arc (cr, model->objects[i].position.x,
+ model->objects[i].position.y,
+ 3, 0, 2*M_PI);
+ cairo_fill (cr);
}
}
static Color green = { 0, 1, 0 };
static Color red = { 1, 0, 0 };
static Color black = { 0, 0, 0 };
-static Color white = { 1, 1, 1 };
typedef struct _Closure Closure;
struct _Closure {
struct timeval start;
};
-static gboolean
-expose_event (GtkWidget *widget,
- GdkEventExpose *event,
- gpointer data)
+static void
+draw_model (GtkWidget *widget, Model *model)
{
- Closure *closure = data;
cairo_t *cr;
cr = gdk_cairo_create (widget->window);
cairo_set_source_rgb (cr, 1, 1, 1);
cairo_paint (cr);
- draw_polygons (cr, closure->model, &blue);
- draw_sticks (cr, closure->model, &black);
- draw_strings (cr, closure->model, &green);
- draw_offsets (cr, closure->model, &blue);
- draw_objects (cr, closure->model, &white);
+ draw_polygons (cr, model, &blue);
+ draw_sticks (cr, model, &black);
+ draw_strings (cr, model, &green);
+ draw_springs (cr, model, &black);
+ draw_offsets (cr, model, &blue);
+ draw_offset_springs (cr, model, &blue);
+ draw_objects (cr, model, &red);
cairo_destroy (cr);
+}
+
+static gboolean
+expose_event (GtkWidget *widget,
+ GdkEventExpose *event,
+ gpointer data)
+{
+ Closure *closure = data;
+
+ draw_model (widget, closure->model);
return TRUE;
}
GdkModifierType state;
gdk_window_get_pointer (event->window, &x, &y, &state);
-
+
closure->model->anchor_position.x = x + 0.5;
closure->model->anchor_position.y = y + 0.5;
{ "Rope", model_init_rope },
{ "Snake", model_init_snake },
{ "Curtain", model_init_curtain },
- { "Grid", model_init_grid }
+ { "Grid", model_init_grid },
+ { "Molecule", model_init_molecule },
+ { "Wobbly", model_init_wobbly }
};
GtkTreeIter iter;
da = gtk_drawing_area_new ();
/* set a minimum size */
- gtk_widget_set_size_request (da, 600, 500);
+ gtk_widget_set_size_request (da, 200, 200);
gtk_container_add (GTK_CONTAINER (frame), da);
closure.model = &model;
closure.frame_count = 0;
gettimeofday (&closure.start, NULL);
- g_timeout_add (50, timeout_callback, &closure);
+ g_timeout_add (40, timeout_callback, &closure);
gtk_main ();
return 0;