1 /* -*- mode: c; c-basic-offset: 2 -*-
4 * http://en.wikipedia.org/wiki/Verlet_integration
5 * http://www.teknikus.dk/tj/gdc2001.htm
9 * - Add code to add boxes
11 * - Try out this idea: make constraint solver take mean of all
12 * corrections at the end instead of meaning as it goes.
23 const double elasticity = 0.5;
24 const double friction = 1;
25 const double gravity = 50;
28 object_init (Object *object, double x, double y, double mass)
30 object->position.x = x;
31 object->position.y = y;
32 object->previous_position.x = x;
33 object->previous_position.y = y;
38 spring_init (Spring *spring, Object *a, Object *b, double length)
42 spring->length = length;
46 stick_init (Stick *stick, Object *a, Object *b, double length)
50 stick->length = length;
54 string_init (String *string, Object *a, Object *b, double length)
58 string->length = length;
62 offset_spring_init (OffsetSpring *spring, Object *a, Object *b,
72 spacer_init (Spacer *spacer, Object *a, Object *b, double length)
76 spacer->length = length;
80 anchor_init (Anchor *anchor, Object *object, double x, double y)
82 anchor->object = object;
88 polygon_init (Polygon *p, int num_points, ...)
90 double dx, dy, length;
94 /* Polygons are defined counter-clock-wise in a coordinate system
95 * with the y-axis pointing down. */
97 va_start (ap, num_points);
98 p->num_points = num_points;
99 p->points = g_new (Point, num_points);
101 for (i = 0; i < num_points; i++) {
102 p->points[i].x = va_arg (ap, double);
103 p->points[i].y = va_arg (ap, double);
107 p->normals = g_new (Vector, p->num_points);
108 /* Compute outward pointing normals. p->normals[i] is the normal
109 * for the edged between p->points[i] and p->points[i + 1]. */
110 for (i = 0; i < p->num_points; i++) {
111 j = (i + 1) % p->num_points;
112 dx = p->points[j].x - p->points[i].x;
113 dy = p->points[j].y - p->points[i].y;
114 length = sqrt (dx * dx + dy * dy);
115 p->normals[i].x = -dy / length;
116 p->normals[i].y = dx / length;
121 polygon_init_diamond (Polygon *polygon, double x, double y)
123 return polygon_init (polygon, 5,
132 polygon_init_rectangle (Polygon *polygon, double x0, double y0,
133 double x1, double y1)
135 return polygon_init (polygon, 4, x0, y0, x0, y1, x1, y1, x1, y0);
139 model_fini (Model *model)
143 g_free (model->objects);
144 g_free (model->sticks);
145 g_free (model->strings);
146 for (i = 0; i < model->num_offsets; i++)
147 g_free (model->offsets[i].objects);
148 g_free (model->springs);
149 g_free (model->offset_springs);
150 g_free (model->spacers);
151 for (i = 0; i < model->num_polygons; i++)
152 g_free (model->polygons[i].points);
153 g_free (model->polygons);
155 memset (model, 0, sizeof *model);
159 model_accumulate_forces (Model *model)
162 double x, y, dx, dy, distance, displacement;
166 for (i = 0; i < model->num_objects; i++) {
168 model->objects[i].force.x = 0;
169 model->objects[i].force.y = gravity * model->objects[i].mass;
172 v.x = model->objects[i].position.x - model->objects[i].previous_position.x;
173 v.y = model->objects[i].position.y - model->objects[i].previous_position.y;
174 model->objects[i].force.x -= v.x * friction;
175 model->objects[i].force.y -= v.y * friction;
178 for (i = 0; i < model->num_springs; i++) {
179 x = model->springs[i].a->position.x;
180 y = model->springs[i].a->position.y;
181 dx = model->springs[i].b->position.x - x;
182 dy = model->springs[i].b->position.y - y;
183 distance = sqrt (dx * dx + dy * dy);
186 displacement = distance - model->springs[i].length;
187 model->springs[i].a->force.x += u.x * model->k * displacement;
188 model->springs[i].a->force.y += u.y * model->k * displacement;
189 model->springs[i].b->force.x -= u.x * model->k * displacement;
190 model->springs[i].b->force.y -= u.y * model->k * displacement;
193 for (i = 0; i < model->num_offset_springs; i++) {
195 (model->offset_springs[i].a->position.x +
196 model->offset_springs[i].b->position.x) / 2;
198 (model->offset_springs[i].a->position.y +
199 model->offset_springs[i].b->position.y) / 2;
201 x = middle.x - model->offset_springs[i].dx / 2;
202 y = middle.y - model->offset_springs[i].dy / 2;
204 dx = x - model->offset_springs[i].a->position.x;
205 dy = y - model->offset_springs[i].a->position.y;
207 model->offset_springs[i].a->force.x += dx * model->k;
208 model->offset_springs[i].a->force.y += dy * model->k;
209 model->offset_springs[i].b->force.x -= dx * model->k;
210 model->offset_springs[i].b->force.y -= dy * model->k;
213 for (i = 0; i < model->num_objects; i++) {
215 model->objects[i].force.x * model->objects[i].force.x +
216 model->objects[i].force.y * model->objects[i].force.y;
224 model_integrate (Model *model, double step)
230 for (i = 0; i < model->num_objects; i++) {
231 o = &model->objects[i];
236 x + (x - o->previous_position.x) + o->force.x * step * step;
238 y + (y - o->previous_position.y) + o->force.y * step * step;
240 o->previous_position.x = x;
241 o->previous_position.y = y;
245 /* The square root in the distance computation for the string and
246 * stick constraints can be aproximated using Newton:
249 * (model->sticks[i].length +
250 * (dx * dx + dy * dy) / model->sticks[i].length) / 2;
252 * This works really well, since the constraints aren't typically
253 * violated much. Thus, the distance is really close to the stick
254 * length, which then makes a good initial guess. However, the
255 * approximation seems to be slower that just calling sqrt()...
259 estimate_distance (double dx, double dy, double r)
261 #ifdef APPROXIMATE_SQUARE_ROOTS
262 return (r + (dx * dx + dy * dy) / r) / 2;
264 return sqrt (dx * dx + dy * dy);
269 polygon_contains_point (Polygon *polygon, Point *point)
274 for (i = 0; i < polygon->num_points; i++) {
275 dx = point->x - polygon->points[i].x;
276 dy = point->y - polygon->points[i].y;
278 if (polygon->normals[i].x * dx + polygon->normals[i].y * dy >= 0)
286 polygon_reflect_object (Polygon *polygon, Object *object)
293 for (i = 0; i < polygon->num_points; i++) {
294 d = polygon->normals[i].x * (object->position.x - polygon->points[i].x) +
295 polygon->normals[i].y * (object->position.y - polygon->points[i].y);
301 n = &polygon->normals[i];
305 object->position.x -= (1 + elasticity) * distance * n->x;
306 object->position.y -= (1 + elasticity) * distance * n->y;
309 n->x * (object->previous_position.x - polygon->points[edge].x) +
310 n->y * (object->previous_position.y - polygon->points[edge].y);
312 object->previous_position.x -= (1 + elasticity) * distance * n->x;
313 object->previous_position.y -= (1 + elasticity) * distance * n->y;
317 model_constrain_polygon (Model *model, Polygon *polygon)
321 for (i = 0; i < model->num_objects; i++) {
322 if (polygon_contains_point (polygon, &model->objects[i].position))
323 polygon_reflect_object (polygon, &model->objects[i]);
328 model_constrain_anchor (Model *model, Anchor *anchor)
330 anchor->object->position.x = anchor->x;
331 anchor->object->position.y = anchor->y;
332 anchor->object->previous_position.x = anchor->x;
333 anchor->object->previous_position.y = anchor->y;
337 model_constrain_offset (Model *model, Offset *offset)
344 for (i = 0; i < offset->num_objects; i++) {
345 x += offset->objects[i]->position.x;
346 y += offset->objects[i]->position.y;
349 x = x / offset->num_objects - offset->dx * (offset->num_objects - 1) / 2;
350 y = y / offset->num_objects - offset->dy * (offset->num_objects - 1) / 2;
352 for (i = 0; i < offset->num_objects; i++) {
353 offset->objects[i]->position.x = x + offset->dx * i;
354 offset->objects[i]->position.y = y + offset->dy * i;
359 model_constrain (Model *model)
361 double dx, dy, x, y, distance, fraction;
364 if (model->mouse_anchor.object != NULL)
365 model_constrain_anchor (model, &model->mouse_anchor);
366 for (i = 0; i < model->num_anchors; i++)
367 model_constrain_anchor (model, &model->anchors[i]);
369 /* String constraints. */
370 for (i = 0; i < model->num_strings; i++) {
371 x = model->strings[i].a->position.x;
372 y = model->strings[i].a->position.y;
373 dx = model->strings[i].b->position.x - x;
374 dy = model->strings[i].b->position.y - y;
375 distance = estimate_distance (dx, dy, model->strings[i].length);
376 if (distance < model->strings[i].length)
378 fraction = (distance - model->strings[i].length) / distance / 2;
379 model->strings[i].a->position.x = x + dx * fraction;
380 model->strings[i].a->position.y = y + dy * fraction;
381 model->strings[i].b->position.x = x + dx * (1 - fraction);
382 model->strings[i].b->position.y = y + dy * (1 - fraction);
385 /* Spacer constraints. */
386 for (i = 0; i < model->num_spacers; i++) {
387 x = model->spacers[i].a->position.x;
388 y = model->spacers[i].a->position.y;
389 dx = model->spacers[i].b->position.x - x;
390 dy = model->spacers[i].b->position.y - y;
391 distance = estimate_distance (dx, dy, model->spacers[i].length);
392 if (distance > model->spacers[i].length)
394 fraction = (distance - model->spacers[i].length) / distance / 2;
395 model->spacers[i].a->position.x = x + dx * fraction;
396 model->spacers[i].a->position.y = y + dy * fraction;
397 model->spacers[i].b->position.x = x + dx * (1 - fraction);
398 model->spacers[i].b->position.y = y + dy * (1 - fraction);
401 /* Stick constraints. */
402 for (i = 0; i < model->num_sticks; i++) {
403 x = model->sticks[i].a->position.x;
404 y = model->sticks[i].a->position.y;
405 dx = model->sticks[i].b->position.x - x;
406 dy = model->sticks[i].b->position.y - y;
407 distance = estimate_distance (dx, dy, model->sticks[i].length);
408 fraction = (distance - model->sticks[i].length) / distance / 2;
409 model->sticks[i].a->position.x = x + dx * fraction;
410 model->sticks[i].a->position.y = y + dy * fraction;
411 model->sticks[i].b->position.x = x + dx * (1 - fraction);
412 model->sticks[i].b->position.y = y + dy * (1 - fraction);
415 /* Offset constraints. */
416 for (i = 0; i < model->num_offsets; i++)
417 model_constrain_offset (model, &model->offsets[i]);
419 /* Polygon constraints. */
420 for (i = 0; i < model->num_polygons; i++)
421 model_constrain_polygon (model, &model->polygons[i]);
425 model_step (Model *model, double delta_t)
429 model_accumulate_forces (model);
430 model_integrate (model, delta_t);
431 for (i = 0; i < 2; i++)
432 model_constrain (model);
434 model->theta += delta_t;
438 object_distance (Object *object, double x, double y)
442 dx = object->position.x - x;
443 dy = object->position.y - y;
445 return sqrt (dx*dx + dy*dy);
449 model_find_nearest (Model *model, double x, double y)
452 double distance, min_distance;
455 for (i = 0; i < model->num_objects; i++) {
456 distance = object_distance (&model->objects[i], x, y);
457 if (i == 0 || distance < min_distance) {
458 min_distance = distance;
459 object = &model->objects[i];