X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=akamaru.c;h=af0a0f19bc66550ad0d94fd8970da51e1fa4c2d0;hb=3d25488c0dcb5aada993d309c2bf3ffef8407425;hp=58d9e5db67840b496b43a225dd47983fab6f415c;hpb=de1df294bc699a797290ca4e84318f8c7315eb3b;p=akamaru diff --git a/akamaru.c b/akamaru.c index 58d9e5d..af0a0f1 100644 --- a/akamaru.c +++ b/akamaru.c @@ -11,8 +11,10 @@ * * TODO: * - * - Add code to add boxes - * - Add circle object + * - Add code to add boxes + * - Add circle object + * - Try out this idea: make constraint solver take mean of all + * corrections at the end instead of meaning as it goes. */ #include @@ -20,6 +22,8 @@ #include #include #include +#include +#include #include const double ground_friction = 0.1, ground_level = 400; @@ -35,6 +39,8 @@ struct _xy_pair { typedef struct _Object Object; typedef struct _Stick Stick; +typedef struct _String String; +typedef struct _Polygon Polygon; typedef struct _Offset Offset; typedef struct _Model Model; @@ -54,18 +60,34 @@ struct _Stick { int length; }; +struct _String { + Object *a, *b; + int length; +}; + struct _Offset { Object *a, *b; int dx, dy; }; +struct _Polygon { + int num_points; + Point *points; + Vector *normals; + int edge; +}; + struct _Model { int num_objects; Object *objects; int num_sticks; Stick *sticks; + int num_strings; + String *strings; int num_offsets; Offset *offsets; + int num_polygons; + Polygon *polygons; double k; double friction; @@ -75,6 +97,75 @@ struct _Model { double theta; }; +static void +polygon_init (Polygon *p, int num_points, ...) +{ + double dx, dy, length; + int i, j; + va_list ap; + + /* Polygons are defined counter-clock-wise in a coordinate system + * with the y-axis pointing down. */ + + va_start (ap, num_points); + p->num_points = num_points; + p->points = g_new (Point, num_points); + + for (i = 0; i < num_points; i++) { + p->points[i].x = va_arg (ap, double); + p->points[i].y = va_arg (ap, double); + } + va_end (ap); + + 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++) { + j = (i + 1) % p->num_points; + dx = p->points[j].x - p->points[i].x; + dy = p->points[j].y - p->points[i].y; + length = sqrt (dx * dx + dy * dy); + p->normals[i].x = -dy / length; + p->normals[i].y = dx / length; + } +} + +static void +polygon_init_diamond (Polygon *polygon, double x, double y) +{ + return polygon_init (polygon, 5, + x, y, + x + 10, y + 40, + x + 90, y + 40, + x + 100, y, + x + 50, y - 20); +} + +static void +polygon_init_rectangle (Polygon *polygon, double x0, double y0, + double x1, double y1) +{ + return polygon_init (polygon, 4, x0, y0, x0, y1, x1, y1, x1, y0); +} + +static void +model_init_polygons (Model *model) +{ + const int num_polygons = 5; + + model->polygons = g_new (Polygon, num_polygons); + polygon_init_diamond (&model->polygons[0], 250, 300); + polygon_init_diamond (&model->polygons[1], 400, 150); + polygon_init_rectangle (&model->polygons[2], -100, 200, 200, 250); + polygon_init_rectangle (&model->polygons[3], -200, ground_level, + 1200, ground_level + 400); + + polygon_init_rectangle (&model->polygons[4], 300, 320, 400, 350); + + + model->num_polygons = num_polygons; +} + static void model_init_snake (Model *model) { @@ -82,11 +173,12 @@ model_init_snake (Model *model) const int num_sticks = num_objects * 2 - 3; int i; + memset (model, 0, sizeof *model); model->objects = g_new (Object, num_objects); model->num_objects = num_objects; model->sticks = g_new (Stick, num_sticks); model->num_sticks = num_sticks; - model->num_offsets = 0; + model_init_polygons (model); for (i = 0; i < num_objects; i++) { model->objects[i].position.x = random() % 200 + 20; @@ -114,15 +206,15 @@ model_init_rope (Model *model) { const int num_objects = 20; const int num_sticks = num_objects - 1; - const int stick_length = 5; + const int stick_length = 10; int i; + memset (model, 0, sizeof *model); model->objects = g_new (Object, num_objects); model->num_objects = num_objects; model->sticks = g_new (Stick, num_sticks); model->num_sticks = num_sticks; - model->offsets = NULL; - model->num_offsets = 0; + model_init_polygons (model); for (i = 0; i < num_objects; i++) { model->objects[i].position.x = 200; @@ -152,12 +244,14 @@ model_init_curtain (Model *model) double x, y; int i, j, index, stick_index; + memset (model, 0, sizeof *model); model->objects = g_new (Object, num_objects); 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_init_polygons (model); for (i = 0; i < num_ropes; i++) { for (j = 0; j < num_rope_objects; j++) { @@ -188,18 +282,73 @@ model_init_curtain (Model *model) 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_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; + double x, y; + int i, j, index, string_index; + + memset (model, 0, sizeof *model); + model->objects = g_new (Object, num_objects); + 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_init_polygons (model); + + for (i = 0; i < num_ropes; i++) { + for (j = 0; j < num_rope_objects; j++) { + x = 200 + i * rope_offset; + y = 40 + j * string_length; + index = i * num_rope_objects + j; + model->objects[index].position.x = x; + model->objects[index].position.y = y; + model->objects[index].previous_position.x = x; + model->objects[index].previous_position.y = y; + + if (i + 1 < num_ropes) { + string_index = i * num_rope_objects + j; + model->strings[string_index].a = &model->objects[index]; + model->strings[string_index].b = &model->objects[index + num_rope_objects]; + model->strings[string_index].length = string_length; + } + + if (j + 1 < num_rope_objects) { + string_index = + (num_ropes - 1) * num_rope_objects + i * (num_rope_objects - 1) + j; + model->strings[string_index].a = &model->objects[index]; + model->strings[string_index].b = &model->objects[index + 1]; + model->strings[string_index].length = string_length; + } + } + + 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->anchor_object = NULL; +} + static void model_fini (Model *model) { g_free (model->objects); - model->objects = NULL; - model->num_objects = 0; g_free (model->sticks); - model->sticks = NULL; - model->sticks = 0; + g_free (model->strings); g_free (model->offsets); - model->offsets = NULL; - model->num_offsets = 0; + memset (model, 0, sizeof *model); } static void @@ -209,7 +358,7 @@ model_accumulate_forces (Model *model) for (i = 0; i < model->num_objects; i++) { model->objects[i].force.x = 0; - model->objects[i].force.y = 0; + model->objects[i].force.y = 3; } } @@ -226,17 +375,99 @@ model_integrate (Model *model, double step) y = o->position.y; o->position.x = - x + (x - o->previous_position.x) + o->force.x * step * step; + x + 0.9 * (x - o->previous_position.x) + o->force.x * step * step; o->position.y = - y + (y - o->previous_position.y) + o->force.y * step * step; + y + 0.9 * (y - o->previous_position.y) + o->force.y * step * step; o->previous_position.x = x; o->previous_position.y = y; } } +/* The square root in the distance computation for the string and + * stick constraints can be aproximated using Newton: + * + * distance = + * (model->sticks[i].length + + * (dx * dx + dy * dy) / model->sticks[i].length) / 2; + * + * This works really well, since the constraints aren't typically + * violated much. Thus, the distance is really close to the stick + * length, which then makes a good initial guess. However, the + * approximation seems to be slower that just calling sqrt()... + */ + +static inline double +estimate_distance (double dx, double dy, double r) +{ +#ifdef APPROXIMATE_SQUARE_ROOTS + return (r + (dx * dx + dy * dy) / r) / 2; +#else + return sqrt (dx * dx + dy * dy); +#endif +} + +static int +polygon_contains_point (Polygon *polygon, Point *point) +{ + int i; + double dx, dy; + + for (i = 0; i < polygon->num_points; i++) { + dx = point->x - polygon->points[i].x; + dy = point->y - polygon->points[i].y; + + if (polygon->normals[i].x * dx + polygon->normals[i].y * dy >= 0) + return FALSE; + } + + return TRUE; +} + +static void +polygon_reflect_object (Polygon *polygon, Object *object) +{ + int i, edge; + double d, distance; + Vector *n; + + distance = -1000; + for (i = 0; i < polygon->num_points; i++) { + d = polygon->normals[i].x * (object->position.x - polygon->points[i].x) + + polygon->normals[i].y * (object->position.y - polygon->points[i].y); + + if (d > distance) { + distance = d; + edge = i; + polygon->edge = i; + n = &polygon->normals[i]; + } + } + + object->position.x -= (1 + elasticity) * distance * n->x; + object->position.y -= (1 + elasticity) * distance * n->y; + + distance = + n->x * (object->previous_position.x - polygon->points[edge].x) + + n->y * (object->previous_position.y - polygon->points[edge].y); + + object->previous_position.x -= (1 + elasticity) * distance * n->x; + object->previous_position.y -= (1 + elasticity) * distance * n->y; +} + +static void +model_constrain_polygon (Model *model, Polygon *polygon) +{ + int i; + + for (i = 0; i < model->num_objects; i++) { + if (polygon_contains_point (polygon, &model->objects[i].position)) + polygon_reflect_object (polygon, &model->objects[i]); + } +} + static void -model_constrain (Model *model, double step) +model_constrain (Model *model) { double dx, dy, x, y, distance, fraction; int i; @@ -249,40 +480,6 @@ model_constrain (Model *model, double step) model->anchor_object->previous_position.y = model->anchor_position.y; } - /* FIXME: this should be "is point inside box" test instead. Figure - * out from previous_position which edge the point has passed - * through and reflect in that. */ - for (i = 0; i < model->num_objects; i++) { - x = model->objects[i].position.x; - y = model->objects[i].position.y; - if (box_top - edge_fuzz <= y && - model->objects[i].previous_position.y <= box_top + edge_fuzz && - x < box_left) { - model->objects[i].position.y = box_top - (y - box_top) * elasticity; - model->objects[i].previous_position.y = - box_top - (model->objects[i].previous_position.y - box_top) * elasticity; - } - } - - /* Ground collision detection constraints. This puts a ground level - * in to make sure the points don't fall off the screen. */ - for (i = 0; i < model->num_objects; i++) { - x = model->objects[i].position.x; - y = model->objects[i].position.y; - - if (model->objects[i].position.y > ground_level) { - model->objects[i].position.y = - ground_level - (model->objects[i].position.y - ground_level) * elasticity; - model->objects[i].previous_position.y = - ground_level - (model->objects[i].previous_position.y - ground_level) * elasticity; - - /* Friction on impact */ - model->objects[i].position.x = - model->objects[i].position.x * (1 - ground_friction) + - model->objects[i].previous_position.x * ground_friction; - } - } - /* Offset constraints. */ for (i = 0; i < model->num_offsets; i++) { x = (model->offsets[i].a->position.x + model->offsets[i].b->position.x) / 2; @@ -293,36 +490,39 @@ model_constrain (Model *model, double step) model->offsets[i].b->position.y = y + model->offsets[i].dy / 2; } -#if 1 + /* String constraints. */ + for (i = 0; i < model->num_strings; i++) { + x = model->strings[i].a->position.x; + y = model->strings[i].a->position.y; + dx = model->strings[i].b->position.x - x; + dy = model->strings[i].b->position.y - y; + distance = estimate_distance (dx, dy, model->strings[i].length); + if (distance < model->strings[i].length) + continue; + fraction = (distance - model->strings[i].length) / distance / 2; + model->strings[i].a->position.x = x + dx * fraction; + model->strings[i].a->position.y = y + dy * fraction; + model->strings[i].b->position.x = x + dx * (1 - fraction); + model->strings[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; y = model->sticks[i].a->position.y; dx = model->sticks[i].b->position.x - x; dy = model->sticks[i].b->position.y - y; - distance = sqrt (dx * dx + dy * dy); + distance = estimate_distance (dx, dy, model->sticks[i].length); fraction = (distance - model->sticks[i].length) / distance / 2; model->sticks[i].a->position.x = x + dx * fraction; model->sticks[i].a->position.y = y + dy * fraction; model->sticks[i].b->position.x = x + dx * (1 - fraction); model->sticks[i].b->position.y = y + dy * (1 - fraction); } -#else - /* Stick constraints, without square roots. */ - squared = stick_length * stick_length; - for (i = 0; i < model->num_objects - 1; i++) { - j = i + 1; - x = model->objects[i].position.x; - y = model->objects[i].position.y; - dx = model->objects[j].position.x - x; - dy = model->objects[j].position.y - y; - fraction = squared / (dx * dx + dy * dy + squared) - 0.5; - model->objects[i].position.x = x + dx * fraction; - model->objects[i].position.y = y + dy * fraction; - model->objects[j].position.x = x + dx * (1 - fraction); - model->objects[j].position.y = y + dy * (1 - fraction); - } -#endif + + /* Polygon constraints. */ + for (i = 0; i < model->num_polygons; i++) + model_constrain_polygon (model, &model->polygons[i]); } static void @@ -333,8 +533,8 @@ model_step (Model *model, double delta_t) model_accumulate_forces (model); model_integrate (model, delta_t); - for (i = 0; i < 20; i++) - model_constrain (model, delta_t); + for (i = 0; i < 100; i++) + model_constrain (model); model->theta += delta_t; } @@ -398,6 +598,31 @@ draw_sticks (cairo_t *cr, cairo_stroke (cr); } +static void +draw_strings (cairo_t *cr, + Model *model, + Color *color) +{ + int i; + + cairo_set_source_rgba (cr, color->red, color->green, color->blue, 1); + cairo_new_path (cr); + cairo_set_line_width (cr, 1); + cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); + cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); + + for (i = 0; i < model->num_strings; i++) { + cairo_move_to (cr, + model->strings[i].a->position.x, + model->strings[i].a->position.y); + cairo_line_to (cr, + model->strings[i].b->position.x, + model->strings[i].b->position.y); + } + + cairo_stroke (cr); +} + static void draw_offsets (cairo_t *cr, Model *model, @@ -424,25 +649,22 @@ draw_offsets (cairo_t *cr, } static void -draw_constraints (cairo_t *cr, - Model *model, - Color *color) +draw_polygons (cairo_t *cr, Model *model, Color *color) { - cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.5); - - cairo_move_to (cr, 0, ground_level); - cairo_line_to (cr, 1500, ground_level); - cairo_line_to (cr, 1500, ground_level + 10); - cairo_line_to (cr, 0, ground_level + 10); - cairo_close_path (cr); + Polygon *p; + int i, j; - cairo_move_to (cr, 0, box_top); - cairo_line_to (cr, box_left, box_top); - cairo_line_to (cr, box_left, box_bottom); - cairo_line_to (cr, 0, box_bottom); - cairo_close_path (cr); + for (i = 0; i < model->num_polygons; i++) { + p = &model->polygons[i]; + cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4); + + for (j = 0; j < p->num_points; j++) + cairo_line_to (cr, p->points[j].x, p->points[j].y); + cairo_close_path (cr); + } cairo_fill (cr); + } static void @@ -455,16 +677,27 @@ draw_objects (cairo_t *cr, Model *model, Color *color) } static Color blue = { 0, 0, 1 }; +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 { + GtkWidget *drawing_area; + GtkWidget *fps_label; + Model *model; + int frame_count; + int i; + struct timeval start; +}; + static gboolean expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer data) { - Model *model = data; + Closure *closure = data; cairo_t *cr; cr = gdk_cairo_create (widget->window); @@ -472,10 +705,11 @@ expose_event (GtkWidget *widget, cairo_set_source_rgb (cr, 1, 1, 1); cairo_paint (cr); - draw_constraints (cr, model, &red); - draw_sticks (cr, model, &black); - draw_offsets (cr, model, &blue); - draw_objects (cr, model, &white); + 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); cairo_destroy (cr); @@ -487,14 +721,15 @@ button_press_event (GtkWidget *widget, GdkEventButton *event, gpointer data) { - Model *model = data; + Closure *closure = data; if (event->button != 1) return TRUE; - model->anchor_position.x = event->x; - model->anchor_position.y = event->y; - model->anchor_object = model_find_nearest (model, event->x, event->y); + closure->model->anchor_position.x = event->x; + closure->model->anchor_position.y = event->y; + closure->model->anchor_object = model_find_nearest (closure->model, + event->x, event->y); return TRUE; } @@ -504,12 +739,12 @@ button_release_event (GtkWidget *widget, GdkEventButton *event, gpointer data) { - Model *model = data; + Closure *closure = data; if ((event->state & GDK_BUTTON1_MASK) == 0) return TRUE; - model->anchor_object = NULL; + closure->model->anchor_object = NULL; return TRUE; } @@ -519,14 +754,14 @@ motion_notify_event (GtkWidget *widget, GdkEventMotion *event, gpointer data) { - Model *model = data; + Closure *closure = data; int x, y; GdkModifierType state; gdk_window_get_pointer (event->window, &x, &y, &state); - model->anchor_position.x = x + 0.5; - model->anchor_position.y = y + 0.5; + closure->model->anchor_position.x = x + 0.5; + closure->model->anchor_position.y = y + 0.5; return TRUE; } @@ -536,7 +771,7 @@ typedef void (*ModelInitFunc) (Model *model); static void model_changed (GtkComboBox *combo, gpointer user_data) { - Model *model = user_data; + Closure *closure = user_data; GtkTreeIter iter; GtkTreeModel *tree_model; ModelInitFunc init; @@ -548,8 +783,8 @@ model_changed (GtkComboBox *combo, gpointer user_data) gtk_tree_model_get (tree_model, &iter, 0, &name, 1, &init, -1); - model_fini (model); - (*init) (model); + model_fini (closure->model); + (*init) (closure->model); } static GtkTreeModel * @@ -561,7 +796,8 @@ create_model_store (void) } models[] = { { "Rope", model_init_rope }, { "Snake", model_init_snake }, - { "Curtain", model_init_curtain } + { "Curtain", model_init_curtain }, + { "Grid", model_init_grid } }; GtkTreeIter iter; @@ -581,7 +817,7 @@ create_model_store (void) } static GtkWidget * -create_model_combo (Model *model) +create_model_combo (Closure *closure) { GtkWidget *hbox; GtkWidget *combo, *label; @@ -607,13 +843,18 @@ create_model_combo (Model *model) gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo); gtk_box_pack_start (GTK_BOX (hbox), combo, FALSE, FALSE, 0); g_signal_connect (combo, "changed", - G_CALLBACK (model_changed), model); + G_CALLBACK (model_changed), closure); + + label = gtk_label_new ("Frames per second: 0"); + gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); + + closure->fps_label = label; return hbox; } -static GtkWidget * -create_window (Model *model) +static void +create_window (Closure *closure) { GtkWidget *window; GtkWidget *frame; @@ -622,7 +863,7 @@ create_window (Model *model) GtkWidget *model_combo; window = gtk_window_new (GTK_WINDOW_TOPLEVEL); - gtk_window_set_title (GTK_WINDOW (window), "Drawing Area"); + gtk_window_set_title (GTK_WINDOW (window), "Akamaru"); g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), &window); @@ -650,16 +891,16 @@ create_window (Model *model) /* Signals used to handle backing pixmap */ g_signal_connect (da, "expose_event", - G_CALLBACK (expose_event), model); + G_CALLBACK (expose_event), closure); /* Event signals */ g_signal_connect (da, "motion_notify_event", - G_CALLBACK (motion_notify_event), model); + G_CALLBACK (motion_notify_event), closure); g_signal_connect (da, "button_press_event", - G_CALLBACK (button_press_event), model); + G_CALLBACK (button_press_event), closure); g_signal_connect (da, "button_release_event", - G_CALLBACK (button_release_event), model); + G_CALLBACK (button_release_event), closure); /* Ask to receive events the drawing area doesn't normally * subscribe to @@ -671,32 +912,48 @@ create_window (Model *model) | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK); - model_combo = create_model_combo (model); + model_combo = create_model_combo (closure); gtk_box_pack_start (GTK_BOX (vbox), model_combo, FALSE, FALSE, 0); - return da; + closure->drawing_area = da; } -typedef struct _Closure Closure; -struct _Closure { - GtkWidget *drawing_area; - Model *model; - int i; -}; - static gint timeout_callback (gpointer data) { Closure *closure = data; - int i; - for (i = 0; i < 3; i++) - model_step (closure->model, 0.5); + model_step (closure->model, 1); closure->i++; if (closure->i == 1) { gtk_widget_queue_draw (closure->drawing_area); closure->i = 0; + closure->frame_count++; + } + + if (closure->frame_count == 200) { + struct timeval end, elapsed; + double total; + char text[50]; + + closure->frame_count = 0; + gettimeofday (&end, NULL); + if (closure->start.tv_usec > end.tv_usec) { + end.tv_usec += 1000000; + end.tv_sec--; + } + + elapsed.tv_usec = end.tv_usec - closure->start.tv_usec; + elapsed.tv_sec = end.tv_sec - closure->start.tv_sec; + + total = elapsed.tv_sec + ((double) elapsed.tv_usec / 1e6); + if (total < 0) { + total = 0; + } + closure->start = end; + snprintf (text, sizeof text, "Frames per second: %.2f", 200 / total); + gtk_label_set_text (GTK_LABEL (closure->fps_label), text); } return TRUE; @@ -710,11 +967,13 @@ main (int argc, char *argv[]) gtk_init (&argc, &argv); model_init_rope (&model); - closure.drawing_area = create_window (&model); + create_window (&closure); closure.i = 0; gtk_widget_show_all (gtk_widget_get_toplevel (closure.drawing_area)); closure.model = &model; - g_timeout_add (100, timeout_callback, &closure); + closure.frame_count = 0; + gettimeofday (&closure.start, NULL); + g_timeout_add (50, timeout_callback, &closure); gtk_main (); return 0;