1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
|
/* -*- mode: c; c-basic-offset: 2 -*-
* To compile:
*
* gcc -Wall -g $(pkg-config --cflags --libs gtk+-2.0 cairo) \
* akamaru.c -o akamaru
*
* See:
*
* http://en.wikipedia.org/wiki/Verlet_integration
* http://www.teknikus.dk/tj/gdc2001.htm
*
* TODO:
*
* - Add code to add boxes
* - Add circle object
*/
#include <gtk/gtk.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <gdk/gdkx.h>
#include <stdlib.h>
#include <math.h>
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;
const double edge_fuzz = 1;
typedef struct _xy_pair Point;
typedef struct _xy_pair Vector;
struct _xy_pair {
double x, y;
};
typedef struct _Object Object;
typedef struct _Stick Stick;
typedef struct _Offset Offset;
typedef struct _Model Model;
struct _Object {
Vector force;
Point position;
Point previous_position;
Vector velocity;
double mass;
double theta;
};
struct _Stick {
Object *a, *b;
int length;
};
struct _Offset {
Object *a, *b;
int dx, dy;
};
struct _Model {
int num_objects;
Object *objects;
int num_sticks;
Stick *sticks;
int num_offsets;
Offset *offsets;
double k;
double friction;
Object *anchor_object;
Vector anchor_position;
double theta;
};
static void
model_init_snake (Model *model)
{
const int num_objects = 20;
const int num_sticks = num_objects * 2 - 3;
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;
model->num_offsets = 0;
for (i = 0; i < num_objects; i++) {
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;
}
}
model->anchor_object = NULL;
}
static void
model_init_curtain (Model *model)
{
const int num_ropes = 5;
const int num_rope_objects = 15;
const int num_objects = num_ropes * num_rope_objects;
const int num_sticks = num_ropes * (num_rope_objects - 1);
const int stick_length = 10;
const int rope_offset = 30;
double x, y;
int i, j, index, stick_index;
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;
for (i = 0; i < num_ropes; i++) {
for (j = 0; j < num_rope_objects; j++) {
x = 200 + i * rope_offset;
y = 40 + j * stick_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 (j + 1 < num_rope_objects) {
stick_index = i * (num_rope_objects - 1) + j;
model->sticks[stick_index].a = &model->objects[index];
model->sticks[stick_index].b = &model->objects[index + 1];
model->sticks[stick_index].length = stick_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->offsets);
model->offsets = NULL;
model->num_offsets = 0;
}
static void
model_accumulate_forces (Model *model)
{
int i;
for (i = 0; i < model->num_objects; i++) {
model->objects[i].force.x = 0;
model->objects[i].force.y = 3;
}
}
static void
model_integrate (Model *model, double step)
{
double x, y;
Object *o;
int i;
for (i = 0; i < model->num_objects; i++) {
o = &model->objects[i];
x = o->position.x;
y = o->position.y;
o->position.x =
x + (x - o->previous_position.x) + o->force.x * step * step;
o->position.y =
y + (y - o->previous_position.y) + o->force.y * step * step;
o->previous_position.x = x;
o->previous_position.y = y;
}
}
static void
model_constrain (Model *model, double step)
{
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;
}
/* 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;
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;
}
#if 1
/* 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);
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
}
static void
model_step (Model *model, double delta_t)
{
int i;
model_accumulate_forces (model);
model_integrate (model, delta_t);
for (i = 0; i < 5; i++)
model_constrain (model, delta_t);
model->theta += delta_t;
}
static double
object_distance (Object *object, double x, double y)
{
double dx, dy;
dx = object->position.x - x;
dy = object->position.y - y;
return sqrt (dx*dx + dy*dy);
}
static Object *
model_find_nearest (Model *model, double x, double y)
{
Object *object;
double distance, min_distance;
int i;
for (i = 0; i < model->num_objects; i++) {
distance = object_distance (&model->objects[i], x, y);
if (i == 0 || distance < min_distance) {
min_distance = distance;
object = &model->objects[i];
}
}
return object;
}
typedef struct _Color Color;
struct _Color {
double red, green, blue;
};
static void
draw_sticks (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, 2);
cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
for (i = 0; i < model->num_sticks; i++) {
cairo_move_to (cr,
model->sticks[i].a->position.x,
model->sticks[i].a->position.y);
cairo_line_to (cr,
model->sticks[i].b->position.x,
model->sticks[i].b->position.y);
}
cairo_stroke (cr);
}
static void
draw_offsets (cairo_t *cr,
Model *model,
Color *color)
{
int i;
cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.5);
cairo_new_path (cr);
cairo_set_line_width (cr, 4);
cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
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);
}
cairo_stroke (cr);
}
static void
draw_constraints (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);
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);
cairo_fill (cr);
}
static void
draw_objects (cairo_t *cr, Model *model, Color *color)
{
int i;
for (i = 0; i < model->num_objects; i++) {
}
}
static Color blue = { 0, 0, 1 };
static Color red = { 1, 0, 0 };
static Color black = { 0, 0, 0 };
static Color white = { 1, 1, 1 };
static gboolean
expose_event (GtkWidget *widget,
GdkEventExpose *event,
gpointer data)
{
Model *model = data;
cairo_t *cr;
cr = gdk_cairo_create (widget->window);
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);
cairo_destroy (cr);
return TRUE;
}
static gboolean
button_press_event (GtkWidget *widget,
GdkEventButton *event,
gpointer data)
{
Model *model = 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);
return TRUE;
}
static gboolean
button_release_event (GtkWidget *widget,
GdkEventButton *event,
gpointer data)
{
Model *model = data;
if ((event->state & GDK_BUTTON1_MASK) == 0)
return TRUE;
model->anchor_object = NULL;
return TRUE;
}
static gboolean
motion_notify_event (GtkWidget *widget,
GdkEventMotion *event,
gpointer data)
{
Model *model = 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;
return TRUE;
}
typedef void (*ModelInitFunc) (Model *model);
static void
model_changed (GtkComboBox *combo, gpointer user_data)
{
Model *model = user_data;
GtkTreeIter iter;
GtkTreeModel *tree_model;
ModelInitFunc init;
char *name;
tree_model = gtk_combo_box_get_model (combo);
if (!gtk_combo_box_get_active_iter (combo, &iter))
return;
gtk_tree_model_get (tree_model, &iter, 0, &name, 1, &init, -1);
model_fini (model);
(*init) (model);
}
static GtkTreeModel *
create_model_store (void)
{
static struct {
const char *name;
ModelInitFunc init;
} models[] = {
{ "Rope", model_init_rope },
{ "Snake", model_init_snake },
{ "Curtain", model_init_curtain }
};
GtkTreeIter iter;
GtkTreeStore *store;
gint i;
store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER);
for (i = 0; i < G_N_ELEMENTS(models); i++) {
gtk_tree_store_append (store, &iter, NULL);
gtk_tree_store_set (store, &iter,
0, models[i].name, 1, models[i].init, -1);
}
return GTK_TREE_MODEL (store);
}
static GtkWidget *
create_model_combo (Model *model)
{
GtkWidget *hbox;
GtkWidget *combo, *label;
GtkTreeModel *store;
GtkCellRenderer *renderer;
hbox = gtk_hbox_new (FALSE, 8);
label = gtk_label_new_with_mnemonic ("_Model:");
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
store = create_model_store ();
combo = gtk_combo_box_new_with_model (store);
gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
g_object_unref (store);
renderer = gtk_cell_renderer_text_new ();
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
"text", 0,
NULL);
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);
return hbox;
}
static GtkWidget *
create_window (Model *model)
{
GtkWidget *window;
GtkWidget *frame;
GtkWidget *vbox;
GtkWidget *da;
GtkWidget *model_combo;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_main_quit), &window);
gtk_container_set_border_width (GTK_CONTAINER (window), 8);
vbox = gtk_vbox_new (FALSE, 8);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
gtk_container_add (GTK_CONTAINER (window), vbox);
/*
* Create the drawing area
*/
frame = gtk_frame_new (NULL);
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
da = gtk_drawing_area_new ();
/* set a minimum size */
gtk_widget_set_size_request (da, 600, 500);
gtk_container_add (GTK_CONTAINER (frame), da);
/* Signals used to handle backing pixmap */
g_signal_connect (da, "expose_event",
G_CALLBACK (expose_event), model);
/* Event signals */
g_signal_connect (da, "motion_notify_event",
G_CALLBACK (motion_notify_event), model);
g_signal_connect (da, "button_press_event",
G_CALLBACK (button_press_event), model);
g_signal_connect (da, "button_release_event",
G_CALLBACK (button_release_event), model);
/* Ask to receive events the drawing area doesn't normally
* subscribe to
*/
gtk_widget_set_events (da, gtk_widget_get_events (da)
| GDK_LEAVE_NOTIFY_MASK
| GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
| GDK_POINTER_MOTION_MASK
| GDK_POINTER_MOTION_HINT_MASK);
model_combo = create_model_combo (model);
gtk_box_pack_start (GTK_BOX (vbox), model_combo, FALSE, FALSE, 0);
return 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);
closure->i++;
if (closure->i == 1) {
gtk_widget_queue_draw (closure->drawing_area);
closure->i = 0;
}
return TRUE;
}
int
main (int argc, char *argv[])
{
Closure closure;
Model model;
gtk_init (&argc, &argv);
model_init_rope (&model);
closure.drawing_area = create_window (&model);
closure.i = 0;
gtk_widget_show_all (gtk_widget_get_toplevel (closure.drawing_area));
closure.model = &model;
g_timeout_add (100, timeout_callback, &closure);
gtk_main ();
return 0;
}
|