From ea0fe26f0d55e530cd1920582da27f29d92914db Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kristian=20H=C3=B8gsberg?= Date: Fri, 19 May 2006 22:50:48 -0400 Subject: [PATCH] Different stick lengths, add back rope model. --- akamaru.c | 52 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/akamaru.c b/akamaru.c index b6a69b2..1c97fc5 100644 --- a/akamaru.c +++ b/akamaru.c @@ -11,8 +11,6 @@ * * TODO: * - * - Fix box collision test - * - Add stick objects instead of hardcoding sticks * - Add code to add boxes * - Add circle object */ @@ -21,9 +19,9 @@ #include #include #include +#include #include -const double stick_length = 30; 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; @@ -52,6 +50,7 @@ struct _Object { struct _Stick { Object *a, *b; + int length; }; struct _Model { @@ -69,10 +68,10 @@ struct _Model { }; static void -model_init (Model *model) +model_init_snake (Model *model) { const int num_objects = 20; - const int num_sticks = 40 - 3; + const int num_sticks = num_objects * 2 - 3; int i; model->objects = g_new (Object, num_objects); @@ -81,18 +80,49 @@ model_init (Model *model) model->num_sticks = num_sticks; for (i = 0; i < num_objects; i++) { - model->objects[i].position.x = 200; - model->objects[i].position.y = i * stick_length + 40; - model->objects[i].previous_position.x = 200; - model->objects[i].previous_position.y = i * stick_length + 40; + model->objects[i].position.x = random() % 200 + 20; + 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; if (i + 1 < num_objects) { model->sticks[i * 2].a = &model->objects[i]; model->sticks[i * 2].b = &model->objects[i + 1]; + model->sticks[i * 2].length = random() % 20 + 20; } if (i + 2 < num_objects) { model->sticks[i * 2 + 1].a = &model->objects[i]; model->sticks[i * 2 + 1].b = &model->objects[i + 2]; + model->sticks[i * 2 + 1].length = random() % 20 + 20; + } + } + + model->anchor_object = NULL; +} + +static void +model_init_rope (Model *model) +{ + const int num_objects = 20; + const int num_sticks = num_objects - 1; + const int stick_length = 20; + int i; + + model->objects = g_new (Object, num_objects); + model->num_objects = num_objects; + model->sticks = g_new (Stick, num_sticks); + model->num_sticks = num_sticks; + + for (i = 0; i < num_objects; i++) { + model->objects[i].position.x = 200; + 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; + + if (i + 1 < num_objects) { + model->sticks[i].a = &model->objects[i]; + model->sticks[i].b = &model->objects[i + 1]; + model->sticks[i].length = stick_length; } } @@ -188,7 +218,7 @@ model_constrain (Model *model, double step) dx = model->sticks[i].b->position.x - x; dy = model->sticks[i].b->position.y - y; distance = sqrt (dx * dx + dy * dy); - fraction = (distance - stick_length) / distance / 2; + 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); @@ -561,7 +591,7 @@ main (int argc, char *argv[]) Model model; gtk_init (&argc, &argv); - model_init (&model); + model_init_snake (&model); closure.drawing_area = create_window (&model); closure.i = 0; gtk_widget_show_all (gtk_widget_get_toplevel (closure.drawing_area)); -- 2.43.0