diff options
author | Kristan Høgsberg <krh@redhat.com> | 2006-06-15 20:26:31 -0400 |
---|---|---|
committer | Kristan Høgsberg <krh@redhat.com> | 2006-06-15 20:26:31 -0400 |
commit | c4e05db3f0514642875e7b8812be24e0f27865f0 (patch) | |
tree | 774bb5bfd21bca9c6b2c186e2b22539c332c5067 | |
parent | 0bd80b990015d21fd866cf9c221aa86f855d531a (diff) |
Make the last few global constants part of the model.
-rw-r--r-- | akamaru.c | 19 | ||||
-rw-r--r-- | akamaru.h | 4 | ||||
-rw-r--r-- | dock.c | 12 | ||||
-rw-r--r-- | main.c | 22 |
4 files changed, 43 insertions, 14 deletions
@@ -6,10 +6,11 @@ * * TODO: * - * - 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. + * - Consider a size, or radius attribute for objects. + * - Make a gravitation object - initialized with a vector or just a point. */ #include <glib.h> @@ -20,10 +21,6 @@ #include "akamaru.h" -const double elasticity = 0.7; -const double friction = 8; -const double gravity = 50; - void object_init (Object *object, double x, double y, double mass) { @@ -174,13 +171,13 @@ model_accumulate_forces (Model *model) for (i = 0; i < model->num_objects; i++) { /* Gravity */ model->objects[i].force.x = 0; - model->objects[i].force.y = gravity * model->objects[i].mass; + model->objects[i].force.y = model->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; + model->objects[i].force.x -= v.x * model->friction; + model->objects[i].force.y -= v.y * model->friction; } for (i = 0; i < model->num_springs; i++) { @@ -291,7 +288,7 @@ polygon_contains_point (Polygon *polygon, Point *point) } static void -polygon_reflect_object (Polygon *polygon, Object *object) +polygon_reflect_object (Polygon *polygon, Object *object, double elasticity) { int i, edge; double d, distance; @@ -327,7 +324,7 @@ model_constrain_polygon (Model *model, Polygon *polygon) for (i = 0; i < model->num_objects; i++) { if (polygon_contains_point (polygon, &model->objects[i].position)) - polygon_reflect_object (polygon, &model->objects[i]); + polygon_reflect_object (polygon, &model->objects[i], model->elasticity); } } @@ -435,7 +432,7 @@ model_step (Model *model, double delta_t) model_accumulate_forces (model); model_integrate (model, delta_t); - for (i = 0; i < 20; i++) + for (i = 0; i < model->constrain_iterations; i++) model_constrain (model); model->theta += delta_t; @@ -91,8 +91,12 @@ struct _Model { Anchor *anchors; int num_polygons; Polygon *polygons; + double k; double friction; + double elasticity; + double gravity; + int constrain_iterations; Anchor mouse_anchor; @@ -36,8 +36,8 @@ timeout_callback (gpointer data) closure->model.objects[i + 1].position.y + 0.5); } - for (i = 0; i < 4; i++) - model_step (&closure->model, 0.03); + for (i = 0; i < 6; i++) + model_step (&closure->model, 0.01); return TRUE; } @@ -96,6 +96,12 @@ model_init_dock (Model *model, int num_items, model->num_anchors = 1; model->k = 0.1; + model->elasticity = 0.9; + model->friction = 150; + model->gravity = 50; + model->k = 0.8; + model->constrain_iterations = 8; + model->polygons = g_new (Polygon, 1); model->num_polygons = 1; polygon_init_enclosing_rectangle (&model->polygons[0], @@ -115,7 +121,7 @@ model_init_dock (Model *model, int num_items, for (i = 1; i < num_objects; i++, object++) { object_init (&model->objects[i], - left_edge + (i - 1) * spread, height - 100, 1); + left_edge + (i - 1) * spread, height - 100, 10); spring_init (spring++, &model->objects[0], object, spacing); for (j = 1; j < num_objects - i; j++) { spacer_init (spacer++, object, object + j, spacing); @@ -17,6 +17,16 @@ #include "akamaru.h" static void +model_init_constants (Model *model) +{ + model->elasticity = 0.7; + model->friction = 8; + model->gravity = 50; + model->k = 0.1; + model->constrain_iterations = 20; +} + +static void model_init_polygons (Model *model) { const int num_polygons = 5; @@ -46,6 +56,8 @@ model_init_snake (Model *model) model->num_objects = num_objects; model->sticks = g_new (Stick, num_sticks); model->num_sticks = num_sticks; + + model_init_constants (model); model_init_polygons (model); for (i = 0; i < num_objects; i++) { @@ -78,6 +90,8 @@ model_init_rope (Model *model) model->num_objects = num_objects; model->sticks = g_new (Stick, num_sticks); model->num_sticks = num_sticks; + + model_init_constants (model); model_init_polygons (model); for (i = 0; i < num_objects; i++) { @@ -110,6 +124,8 @@ model_init_curtain (Model *model) model->num_sticks = num_sticks; model->offsets = g_new (Offset, 1); model->num_offsets = 1; + + model_init_constants (model); model_init_polygons (model); model->offsets[0].num_objects = num_ropes; @@ -157,6 +173,8 @@ model_init_grid (Model *model) model->num_strings = num_strings; model->offsets = g_new (Offset, 1); model->num_offsets = 1; + + model_init_constants (model); model_init_polygons (model); model->offsets[0].num_objects = num_ropes; @@ -198,6 +216,8 @@ model_init_molecule (Model *model) model->num_springs = num_springs; model->k = 2; + model_init_constants (model); + for (i = 0; i < num_objects; i++) object_init (&model->objects[i], 200 + i * 20, 200, 0); @@ -232,6 +252,7 @@ model_init_wobbly (Model *model) model->num_offset_springs = num_offset_springs; model->k = 4.5; + model_init_constants (model); model_init_polygons (model); object = model->objects; @@ -277,6 +298,7 @@ model_init_dock (Model *model) model->num_anchors = 1; model->k = 0.1; + model_init_constants (model); model->polygons = g_new (Polygon, 1); model->num_polygons = 1; polygon_init_enclosing_rectangle (&model->polygons[0], 10, 10, 700, 500); |