X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=akamaru.c;h=b26adea7b176450d6c82b528b89d4f2382164313;hb=2620e681c51c7409e42c969fde43aaedcaa3f98d;hp=14d06015b67c1d257d35bbee4aa44ef9c578529d;hpb=d3969b617a9f27962f5bd8b1113763bf7134fc47;p=akamaru diff --git a/akamaru.c b/akamaru.c index 14d0601..b26adea 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,13 +22,12 @@ #include #include #include +#include #include #include -const double ground_friction = 0.1, ground_level = 400; -const double box_left = 200, box_top = 200, box_bottom = 210; +const double ground_level = 500; const double elasticity = 0.7; -const double edge_fuzz = 1; typedef struct _xy_pair Point; typedef struct _xy_pair Vector; @@ -37,6 +38,7 @@ 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; @@ -62,10 +64,18 @@ struct _String { }; struct _Offset { - Object *a, *b; + int num_objects; + Object **objects; int dx, dy; }; +struct _Polygon { + int num_points; + Point *points; + Vector *normals; + int edge; +}; + struct _Model { int num_objects; Object *objects; @@ -75,6 +85,8 @@ struct _Model { String *strings; int num_offsets; Offset *offsets; + int num_polygons; + Polygon *polygons; double k; double friction; @@ -84,6 +96,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) { @@ -91,14 +172,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->strings = NULL; - model->num_strings = 0; - model->offsets = NULL; - model->num_offsets = 0; + model_init_polygons (model); for (i = 0; i < num_objects; i++) { model->objects[i].position.x = random() % 200 + 20; @@ -126,17 +205,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->strings = NULL; - model->num_strings = 0; - model->offsets = NULL; - model->num_offsets = 0; + model_init_polygons (model); for (i = 0; i < num_objects; i++) { model->objects[i].position.x = 200; @@ -166,14 +243,19 @@ 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->strings = NULL; - model->num_strings = 0; - 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++) { @@ -193,12 +275,7 @@ model_init_curtain (Model *model) } } - 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; @@ -217,14 +294,19 @@ model_init_grid (Model *model) 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->sticks = NULL; - model->num_sticks = 0; 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++) { @@ -252,12 +334,7 @@ model_init_grid (Model *model) } } - 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; @@ -267,17 +344,10 @@ 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); - model->strings = NULL; - model->strings = 0; g_free (model->offsets); - model->offsets = NULL; - model->num_offsets = 0; + memset (model, 0, sizeof *model); } static void @@ -304,9 +374,9 @@ 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; @@ -336,62 +406,99 @@ estimate_distance (double dx, double dy, double r) #endif } -static void -model_constrain (Model *model, double step) +static int +polygon_contains_point (Polygon *polygon, Point *point) { - double dx, dy, x, y, distance, fraction; int i; + double dx, dy; - /* Anchor object constraint. */ - if (model->anchor_object != NULL) { - model->anchor_object->position.x = model->anchor_position.x; - model->anchor_object->position.y = model->anchor_position.y; - model->anchor_object->previous_position.x = model->anchor_position.x; - model->anchor_object->previous_position.y = model->anchor_position.y; + 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; } - /* 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; + 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]; } } - /* Ground collision detection constraints. This puts a ground level - * in to make sure the points don't fall off the screen. */ + 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++) { - 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; - } + if (polygon_contains_point (polygon, &model->objects[i].position)) + polygon_reflect_object (polygon, &model->objects[i]); } +} - /* 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; +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) +{ + double dx, dy, x, y, distance, fraction; + int i; + + /* Anchor object constraint. */ + if (model->anchor_object != NULL) { + model->anchor_object->position.x = model->anchor_position.x; + model->anchor_object->position.y = model->anchor_position.y; + model->anchor_object->previous_position.x = model->anchor_position.x; + model->anchor_object->previous_position.y = model->anchor_position.y; } /* String constraints. */ @@ -423,6 +530,14 @@ model_constrain (Model *model, double step) model->sticks[i].b->position.x = x + dx * (1 - fraction); 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]); } static void @@ -433,8 +548,8 @@ model_step (Model *model, double delta_t) model_accumulate_forces (model); model_integrate (model, delta_t); - for (i = 0; i < 100; i++) - model_constrain (model, delta_t); + for (i = 0; i < 500; i++) + model_constrain (model); model->theta += delta_t; } @@ -528,7 +643,7 @@ draw_offsets (cairo_t *cr, 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); @@ -537,37 +652,33 @@ draw_offsets (cairo_t *cr, cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); for (i = 0; i < model->num_offsets; i++) { - cairo_move_to (cr, - model->offsets[i].a->position.x, - model->offsets[i].a->position.y); - cairo_line_to (cr, - model->offsets[i].b->position.x, - model->offsets[i].b->position.y); + 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); } - cairo_stroke (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 @@ -595,12 +706,9 @@ 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); @@ -608,13 +716,23 @@ expose_event (GtkWidget *widget, cairo_set_source_rgb (cr, 1, 1, 1); cairo_paint (cr); - draw_constraints (cr, closure->model, &red); - 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_offsets (cr, model, &blue); + draw_objects (cr, model, &white); cairo_destroy (cr); +} + +static gboolean +expose_event (GtkWidget *widget, + GdkEventExpose *event, + gpointer data) +{ + Closure *closure = data; + + draw_model (widget, closure->model); return TRUE; } @@ -662,7 +780,7 @@ motion_notify_event (GtkWidget *widget, 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; @@ -787,7 +905,7 @@ create_window (Closure *closure) 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); @@ -825,7 +943,6 @@ static gint timeout_callback (gpointer data) { Closure *closure = data; - int i; model_step (closure->model, 1); @@ -877,7 +994,7 @@ main (int argc, char *argv[]) closure.model = &model; closure.frame_count = 0; gettimeofday (&closure.start, NULL); - g_timeout_add (40, timeout_callback, &closure); + g_timeout_add (100, timeout_callback, &closure); gtk_main (); return 0;