*
* TODO:
*
- * - Fix box collision test
- * - Add stick objects instead of hardcoding sticks
* - Add code to add boxes
* - Add circle object
*/
#include <cairo.h>
#include <cairo-xlib.h>
#include <gdk/gdkx.h>
+#include <stdlib.h>
#include <math.h>
-const double stick_length = 10;
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;
typedef struct _Object Object;
typedef struct _Stick Stick;
+typedef struct _String String;
+typedef struct _Offset Offset;
typedef struct _Model Model;
struct _Object {
struct _Stick {
Object *a, *b;
+ int length;
+};
+
+struct _String {
+ Object *a, *b;
+ int length;
+};
+
+struct _Offset {
+ Object *a, *b;
+ int dx, dy;
};
struct _Model {
Object *objects;
int num_sticks;
Stick *sticks;
+ int num_strings;
+ String *strings;
+ int num_offsets;
+ Offset *offsets;
double k;
double friction;
};
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);
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;
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].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 = 5;
+ 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->strings = NULL;
+ model->num_strings = 0;
+ model->offsets = NULL;
+ model->num_offsets = 0;
+
+ 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->strings = NULL;
+ model->num_strings = 0;
+ 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_init_grid (Model *model)
+{
+ const int num_ropes = 4;
+ const int num_rope_objects = 4;
+ const int num_objects = num_ropes * num_rope_objects;
+ const int num_strings = num_ropes * (num_rope_objects - 1) +
+ (num_ropes - 1) * num_rope_objects;
+ const int string_length = 30;
+ const int rope_offset = 30;
+ double x, y;
+ int i, j, index, string_index;
+
+ 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;
+
+ for (i = 0; i < num_ropes; i++) {
+ for (j = 0; j < num_rope_objects; j++) {
+ x = 200 + i * rope_offset;
+ y = 40 + j * string_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 (i + 1 < num_ropes) {
+ string_index = i * num_rope_objects + j;
+ model->strings[string_index].a = &model->objects[index];
+ model->strings[string_index].b = &model->objects[index + num_rope_objects];
+ model->strings[string_index].length = string_length;
+ }
+
+ if (j + 1 < num_rope_objects) {
+ string_index =
+ (num_ropes - 1) * num_rope_objects + i * (num_rope_objects - 1) + j;
+ model->strings[string_index].a = &model->objects[index];
+ model->strings[string_index].b = &model->objects[index + 1];
+ model->strings[string_index].length = string_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->strings);
+ model->strings = NULL;
+ model->strings = 0;
+ g_free (model->offsets);
+ model->offsets = NULL;
+ model->num_offsets = 0;
+}
+
static void
model_accumulate_forces (Model *model)
{
static void
model_constrain (Model *model, double step)
{
- double dx, dy, x, y, distance, fraction, squared;
- int i, j;
+ double dx, dy, x, y, distance, fraction;
+ int i;
/* Anchor object constraint. */
if (model->anchor_object != NULL) {
}
}
+ /* 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;
+ }
+
+ /* String constraints. */
+ for (i = 0; i < model->num_strings; i++) {
+ x = model->strings[i].a->position.x;
+ y = model->strings[i].a->position.y;
+ dx = model->strings[i].b->position.x - x;
+ dy = model->strings[i].b->position.y - y;
+ distance = sqrt (dx * dx + dy * dy);
+ if (distance < model->strings[i].length)
+ continue;
+ fraction = (distance - model->strings[i].length) / distance / 2;
+ model->strings[i].a->position.x = x + dx * fraction;
+ model->strings[i].a->position.y = y + dy * fraction;
+ model->strings[i].b->position.x = x + dx * (1 - fraction);
+ model->strings[i].b->position.y = y + dy * (1 - fraction);
+ }
+
#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[j].b->position.x - x;
- dy = model->sticks[j].b->position.y - 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 - 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[j].b->position.x = x + dx * (1 - fraction);
- model->sticks[j].b->position.y = y + dy * (1 - 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. */
model_accumulate_forces (model);
model_integrate (model, delta_t);
- for (i = 0; i < 10; i++)
+ for (i = 0; i < 35; i++)
model_constrain (model, delta_t);
model->theta += delta_t;
};
static void
-draw_star (cairo_t *cr,
- gdouble cx,
- gdouble cy,
- double theta,
- Color *color)
+draw_sticks (cairo_t *cr,
+ Model *model,
+ Color *color)
{
- const int spike_count = 5;
- const int inner_radius = 2;
- const int outer_radius = 4;
- double x, y;
int i;
- cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.5);
+ cairo_set_source_rgba (cr, color->red, color->green, color->blue, 1);
cairo_new_path (cr);
- for (i = 0; i < spike_count; i++) {
- x = cx + cos ((i * 2) * M_PI / spike_count + theta) * inner_radius;
- y = cy + sin ((i * 2) * M_PI / spike_count + theta) * inner_radius;
-
- if (i == 0)
- cairo_move_to (cr, x, y);
- else
- cairo_line_to (cr, x, y);
-
- x = cx + cos ((i * 2 + 1) * M_PI / spike_count + theta) * outer_radius;
- y = cy + sin ((i * 2 + 1) * M_PI / spike_count + theta) * outer_radius;
-
- cairo_line_to (cr, x, y);
+ 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_fill (cr);
+
+ cairo_stroke (cr);
}
static void
-draw_lines (cairo_t *cr,
- Model *model,
- Color *color)
+draw_strings (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, 1);
+ cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
+ cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
+
+ for (i = 0; i < model->num_strings; i++) {
+ cairo_move_to (cr,
+ model->strings[i].a->position.x,
+ model->strings[i].a->position.y);
+ cairo_line_to (cr,
+ model->strings[i].b->position.x,
+ model->strings[i].b->position.y);
+ }
+
+ cairo_stroke (cr);
+}
+
+static void
+draw_offsets (cairo_t *cr,
+ Model *model,
+ Color *color)
{
int i;
cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
- for (i = 0; i < model->num_objects; i++) {
+ 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->objects[i].position.x,
- model->objects[i].position.y);
+ model->offsets[i].b->position.x,
+ model->offsets[i].b->position.y);
}
cairo_stroke (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 green = { 0, 1, 0 };
static Color red = { 1, 0, 0 };
+static Color black = { 0, 0, 0 };
+static Color white = { 1, 1, 1 };
static gboolean
-sproing_expose_event (GtkWidget *widget,
- GdkEventExpose *event,
- gpointer data)
+expose_event (GtkWidget *widget,
+ GdkEventExpose *event,
+ gpointer data)
{
Model *model = data;
- int i;
cairo_t *cr;
cr = gdk_cairo_create (widget->window);
cairo_paint (cr);
draw_constraints (cr, model, &red);
- draw_lines (cr, model, &blue);
-
-#if 0
- for (i = 0; i < model->num_objects; i++) {
- draw_star (widget, model->objects[i].position.x,
- model->objects[i].position.y, model->objects[i].theta, &blue);
- }
-#endif
+ 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
-sproing_button_press_event (GtkWidget *widget,
- GdkEventButton *event,
- gpointer data)
+button_press_event (GtkWidget *widget,
+ GdkEventButton *event,
+ gpointer data)
{
Model *model = data;
}
static gboolean
-sproing_button_release_event (GtkWidget *widget,
- GdkEventButton *event,
- gpointer data)
+button_release_event (GtkWidget *widget,
+ GdkEventButton *event,
+ gpointer data)
{
Model *model = data;
}
static gboolean
-sproing_motion_notify_event (GtkWidget *widget,
- GdkEventMotion *event,
- gpointer data)
+motion_notify_event (GtkWidget *widget,
+ GdkEventMotion *event,
+ gpointer data)
{
Model *model = data;
int x, y;
return TRUE;
}
+typedef void (*ModelInitFunc) (Model *model);
+
static void
-spring_constant_changed (GtkSpinButton *spinbutton, gpointer user_data)
+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;
- model->k = gtk_spin_button_get_value (spinbutton);
+ gtk_tree_model_get (tree_model, &iter, 0, &name, 1, &init, -1);
+
+ model_fini (model);
+ (*init) (model);
}
-static void
-friction_changed (GtkSpinButton *spinbutton, gpointer user_data)
+static GtkTreeModel *
+create_model_store (void)
{
- Model *model = user_data;
+ static struct {
+ const char *name;
+ ModelInitFunc init;
+ } models[] = {
+ { "Rope", model_init_rope },
+ { "Snake", model_init_snake },
+ { "Curtain", model_init_curtain },
+ { "Grid", model_init_grid }
+ };
+
+ 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);
- model->friction = gtk_spin_button_get_value (spinbutton);
}
static GtkWidget *
-create_spinners (Model *model)
+create_model_combo (Model *model)
{
GtkWidget *hbox;
- GtkWidget *spinner, *label;
+ GtkWidget *combo, *label;
+ GtkTreeModel *store;
+ GtkCellRenderer *renderer;
hbox = gtk_hbox_new (FALSE, 8);
- label = gtk_label_new_with_mnemonic ("_Spring constant:");
- gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
- spinner = gtk_spin_button_new_with_range (0.05, 30.00, 0.05);
- gtk_label_set_mnemonic_widget (GTK_LABEL (label), spinner);
- gtk_box_pack_start (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
- gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinner), model->k);
- g_signal_connect (spinner, "value-changed",
- G_CALLBACK (spring_constant_changed), model);
-
- label = gtk_label_new_with_mnemonic ("_Friction:");
+ label = gtk_label_new_with_mnemonic ("_Model:");
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
- spinner = gtk_spin_button_new_with_range (0.05, 15.00, 0.05);
- gtk_label_set_mnemonic_widget (GTK_LABEL (label), spinner);
- gtk_box_pack_start (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);
- gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinner), model->friction);
- g_signal_connect (spinner, "value-changed",
- G_CALLBACK (friction_changed), model);
+
+ 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;
}
GtkWidget *frame;
GtkWidget *vbox;
GtkWidget *da;
- GtkWidget *spinners;
+ GtkWidget *model_combo;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
/* Signals used to handle backing pixmap */
g_signal_connect (da, "expose_event",
- G_CALLBACK (sproing_expose_event), model);
+ G_CALLBACK (expose_event), model);
/* Event signals */
g_signal_connect (da, "motion_notify_event",
- G_CALLBACK (sproing_motion_notify_event), model);
+ G_CALLBACK (motion_notify_event), model);
g_signal_connect (da, "button_press_event",
- G_CALLBACK (sproing_button_press_event), model);
+ G_CALLBACK (button_press_event), model);
g_signal_connect (da, "button_release_event",
- G_CALLBACK (sproing_button_release_event), model);
+ G_CALLBACK (button_release_event), model);
/* Ask to receive events the drawing area doesn't normally
* subscribe to
| GDK_POINTER_MOTION_MASK
| GDK_POINTER_MOTION_HINT_MASK);
- spinners = create_spinners (model);
- gtk_box_pack_start (GTK_BOX (vbox), spinners, FALSE, FALSE, 0);
+ model_combo = create_model_combo (model);
+ gtk_box_pack_start (GTK_BOX (vbox), model_combo, FALSE, FALSE, 0);
return da;
}
Model model;
gtk_init (&argc, &argv);
- model_init (&model);
+ model_init_rope (&model);
closure.drawing_area = create_window (&model);
closure.i = 0;
gtk_widget_show_all (gtk_widget_get_toplevel (closure.drawing_area));