summaryrefslogtreecommitdiff
path: root/src/examples/ephysics
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@gmail.com>2013-01-03 22:10:40 +0000
committerGustavo Sverzut Barbieri <barbieri@gmail.com>2013-01-03 22:10:40 +0000
commit9e0788cc2e261ec66cf740e35a3e71f4485df763 (patch)
tree8c1230e446edd6966d39c1b8959c7f97f5c0f731 /src/examples/ephysics
parent9edec477ebb83e64f3e464d82665e2b9f01f9bb0 (diff)
efl: merge ephysics
changes: * __UNUSED__ -> EINA_UNUSED * Fixed doc hierarchy SVN revision: 82126
Diffstat (limited to 'src/examples/ephysics')
-rw-r--r--src/examples/ephysics/Makefile.am87
-rw-r--r--src/examples/ephysics/ephysics_logo.c280
-rw-r--r--src/examples/ephysics/ephysics_test.h81
-rw-r--r--src/examples/ephysics/test.c313
-rw-r--r--src/examples/ephysics/test_bouncing_ball.c112
-rw-r--r--src/examples/ephysics/test_bouncing_text.c67
-rw-r--r--src/examples/ephysics/test_camera.c236
-rw-r--r--src/examples/ephysics/test_camera_track.c248
-rw-r--r--src/examples/ephysics/test_colliding_balls.c90
-rw-r--r--src/examples/ephysics/test_collision_detection.c187
-rw-r--r--src/examples/ephysics/test_collision_filter.c91
-rw-r--r--src/examples/ephysics/test_constraint.c117
-rw-r--r--src/examples/ephysics/test_delete.c193
-rw-r--r--src/examples/ephysics/test_forces.c114
-rw-r--r--src/examples/ephysics/test_growing_balls.c122
-rw-r--r--src/examples/ephysics/test_no_gravity.c133
-rw-r--r--src/examples/ephysics/test_rotating_forever.c206
-rw-r--r--src/examples/ephysics/test_shapes.c110
-rw-r--r--src/examples/ephysics/test_sleeping_threshold.c119
-rw-r--r--src/examples/ephysics/test_slider.c170
-rw-r--r--src/examples/ephysics/test_velocity.c211
21 files changed, 3287 insertions, 0 deletions
diff --git a/src/examples/ephysics/Makefile.am b/src/examples/ephysics/Makefile.am
new file mode 100644
index 0000000..00f8560
--- /dev/null
+++ b/src/examples/ephysics/Makefile.am
@@ -0,0 +1,87 @@
1MAINTAINERCLEANFILES = Makefile.in
2
3AM_CPPFLAGS = \
4-I. \
5-I$(top_srcdir)/src/lib/eina \
6-I$(top_builddir)/src/lib/eina \
7-I$(top_srcdir)/src/lib/eo \
8-I$(top_builddir)/src/lib/eo \
9-I$(top_srcdir)/src/lib/ecore \
10-I$(top_builddir)/src/lib/ecore \
11-I$(top_srcdir)/src/lib/evas \
12-I$(top_builddir)/src/lib/evas \
13-I$(top_srcdir)/src/lib/ecore_evas \
14-I$(top_builddir)/src/lib/ecore_evas \
15-I$(top_srcdir)/src/lib/ephysics \
16-I$(top_builddir)/src/lib/ephysics
17
18LDADD = \
19$(top_builddir)/src/lib/eina/libeina.la \
20$(top_builddir)/src/lib/eo/libeo.la \
21$(top_builddir)/src/lib/ecore/libecore.la \
22$(top_builddir)/src/lib/evas/libevas.la \
23$(top_builddir)/src/lib/ecore_evas/libecore_evas.la \
24$(top_builddir)/src/lib/ephysics/libephysics.la \
25@EPHYSICS_LDFLAGS@
26
27SRCS = \
28ephysics_logo.c \
29ephysics_test.h \
30test_bouncing_ball.c \
31test_bouncing_text.c \
32test.c \
33test_camera.c \
34test_camera_track.c \
35test_colliding_balls.c \
36test_collision_detection.c \
37test_collision_filter.c \
38test_constraint.c \
39test_delete.c \
40test_forces.c \
41test_growing_balls.c \
42test_no_gravity.c \
43test_rotating_forever.c \
44test_shapes.c \
45test_sleeping_threshold.c \
46test_slider.c \
47test_velocity.c
48
49EXTRA_PROGRAMS =
50
51# NOTE: neither ephysics_test or ephysics_logo won't be built as they needs Elementary
52# TODO: convert these to bare ecore_evas, at least a reduced set to have compile-able examples
53
54ephysics_test_SOURCES = \
55ephysics_test.h \
56test_bouncing_ball.c \
57test_bouncing_text.c \
58test.c \
59test_camera.c \
60test_camera_track.c \
61test_colliding_balls.c \
62test_collision_detection.c \
63test_collision_filter.c \
64test_constraint.c \
65test_delete.c \
66test_forces.c \
67test_growing_balls.c \
68test_no_gravity.c \
69test_rotating_forever.c \
70test_shapes.c \
71test_sleeping_threshold.c \
72test_slider.c \
73test_velocity.c
74
75examples: $(EXTRA_PROGRAMS)
76
77clean-local:
78 rm -f $(EXTRA_PROGRAMS)
79
80install-examples:
81 mkdir -p $(datadir)/ephysics/examples
82 $(install_sh_DATA) -c $(SRCS) $(datadir)/ephysics/examples
83
84uninstall-local:
85 for f in $(SRCS) ; do \
86 rm -f $(datadir)/ephysics/examples/$$f ; \
87 done
diff --git a/src/examples/ephysics/ephysics_logo.c b/src/examples/ephysics/ephysics_logo.c
new file mode 100644
index 0000000..2d05566
--- /dev/null
+++ b/src/examples/ephysics/ephysics_logo.c
@@ -0,0 +1,280 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include <Elementary.h>
6#include <EPhysics.h>
7#include <Evas.h>
8
9#define WIDTH (512)
10#define HEIGHT (384)
11#define DEPTH (100)
12#define FLOOR_Y (HEIGHT - 80)
13#define SH_THRESHOLD (250)
14#define SH_OFFSET_X (- 16)
15#define OFFSET_X (90)
16#define PADDING_X_1 (16)
17#define PADDING_X_2 (12)
18#define PADDING_X_3 (22)
19#define E_THRESHOLD (WIDTH + 560)
20#define LAYER_SHADOW (10)
21#define LAYER_LETTER (20)
22
23#define CENTER(total, item) (((total) - (item)) / 2)
24#define LIMIT(val, op, ref) (((val) op (ref)) ? (val) : (ref));
25#define PROP_GET(pos, min, max) (((min) + ((max) - (min)) * (pos)) / (max))
26
27static void
28_update_box_cb(void *data __UNUSED__, EPhysics_Body *body, void *event_info)
29{
30 Evas_Object *image = event_info;
31 Evas_Object *shadow = evas_object_data_get(image, "shadow");
32 Evas_Object *light = evas_object_data_get(image, "light");
33 int x, y, w, h, floor_distance, alpha = 0;
34
35 /* modify the evas object according to the body */
36 ephysics_body_evas_object_update(body);
37 evas_object_geometry_get(image, &x, &y, &w, &h);
38
39 floor_distance = FLOOR_Y - h;
40
41 /* Bodies penetrates the ground slightly before bouncing. */
42 /* This is to be expected in realtime physics engines. */
43 // TODO BUG: should we move the object up by the difference????
44 //y = LIMIT(y, <=, floor_distance);
45
46 /* We should show the shadow when we're close enough to ground */
47 if (y > SH_THRESHOLD)
48 {
49 int sh_w, sh_h;
50 double pos_x;
51
52 evas_object_image_size_get(shadow, &sh_w, &sh_h);
53 /* shadow is darker with bigger y */
54 alpha = 255 * (y - SH_THRESHOLD) / (floor_distance - SH_THRESHOLD);
55 /* and with bigger x -- it's proportional to x / WIDTH, but varies
56 * from 100 to 255.
57 */
58 pos_x = (double) x / (WIDTH - w);
59 alpha = alpha * PROP_GET(pos_x, 100, 255);
60 /* box shadow is not resized, just moved */
61 evas_object_move(shadow, x + CENTER(w, sh_w) + SH_OFFSET_X,
62 FLOOR_Y - sh_h + 2);
63 }
64 evas_object_color_set(shadow, alpha, alpha, alpha, alpha);
65
66 evas_object_move(light, x, y);
67 /* it's lighter with bigger y */
68 alpha = (y <= 0) ? 0 : y * 255 / floor_distance;
69 /* and with bigger x */
70 alpha = alpha * (x - OFFSET_X + 80) / (WIDTH - OFFSET_X);
71 evas_object_color_set(light, alpha, alpha, alpha, alpha);
72}
73
74static void
75_update_circle_cb(void *data __UNUSED__, EPhysics_Body *body, void *event_info)
76{
77 Evas_Object *image = event_info;
78 Evas_Object *shadow = evas_object_data_get(image, "shadow");
79 Evas_Object *light = evas_object_data_get(image, "light");
80 int x, y, w, h, sh_w, sh_h, alpha = 0;
81 const Evas_Map *map;
82
83 /* modify the evas object according to the body */
84 ephysics_body_evas_object_update(body);
85 evas_object_geometry_get(image, &x, &y, &w, &h);
86
87 evas_object_move(light, x, y);
88 alpha = x * 255 / (WIDTH - w);
89 evas_object_color_set(light, alpha, alpha, alpha, alpha);
90
91 /* use the same map from image to the light (rotate it) */
92 map = evas_object_map_get(image);
93 evas_object_map_set(light, map);
94 evas_object_map_enable_set(light, EINA_TRUE);
95
96 evas_object_image_size_get(shadow, &sh_w, &sh_h);
97 evas_object_move(shadow, x + CENTER(w, sh_w) + SH_OFFSET_X,
98 FLOOR_Y - sh_h + 2);
99 alpha = 127 + alpha / 2;
100 evas_object_color_set(shadow, alpha, alpha, alpha, alpha);
101
102 if (x > E_THRESHOLD)
103 ephysics_body_move(body, -w - 1, y, -15);
104}
105
106static void
107_letter_add(Evas *evas, const char *letter, Evas_Object **image, Evas_Object **light, Evas_Object **shadow)
108{
109 int w, h, sh_w, sh_h;
110 char buf[1024];
111
112 snprintf(buf, sizeof(buf), PACKAGE_DATA_DIR "/logo_shadow-%s.png", letter);
113 *shadow = evas_object_image_filled_add(evas);
114 evas_object_image_file_set(*shadow, buf, NULL);
115 evas_object_image_size_get(*shadow, &sh_w, &sh_h);
116 evas_object_resize(*shadow, sh_w, sh_h);
117 evas_object_color_set(*shadow, 0, 0, 0, 0);
118 evas_object_layer_set(*shadow, LAYER_SHADOW);
119 evas_object_show(*shadow);
120
121 snprintf(buf, sizeof(buf), PACKAGE_DATA_DIR "/logo_letter-%s.png", letter);
122 *image = evas_object_image_filled_add(evas);
123 evas_object_image_file_set(*image, buf, NULL);
124 evas_object_image_size_get(*image, &w, &h);
125 evas_object_resize(*image, w, h);
126 evas_object_layer_set(*image, LAYER_LETTER);
127 evas_object_show(*image);
128
129 snprintf(buf, sizeof(buf), PACKAGE_DATA_DIR "/logo_light-%s.png", letter);
130 *light = evas_object_image_filled_add(evas);
131 evas_object_image_file_set(*light, buf, NULL);
132 evas_object_resize(*light, w, h);
133 evas_object_layer_set(*light, LAYER_LETTER);
134 evas_object_show(*light);
135
136 /* allow easy access to shadow and light from the letter image */
137 evas_object_data_set(*image, "shadow", *shadow);
138 evas_object_data_set(*image, "light", *light);
139}
140
141static void
142_letter_body_setup_common(EPhysics_Body *body, Evas_Object *view)
143{
144 ephysics_body_evas_object_set(body, view, EINA_TRUE);
145 ephysics_body_mass_set(body, 1.2);
146 ephysics_body_restitution_set(body, 0.6);
147 ephysics_body_angular_movement_enable_set(body, EINA_FALSE, EINA_FALSE,
148 EINA_FALSE);
149}
150
151static EPhysics_Body *
152_letter_body_box_add(EPhysics_World *world, Evas_Object *image)
153{
154 EPhysics_Body *body = ephysics_body_box_add(world);
155
156 _letter_body_setup_common(body, image);
157
158 ephysics_body_event_callback_add
159 (body, EPHYSICS_CALLBACK_BODY_UPDATE, _update_box_cb, NULL);
160
161 return body;
162}
163
164static EPhysics_Body *
165_letter_body_circle_add(EPhysics_World *world, Evas_Object *image)
166{
167 EPhysics_Body *body = ephysics_body_cylinder_add(world);
168
169 _letter_body_setup_common(body, image);
170
171 ephysics_body_event_callback_add
172 (body, EPHYSICS_CALLBACK_BODY_UPDATE, _update_circle_cb, NULL);
173
174 return body;
175}
176
177EAPI_MAIN int
178elm_main(int argc __UNUSED__, char **argv __UNUSED__)
179{
180 Evas_Object *win, *bg, *image, *light, *shadow;
181 EPhysics_Body *ground_body, *letter_body;
182 EPhysics_World *world;
183 unsigned int i = 0;
184 int x, w, h, sh_w;
185 Evas *evas;
186
187 struct letter_desc {
188 const char *letter;
189 int padding;
190 } falling_letters[] = {
191 {"P", PADDING_X_1},
192 {"H", PADDING_X_1},
193 {"Y", PADDING_X_3},
194 {"S1", PADDING_X_2},
195 {"I", PADDING_X_2},
196 {"C", PADDING_X_3},
197 {"S2", 0}
198 };
199
200 if (!ephysics_init())
201 return - 1;
202
203 elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
204
205 win = elm_win_add(NULL, "main", ELM_WIN_SPLASH);
206 elm_win_title_set(win, "EPhysics Logo");
207 elm_win_autodel_set(win, EINA_TRUE);
208 evas_object_show(win);
209
210 bg = elm_bg_add(win);
211 elm_bg_file_set(bg, PACKAGE_DATA_DIR "/logo_background.png", NULL);
212 elm_win_resize_object_add(win, bg);
213 evas_object_size_hint_min_set(bg, WIDTH, HEIGHT);
214 evas_object_size_hint_max_set(bg, WIDTH, HEIGHT);
215 evas_object_show(bg);
216
217 world = ephysics_world_new();
218 ephysics_world_render_geometry_set(world, 0, 0, -50, WIDTH, HEIGHT, DEPTH);
219
220 ground_body = ephysics_body_box_add(world);
221 ephysics_body_mass_set(ground_body, EPHYSICS_BODY_MASS_STATIC);
222 ephysics_body_geometry_set(ground_body, -100, FLOOR_Y, -15, WIDTH + 800, 10,
223 30);
224 ephysics_body_restitution_set(ground_body, 0.65);
225 ephysics_body_friction_set(ground_body, 0.8);
226
227 evas = evas_object_evas_get(win);
228 x = OFFSET_X;
229
230 for (i = 0; i < EINA_C_ARRAY_LENGTH(falling_letters); i++)
231 {
232 _letter_add(evas, falling_letters[i].letter, &image, &light, &shadow);
233
234 evas_object_image_size_get(shadow, &sh_w, NULL);
235 evas_object_image_size_get(image, &w, &h);
236
237 /* place image and light on top, above what the viewport can show */
238 evas_object_move(image, x, -h * (i + 1) - 50);
239 evas_object_move(light, x, -h * (i + 1) - 50);
240 /* place shadow below the hit-line: FLOOR_Y, centered at image */
241 evas_object_move(shadow, x + CENTER(w, sh_w), FLOOR_Y);
242
243 x += falling_letters[i].padding + w;
244
245 letter_body = _letter_body_box_add(world, image);
246 ephysics_body_friction_set(letter_body, 0.4);
247 }
248
249 /* E is a circle that comes rolling on the floor */
250 _letter_add(evas, "E", &image, &light, &shadow);
251 evas_object_color_set(shadow, 255, 255, 255, 255);
252
253 evas_object_image_size_get(shadow, &sh_w, NULL);
254 evas_object_image_size_get(image, &w, &h);
255 /* place image and light so they are above the floor,
256 * and to the left of viewport
257 */
258 evas_object_move(image, -w - 1, FLOOR_Y - h + 1);
259 evas_object_move(light, -w - 1, FLOOR_Y - h + 1);
260 /* place the shadow below the hit-line: FLOOR_Y, centered at image */
261 evas_object_move(shadow, -w - 1 + CENTER(w, sh_w), FLOOR_Y);
262
263 letter_body = _letter_body_circle_add(world, image);
264 ephysics_body_friction_set(letter_body, 1);
265 ephysics_body_mass_set(letter_body, 1);
266 ephysics_body_angular_movement_enable_set(letter_body, EINA_FALSE,
267 EINA_FALSE, EINA_TRUE);
268
269 /* make the "E" logo get into the viewport by applying an horizontal force */
270 ephysics_body_central_impulse_apply(letter_body, 390, 0, 0);
271
272 elm_run();
273
274 ephysics_world_del(world);
275 ephysics_shutdown();
276 elm_shutdown();
277
278 return 0;
279}
280ELM_MAIN()
diff --git a/src/examples/ephysics/ephysics_test.h b/src/examples/ephysics/ephysics_test.h
new file mode 100644
index 0000000..56db595
--- /dev/null
+++ b/src/examples/ephysics/ephysics_test.h
@@ -0,0 +1,81 @@
1#ifndef _EPHYSICS_PRIVATE_H
2#define _EPHYSICS_PRIVATE_H
3
4#include <Edje.h>
5#include <Elementary.h>
6#include <Eina.h>
7#include <Evas.h>
8
9#include "EPhysics.h"
10
11#ifdef EPHYSICS_LOG_COLOR
12#undef EPHYSICS_LOG_COLOR
13#endif
14#define EPHYSICS_LOG_COLOR EINA_COLOR_BLUE
15
16#ifdef ERR
17# undef ERR
18#endif
19#define ERR(...) EINA_LOG_DOM_ERR(_ephysics_test_log_dom, __VA_ARGS__)
20
21#ifdef DBG
22# undef DBG
23#endif
24#define DBG(...) EINA_LOG_DOM_DBG(_ephysics_test_log_dom, __VA_ARGS__)
25
26#ifdef INF
27# undef INF
28#endif
29#define INF(...) EINA_LOG_DOM_INFO(_ephysics_test_log_dom, __VA_ARGS__)
30
31#ifdef WRN
32# undef WRN
33#endif
34#define WRN(...) EINA_LOG_DOM_WARN(_ephysics_test_log_dom, __VA_ARGS__)
35
36#ifdef CRIT
37# undef CRIT
38#endif
39#define CRIT(...) EINA_LOG_DOM_CRIT(_ephysics_test_log_dom, __VA_ARGS__)
40
41#define EPHYSICS_TEST_THEME "ephysics_test"
42#define WIDTH (512)
43#define HEIGHT (384)
44#define DEPTH (100)
45#define SHADOW_ALPHA_ID (1)
46#define FLOOR_Y (344)
47#define FLOOR_WIDTH (750)
48#define SH_THRESHOLD (200)
49
50#ifdef __cplusplus
51extern "C" {
52#endif
53
54extern int _ephysics_test_log_dom;
55
56typedef struct _Test_Data Test_Data;
57
58struct _Test_Data {
59 EPhysics_World *world;
60 Evas_Object *layout;
61 Evas_Object *win;
62 Eina_List *constraints;
63 Eina_List *evas_objs;
64 Eina_List *bodies;
65 void *data;
66};
67
68Test_Data *test_data_new(void);
69void test_data_del(Test_Data *test_data);
70
71void test_clean(Test_Data *test_data);
72
73void test_win_add(Test_Data *test_data, const char *title, Eina_Bool autodel);
74
75void update_object_cb(void *data, EPhysics_Body *body, void *event_info);
76
77#ifdef __cplusplus
78}
79#endif
80
81#endif
diff --git a/src/examples/ephysics/test.c b/src/examples/ephysics/test.c
new file mode 100644
index 0000000..88cbb5e
--- /dev/null
+++ b/src/examples/ephysics/test.c
@@ -0,0 +1,313 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7#define EPHYSICS_TEST_LOG_COLOR EINA_COLOR_GREEN
8
9int _ephysics_test_log_dom = -1;
10
11typedef struct _EPhysics_Test EPhysics_Test;
12struct _EPhysics_Test {
13 const char *name;
14 void (*func)(void *data, Evas_Object *obj, void *event_info);
15};
16
17/* examples prototypes */
18void test_bouncing_3d(void *data, Evas_Object *obj, void *event_info);
19void test_bouncing_ball(void *data, Evas_Object *obj, void *event_info);
20void test_bouncing_text(void *data, Evas_Object *obj, void *event_info);
21void test_camera(void *data, Evas_Object *obj, void *event_info);
22void test_camera_track(void *data, Evas_Object *obj, void *event_info);
23void test_coins(void *data, Evas_Object *obj, void *event_info);
24void test_colliding_balls(void *data, Evas_Object *obj, void *event_info);
25void test_collision(void *data, Evas_Object *obj, void *event_info);
26void test_collision_filter(void *data, Evas_Object *obj, void *event_info);
27void test_collision_speed(void *data, Evas_Object *obj, void *event_info);
28void test_constraint(void *data, Evas_Object *obj, void *event_info);
29void test_cube_3d(void *data, Evas_Object *obj, void *event_info);
30void test_delete(void *data, Evas_Object *obj, void *event_info);
31void test_dices(void *data, Evas_Object *obj, void *event_info);
32void test_falling_letters(void *data, Evas_Object *obj, void *event_info);
33void test_flag(void *data, Evas_Object *obj, void *event_info);
34void test_flip_page(void *data, Evas_Object *obj, void *event_info);
35void test_forces(void *data, Evas_Object *obj, void *event_info);
36void test_grab(void *data, Evas_Object *obj, void *event_info);
37void test_growing_balls(void *data, Evas_Object *obj, void *event_info);
38void test_heavy(void *data, Evas_Object *obj, void *event_info);
39void test_hinge(void *data, Evas_Object *obj, void *event_info);
40void test_jumping_balls(void *data, Evas_Object *obj, void *event_info);
41void test_light(void *data, Evas_Object *obj, void *event_info);
42void test_material(void *data, Evas_Object *obj, void *event_info);
43void test_no_gravity(void *data, Evas_Object *obj, void *event_info);
44void test_rotate(void *data, Evas_Object *obj, void *event_info);
45void test_rotating_forever(void *data, Evas_Object *obj, void *event_info);
46void test_velocity(void *data, Evas_Object *obj, void *event_info);
47void test_shapes(void *data, Evas_Object *obj, void *event_info);
48void test_sleeping(void *data, Evas_Object *obj, void *event_info);
49void test_slider(void *data, Evas_Object *obj, void *event_info);
50void test_soft_body(void *data, Evas_Object *obj, void *event_info);
51void test_soft_button(void *data, Evas_Object *obj, void *event_info);
52void test_soft_sphere(void *data, Evas_Object *obj, void *event_info);
53void test_win_resize(void *data, Evas_Object *obj, void *event_info);
54
55static const EPhysics_Test tests[] = {
56 {"Bouncing 3D", test_bouncing_3d},
57 {"Bouncing Ball", test_bouncing_ball},
58 {"Bouncing Text", test_bouncing_text},
59 {"Camera", test_camera},
60 {"Camera Track", test_camera_track},
61 {"Colliding Balls", test_colliding_balls},
62 {"Collision Detection", test_collision},
63 {"Collision Filter", test_collision_filter},
64 {"Collision High Speed", test_collision_speed},
65 {"Constraint", test_constraint},
66 {"Cube 3D", test_cube_3d},
67 {"Dices", test_dices},
68 {"Delete Body", test_delete},
69 {"Falling Letters", test_falling_letters},
70 {"Flag - Cloth", test_flag},
71 {"Flip Page", test_flip_page},
72 {"Forces", test_forces},
73 {"Grab", test_grab},
74 {"Growing Balls", test_growing_balls},
75 {"Heavy", test_heavy},
76 {"Hinge", test_hinge},
77 {"Jumping Balls", test_jumping_balls},
78 {"Light", test_light},
79 {"Material", test_material},
80 {"No Gravity", test_no_gravity},
81 {"Raster's Coins", test_coins},
82 {"Rotate", test_rotate},
83 {"Rotating Forever", test_rotating_forever},
84 {"Velocity", test_velocity},
85 {"Shapes", test_shapes},
86 {"Sleeping Threshold", test_sleeping},
87 {"Slider", test_slider},
88 {"Soft Body", test_soft_body},
89 {"Soft Button", test_soft_button},
90 {"Soft Sphere", test_soft_sphere},
91 {"Win Resize", test_win_resize},
92};
93
94static void
95_win_del(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
96{
97 elm_exit();
98}
99
100static void
101_subwin_del_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
102{
103 test_data_del(data);
104 ephysics_shutdown();
105}
106
107void
108update_object_cb(void *data, EPhysics_Body *body, void *event_info)
109{
110 Evas_Object *sphere = event_info;
111 Evas_Object *shadow = data;
112 Edje_Message_Float msg;
113 Evas_Object *edje;
114 int x, y, w, h;
115
116 ephysics_body_evas_object_update(body);
117 evas_object_geometry_get(sphere, &x, &y, &w, &h);
118
119 if (y > SH_THRESHOLD)
120 {
121 int sh_w;
122
123 msg.val = (double) (y - SH_THRESHOLD) / (FLOOR_Y - SH_THRESHOLD - h);
124 sh_w = (1 + 2 * msg.val) * w / 3;
125 evas_object_resize(shadow, sh_w, 3);
126 evas_object_move(shadow, x + (w - sh_w) / 2, FLOOR_Y);
127 }
128 else
129 msg.val = 0;
130
131 edje = elm_layout_edje_get(shadow);
132 edje_object_message_send(edje, EDJE_MESSAGE_FLOAT, SHADOW_ALPHA_ID, &msg);
133}
134
135void
136test_clean(Test_Data *test_data)
137{
138 EPhysics_Constraint *constraint;
139 Evas_Object *evas_obj;
140 EPhysics_Body *body;
141
142 EINA_LIST_FREE(test_data->constraints, constraint)
143 ephysics_constraint_del(constraint);
144
145 EINA_LIST_FREE(test_data->bodies, body)
146 ephysics_body_del(body);
147
148 EINA_LIST_FREE(test_data->evas_objs, evas_obj)
149 evas_object_del(evas_obj);
150}
151
152void
153test_data_del(Test_Data *test_data)
154{
155 Evas_Object *evas_obj;
156 EINA_LIST_FREE(test_data->evas_objs, evas_obj)
157 evas_object_del(evas_obj);
158
159 eina_list_free(test_data->bodies);
160 eina_list_free(test_data->constraints);
161
162 evas_object_del(test_data->layout);
163 ephysics_world_del(test_data->world);
164 free(test_data);
165}
166
167Test_Data *
168test_data_new(void)
169{
170 Test_Data *test_data;
171
172 test_data = calloc(1, sizeof(Test_Data));
173 if (!test_data)
174 ERR("Failed to create test data");
175
176 return test_data;
177}
178
179void
180test_win_add(Test_Data *test_data, const char *title, Eina_Bool autodel)
181{
182 Evas_Object *win, *ly;
183
184 win = elm_win_add(NULL, title, ELM_WIN_BASIC);
185 elm_win_title_set(win, title);
186 elm_win_autodel_set(win, EINA_TRUE);
187 evas_object_show(win);
188 test_data->win = win;
189 if (autodel)
190 evas_object_event_callback_add(win, EVAS_CALLBACK_DEL,
191 _subwin_del_cb, test_data);
192
193 ly = elm_layout_add(win);
194 elm_win_resize_object_add(win, ly);
195 evas_object_size_hint_min_set(ly, WIDTH, HEIGHT);
196 evas_object_size_hint_max_set(ly, WIDTH, HEIGHT);
197 elm_layout_file_set(ly, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
198 "frame");
199 evas_object_show(ly);
200 test_data->layout = ly;
201}
202
203static void
204_test_run(char *autorun)
205{
206 unsigned int i;
207
208 for (i = 0; i < EINA_C_ARRAY_LENGTH(tests); i++)
209 {
210 EPhysics_Test test = tests[i];
211 if (!strcmp(test.name, autorun))
212 {
213 test.func(NULL, NULL, NULL);
214 return;
215 }
216 }
217}
218
219static void
220_main_win_add(char *autorun)
221{
222 Elm_Object_Item *selected = NULL;
223 Evas_Object *win, *list;
224 unsigned int i;
225
226 win = elm_win_add(NULL, "main", ELM_WIN_BASIC);
227 elm_win_title_set(win, "EPhysics Tests");
228 evas_object_smart_callback_add(win, "delete,request", _win_del, NULL);
229 elm_win_autodel_set(win, EINA_TRUE);
230 evas_object_resize(win, 460, 560);
231 evas_object_show(win);
232
233 list = elm_list_add(win);
234 elm_object_style_set(list, "ephysics-test");
235 elm_list_select_mode_set(list, ELM_OBJECT_SELECT_MODE_ALWAYS);
236 evas_object_size_hint_weight_set(list, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
237 elm_win_resize_object_add(win, list);
238 evas_object_show(list);
239
240 for (i = 0; i < EINA_C_ARRAY_LENGTH(tests); i++)
241 {
242 Elm_Object_Item *item;
243 EPhysics_Test test;
244
245 test = tests[i];
246 item = elm_list_item_append(list, test.name, NULL, NULL, test.func,
247 NULL);
248
249 if ((autorun) && (!strcmp(test.name, autorun)))
250 selected = item;
251 }
252
253 elm_list_go(list);
254
255 if (selected)
256 elm_list_item_bring_in(selected);
257
258 if (autorun)
259 _test_run(autorun);
260}
261
262EAPI_MAIN int
263elm_main(int argc, char **argv)
264{
265 Eina_Bool test_win_only = EINA_FALSE;
266 char *autorun = NULL;
267
268 _ephysics_test_log_dom = eina_log_domain_register(
269 "ephysics-test", EPHYSICS_TEST_LOG_COLOR);
270 if (_ephysics_test_log_dom < 0)
271 {
272 EINA_LOG_CRIT("Could not register log domain: ephysics-test");
273 return -1;
274 }
275
276 elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
277 elm_policy_set(ELM_POLICY_EXIT, ELM_POLICY_EXIT_WINDOWS_DEL);
278
279 elm_app_compile_bin_dir_set(PACKAGE_BIN_DIR);
280 elm_app_compile_data_dir_set(PACKAGE_DATA_DIR);
281
282 /* if called with a single argument try to autorun a test with
283 * the same name as the given param
284 * ex: ephysics_test "Jumping Balls" */
285 if (argc == 2)
286 autorun = argv[1];
287 else if (argc == 3)
288 {
289 if ((!strcmp(argv[1], "--test-win-only")) ||
290 (!strcmp(argv[1], "-to")))
291 {
292 test_win_only = EINA_TRUE;
293 autorun = argv[2];
294 }
295 }
296
297 elm_theme_extension_add(NULL,
298 PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj");
299
300 if (test_win_only)
301 _test_run(autorun);
302 else
303 _main_win_add(autorun);
304
305 elm_run();
306
307 eina_log_domain_unregister(_ephysics_test_log_dom);
308 _ephysics_test_log_dom = -1;
309
310 elm_shutdown();
311 return 0;
312}
313ELM_MAIN()
diff --git a/src/examples/ephysics/test_bouncing_ball.c b/src/examples/ephysics/test_bouncing_ball.c
new file mode 100644
index 0000000..00736b0
--- /dev/null
+++ b/src/examples/ephysics/test_bouncing_ball.c
@@ -0,0 +1,112 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7static Eina_Bool
8_on_keydown(void *data, Evas_Object *obj __UNUSED__, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
9{
10 Evas_Event_Key_Down *ev = event_info;
11 EPhysics_Body *body = data;
12
13 if (type != EVAS_CALLBACK_KEY_UP)
14 return EINA_FALSE;
15
16 if (strcmp(ev->keyname, "Up") == 0)
17 ephysics_body_central_impulse_apply(body, 0, -300, 0);
18 else if (strcmp(ev->keyname, "Down") == 0)
19 ephysics_body_central_impulse_apply(body, 0, 300, 0);
20 else if (strcmp(ev->keyname, "Right") == 0)
21 ephysics_body_central_impulse_apply(body, 300, 0, 0);
22 else if (strcmp(ev->keyname, "Left") == 0)
23 ephysics_body_central_impulse_apply(body, -300, 0, 0);
24
25 return EINA_TRUE;
26}
27
28static void
29_world_populate(Test_Data *test_data)
30{
31 Evas_Object *sphere, *shadow;
32 EPhysics_Body *fall_body;
33
34 shadow = elm_layout_add(test_data->win);
35 elm_layout_file_set(
36 shadow, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-ball");
37 evas_object_move(shadow, WIDTH / 3, FLOOR_Y);
38 evas_object_resize(shadow, 70, 3);
39 evas_object_show(shadow);
40 test_data->evas_objs = eina_list_append(test_data->evas_objs, shadow);
41
42 sphere = elm_image_add(test_data->win);
43 elm_image_file_set(
44 sphere, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "big-blue-ball");
45 evas_object_move(sphere, WIDTH / 3, HEIGHT / 8);
46 evas_object_resize(sphere, 70, 70);
47 evas_object_show(sphere);
48 test_data->evas_objs = eina_list_append(test_data->evas_objs, sphere);
49
50 fall_body = ephysics_body_sphere_add(test_data->world);
51 ephysics_body_evas_object_set(fall_body, sphere, EINA_TRUE);
52 ephysics_body_restitution_set(fall_body, 0.95);
53 ephysics_body_friction_set(fall_body, 0.1);
54 ephysics_body_event_callback_add(fall_body, EPHYSICS_CALLBACK_BODY_UPDATE,
55 update_object_cb, shadow);
56 test_data->bodies = eina_list_append(test_data->bodies, fall_body);
57 test_data->data = fall_body;
58}
59
60static void
61_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
62{
63 Test_Data *test_data = data;
64
65 DBG("Restart pressed");
66 elm_object_event_callback_del(test_data->win, _on_keydown, test_data->data);
67 test_clean(test_data);
68 _world_populate(test_data);
69 elm_object_event_callback_add(test_data->win, _on_keydown, test_data->data);
70}
71
72void
73test_bouncing_ball(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
74{
75 EPhysics_Body *boundary;
76 EPhysics_World *world;
77 Test_Data *test_data;
78
79 if (!ephysics_init())
80 return;
81
82 test_data = test_data_new();
83 test_win_add(test_data, "Bouncing Ball", EINA_TRUE);
84
85 elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme",
86 _restart, test_data);
87 elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test");
88 elm_object_signal_emit(test_data->layout, "arrows,show", "ephysics_test");
89
90 world = ephysics_world_new();
91 ephysics_world_light_all_bodies_set(world, EINA_TRUE);
92 ephysics_world_render_geometry_set(world, 50, 40, -50,
93 WIDTH - 100, FLOOR_Y - 40, DEPTH);
94 test_data->world = world;
95
96 boundary = ephysics_body_bottom_boundary_add(test_data->world);
97 ephysics_body_restitution_set(boundary, 0.65);
98 ephysics_body_friction_set(boundary, 4);
99
100 boundary = ephysics_body_right_boundary_add(test_data->world);
101 ephysics_body_restitution_set(boundary, 0.4);
102 ephysics_body_friction_set(boundary, 3);
103
104 boundary = ephysics_body_left_boundary_add(test_data->world);
105 ephysics_body_restitution_set(boundary, 0.4);
106 ephysics_body_friction_set(boundary, 3);
107
108 ephysics_body_top_boundary_add(test_data->world);
109
110 _world_populate(test_data);
111 elm_object_event_callback_add(test_data->win, _on_keydown, test_data->data);
112}
diff --git a/src/examples/ephysics/test_bouncing_text.c b/src/examples/ephysics/test_bouncing_text.c
new file mode 100644
index 0000000..c2d7d09
--- /dev/null
+++ b/src/examples/ephysics/test_bouncing_text.c
@@ -0,0 +1,67 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7static void
8_world_populate(Test_Data *test_data)
9{
10 EPhysics_Body *text_body;
11 Evas_Object *text;
12 int x, y, w;
13
14 text = evas_object_text_add(evas_object_evas_get(test_data->win));
15 evas_object_text_text_set(text, "EPHYSICS");
16 evas_object_text_font_set(text, "Sans", 54);
17 evas_object_color_set(text, 95, 56, 19, 255);
18 evas_object_move(text, WIDTH / 4, HEIGHT / 8);
19 evas_object_show(text);
20 evas_object_geometry_get(text, &x, &y, &w, NULL);
21 test_data->evas_objs = eina_list_append(test_data->evas_objs, text);
22
23 text_body = ephysics_body_box_add(test_data->world);
24 ephysics_body_evas_object_set(text_body, text, EINA_FALSE);
25 ephysics_body_geometry_set(text_body, x, y, -15, w * 5 / 6, 46, 30);
26 ephysics_body_restitution_set(text_body, 0.95);
27 ephysics_body_friction_set(text_body, 0.1);
28 test_data->bodies = eina_list_append(test_data->bodies, text_body);
29}
30
31static void
32_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
33{
34 Test_Data *test_data = data;
35
36 DBG("Restart pressed");
37 test_clean(test_data);
38 _world_populate(test_data);
39}
40
41void
42test_bouncing_text(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
43{
44 EPhysics_Body *boundary;
45 EPhysics_World *world;
46 Test_Data *test_data;
47
48 if (!ephysics_init())
49 return;
50
51 test_data = test_data_new();
52 test_win_add(test_data, "Bouncing Text", EINA_TRUE);
53 elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test");
54 elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme",
55 _restart, test_data);
56
57 world = ephysics_world_new();
58 ephysics_world_render_geometry_set(world, 50, 40, -50,
59 WIDTH - 100, FLOOR_Y - 40, 100);
60 test_data->world = world;
61
62 boundary = ephysics_body_bottom_boundary_add(test_data->world);
63 ephysics_body_restitution_set(boundary, 0.65);
64 ephysics_body_friction_set(boundary, 2);
65
66 _world_populate(test_data);
67}
diff --git a/src/examples/ephysics/test_camera.c b/src/examples/ephysics/test_camera.c
new file mode 100644
index 0000000..8dc1dcc
--- /dev/null
+++ b/src/examples/ephysics/test_camera.c
@@ -0,0 +1,236 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7typedef struct _Camera_Data Camera_Data;
8
9struct _Camera_Data {
10 Test_Data base;
11 Ecore_Animator *animator;
12 int old_x;
13};
14
15static void
16_update_floor(Evas_Object *floor_obj, Evas_Coord delta)
17{
18 int fx, x, y;
19
20 evas_object_geometry_get(floor_obj, &x, &y, NULL, NULL);
21 fx = x + delta;
22 if (fx < -FLOOR_WIDTH)
23 fx += 2 * FLOOR_WIDTH;
24 evas_object_move(floor_obj, fx, y);
25}
26
27static void
28_camera_moved_cb(void *data, EPhysics_World *world __UNUSED__, void *event_info)
29{
30 EPhysics_Camera *camera = event_info;
31 Camera_Data *camera_data = data;
32 Evas_Object *floor_obj;
33 int x;
34
35 DBG("Camera moved");
36
37 ephysics_camera_position_get(camera, &x, NULL);
38
39 floor_obj = evas_object_data_get(camera_data->base.layout, "floor");
40 _update_floor(floor_obj, camera_data->old_x - x);
41
42 floor_obj = evas_object_data_get(camera_data->base.layout, "floor2");
43 _update_floor(floor_obj, camera_data->old_x - x);
44
45 camera_data->old_x = x;
46}
47
48static Eina_Bool
49_camera_move_cb(void *data)
50{
51 Camera_Data *camera_data = data;
52 EPhysics_Camera *camera;
53 int x, y, w;
54
55 ephysics_world_render_geometry_get(camera_data->base.world,
56 NULL, NULL, NULL, &w, NULL, NULL);
57
58 camera = ephysics_world_camera_get(camera_data->base.world);
59 ephysics_camera_position_get(camera, &x, &y);
60
61 if (x + w > WIDTH * 2)
62 {
63 camera_data->animator = NULL;
64 return EINA_FALSE;
65 }
66
67 x += 2;
68 ephysics_camera_position_set(camera, x, y);
69
70 return EINA_TRUE;
71}
72
73static void
74_world_populate(Camera_Data *camera_data)
75{
76 Evas_Object *sphere, *shadow;
77 EPhysics_Body *fall_body;
78
79 shadow = elm_layout_add(camera_data->base.win);
80 elm_layout_file_set(
81 shadow, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-ball");
82 evas_object_move(shadow, WIDTH / 3, FLOOR_Y);
83 evas_object_resize(shadow, 70, 3);
84 evas_object_show(shadow);
85 camera_data->base.evas_objs = eina_list_append(camera_data->base.evas_objs,
86 shadow);
87
88 sphere = elm_image_add(camera_data->base.win);
89 elm_image_file_set(
90 sphere, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
91 "big-blue-ball");
92 evas_object_move(sphere, WIDTH / 3, FLOOR_Y - 70);
93 evas_object_resize(sphere, 70, 70);
94 evas_object_show(sphere);
95 camera_data->base.evas_objs = eina_list_append(camera_data->base.evas_objs,
96 sphere);
97
98 fall_body = ephysics_body_sphere_add(camera_data->base.world);
99 ephysics_body_evas_object_set(fall_body, sphere, EINA_TRUE);
100 ephysics_body_restitution_set(fall_body, 0.2);
101 ephysics_body_friction_set(fall_body, 1);
102 ephysics_body_event_callback_add(fall_body, EPHYSICS_CALLBACK_BODY_UPDATE,
103 update_object_cb, shadow);
104 camera_data->base.bodies = eina_list_append(camera_data->base.bodies,
105 fall_body);
106 ephysics_body_central_impulse_apply(fall_body, 300, 0, 0);
107
108 shadow = elm_layout_add(camera_data->base.win);
109 elm_layout_file_set(
110 shadow, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-ball");
111 evas_object_move(shadow, WIDTH * 4 / 3, FLOOR_Y);
112 evas_object_resize(shadow, 70, 3);
113 evas_object_show(shadow);
114 camera_data->base.evas_objs = eina_list_append(camera_data->base.evas_objs,
115 shadow);
116
117 sphere = elm_image_add(camera_data->base.win);
118 elm_image_file_set(
119 sphere, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "big-red-ball");
120 evas_object_move(sphere, WIDTH * 4 / 3, FLOOR_Y - 70);
121 evas_object_resize(sphere, 70, 70);
122 evas_object_show(sphere);
123 camera_data->base.evas_objs = eina_list_append(camera_data->base.evas_objs,
124 sphere);
125
126 fall_body = ephysics_body_sphere_add(camera_data->base.world);
127 ephysics_body_mass_set(fall_body, 4.6);
128 ephysics_body_evas_object_set(fall_body, sphere, EINA_TRUE);
129 ephysics_body_restitution_set(fall_body, 0.12);
130 ephysics_body_friction_set(fall_body, 2);
131 ephysics_body_event_callback_add(fall_body, EPHYSICS_CALLBACK_BODY_UPDATE,
132 update_object_cb, shadow);
133 camera_data->base.bodies = eina_list_append(camera_data->base.bodies,
134 fall_body);
135
136 camera_data->animator = ecore_animator_add(_camera_move_cb, camera_data);
137}
138
139static void
140_win_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
141{
142 Camera_Data *camera_data = data;
143
144 if (camera_data->animator)
145 ecore_animator_del(camera_data->animator);
146
147 test_clean((Test_Data *)camera_data);
148 evas_object_del(camera_data->base.layout);
149 ephysics_world_del(camera_data->base.world);
150 free(camera_data);
151 ephysics_shutdown();
152}
153
154static void
155_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
156{
157 Camera_Data *camera_data = data;
158 EPhysics_Camera *camera;
159 Evas_Object *floor_obj;
160
161 DBG("Restart pressed");
162
163 if (camera_data->animator)
164 ecore_animator_del(camera_data->animator);
165
166 camera = ephysics_world_camera_get(camera_data->base.world);
167 ephysics_camera_position_set(camera, 50, 40);
168
169 camera_data->old_x = 0;
170 floor_obj = evas_object_data_get(camera_data->base.layout, "floor");
171 evas_object_move(floor_obj, 0, FLOOR_Y - 20);
172 floor_obj = evas_object_data_get(camera_data->base.layout, "floor2");
173 evas_object_move(floor_obj, FLOOR_WIDTH, FLOOR_Y - 20);
174
175 test_clean((Test_Data *)camera_data);
176 _world_populate(camera_data);
177}
178
179void
180test_camera(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
181{
182 Camera_Data *camera_data;
183 EPhysics_Body *boundary;
184 Evas_Object *floor_obj;
185 EPhysics_World *world;
186
187 if (!ephysics_init())
188 return;
189
190 camera_data = calloc(1, sizeof(Camera_Data));
191 if (!camera_data)
192 {
193 ERR("Failed to create test data");
194 ephysics_shutdown();
195 return;
196 }
197
198 test_win_add((Test_Data *)camera_data, "Camera", EINA_FALSE);
199 evas_object_event_callback_add(camera_data->base.win, EVAS_CALLBACK_DEL,
200 _win_del, camera_data);
201
202 elm_layout_signal_callback_add(camera_data->base.layout, "restart",
203 "test-theme", _restart, camera_data);
204
205 floor_obj = elm_image_add(camera_data->base.win);
206 elm_image_file_set(
207 floor_obj, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "floor");
208 evas_object_move(floor_obj, 0, FLOOR_Y - 20);
209 evas_object_resize(floor_obj, FLOOR_WIDTH, 224);
210 evas_object_show(floor_obj);
211 evas_object_data_set(camera_data->base.layout, "floor", floor_obj);
212
213 floor_obj = elm_image_add(camera_data->base.win);
214 elm_image_file_set(
215 floor_obj, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "floor");
216 evas_object_move(floor_obj, FLOOR_WIDTH, FLOOR_Y - 20);
217 evas_object_resize(floor_obj, FLOOR_WIDTH, 224);
218 evas_object_show(floor_obj);
219 evas_object_data_set(camera_data->base.layout, "floor2", floor_obj);
220
221 world = ephysics_world_new();
222 ephysics_world_render_geometry_set(world, 50, 40, -50,
223 WIDTH - 100, FLOOR_Y - 40, DEPTH);
224 ephysics_world_event_callback_add(world,
225 EPHYSICS_CALLBACK_WORLD_CAMERA_MOVED,
226 _camera_moved_cb, camera_data);
227 camera_data->base.world = world;
228
229 boundary = ephysics_body_box_add(camera_data->base.world);
230 ephysics_body_mass_set(boundary, 0);
231 ephysics_body_geometry_set(boundary, 0, FLOOR_Y, -15, WIDTH * 2, 10, 30);
232 ephysics_body_restitution_set(boundary, 0.65);
233 ephysics_body_friction_set(boundary, 4);
234
235 _world_populate(camera_data);
236}
diff --git a/src/examples/ephysics/test_camera_track.c b/src/examples/ephysics/test_camera_track.c
new file mode 100644
index 0000000..443ec3f
--- /dev/null
+++ b/src/examples/ephysics/test_camera_track.c
@@ -0,0 +1,248 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7typedef struct _Track_Data Track_Data;
8
9struct _Track_Data {
10 Test_Data base;
11 EPhysics_Body *body;
12 Evas_Object *sp;
13 int old_cx;
14};
15
16static void
17_update_floor(Evas_Object *layout, const char *name, int delta_x, int cy, Eina_Bool ver)
18{
19 Evas_Object *floor_obj;
20 int x, y, fx, fy;
21
22 floor_obj = evas_object_data_get(layout, name);
23 evas_object_geometry_get(floor_obj, &x, &y, NULL, NULL);
24 fx = x + delta_x;
25 if (fx < -FLOOR_WIDTH)
26 fx += 2 * FLOOR_WIDTH;
27
28 fy = (ver) ? FLOOR_Y - 20 - cy + 40 : y;
29 evas_object_move(floor_obj, fx, fy);
30}
31
32static void
33_camera_moved_cb(void *data, EPhysics_World *world __UNUSED__, void *event_info)
34{
35 EPhysics_Camera *camera = event_info;
36 Track_Data *track_data = data;
37 int cx, cy, delta_x = 0;
38 Eina_Bool hor, ver;
39
40 DBG("Camera moved");
41
42 ephysics_camera_tracked_body_get(camera, NULL, &hor, &ver);
43 ephysics_camera_position_get(camera, &cx, &cy);
44
45 if (hor)
46 delta_x = track_data->old_cx - cx;
47
48 _update_floor(track_data->base.layout, "floor", delta_x, cy, ver);
49 _update_floor(track_data->base.layout, "floor2", delta_x, cy, ver);
50
51 track_data->old_cx = cx;
52}
53
54static void
55_track_apply(Track_Data *track_data)
56{
57 Eina_Bool hor = EINA_FALSE;
58 Eina_Bool ver = EINA_FALSE;
59 EPhysics_Camera *camera;
60 EPhysics_Body *body;
61 int mode;
62
63 body = track_data->body;
64 camera = ephysics_world_camera_get(track_data->base.world);
65 mode = (int) elm_spinner_value_get(track_data->sp);
66
67 switch (mode)
68 {
69 case 1:
70 hor = EINA_TRUE;
71 break;
72 case 3:
73 hor = EINA_TRUE;
74 case 2:
75 ver = EINA_TRUE;
76 }
77
78 INF("Tracking mode: hor = %i, ver = %i", hor, ver);
79 ephysics_camera_body_track(camera, body, hor, ver);
80}
81
82static void
83_tracking_mode_cb(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
84{
85 _track_apply(data);
86}
87
88static void
89_world_populate(Track_Data *track_data)
90{
91 static const char *colors[] = {"blue-cube", "purple-cube"};
92 Evas_Object *cube, *sphere;
93 EPhysics_Body *body;
94 int i, color, row;
95
96 for (i = 0; i < 9; i++)
97 {
98 color = i % 3 % 2;
99 row = i / 3;
100
101 cube = elm_image_add(track_data->base.win);
102 elm_image_file_set(
103 cube, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
104 colors[color]);
105 evas_object_move(cube, i * 70, (row + 2) * 70);
106 evas_object_resize(cube, 70, 70);
107 evas_object_show(cube);
108 track_data->base.evas_objs = eina_list_append(
109 track_data->base.evas_objs, cube);
110
111 body = ephysics_body_box_add(track_data->base.world);
112 ephysics_body_evas_object_set(body, cube, EINA_TRUE);
113 ephysics_body_restitution_set(body, 0.95);
114 ephysics_body_friction_set(body, 1);
115 ephysics_body_mass_set(body, 0);
116 track_data->base.bodies = eina_list_append(
117 track_data->base.bodies, body);
118 }
119
120 sphere = elm_image_add(track_data->base.win);
121 elm_image_file_set(
122 sphere, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "green-ball");
123 evas_object_move(sphere, 0, 80);
124 evas_object_resize(sphere, 54, 54);
125 evas_object_show(sphere);
126 track_data->base.evas_objs = eina_list_append(track_data->base.evas_objs,
127 sphere);
128
129 body = ephysics_body_sphere_add(track_data->base.world);
130 ephysics_body_evas_object_set(body, sphere, EINA_TRUE);
131 ephysics_body_restitution_set(body, 0.95);
132 ephysics_body_friction_set(body, 1);
133 ephysics_body_damping_set(body, 0.1, 0);
134 ephysics_body_central_impulse_apply(body, 90, 0, 0);
135 track_data->body = body;
136 track_data->base.bodies = eina_list_append(track_data->base.bodies, body);
137}
138
139static void
140_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
141{
142 Track_Data *track_data = data;
143 EPhysics_Camera *camera;
144 Evas_Object *floor_obj;
145
146 DBG("Restart pressed");
147
148 test_clean((Test_Data *)track_data);
149 _world_populate(track_data);
150
151 floor_obj = evas_object_data_get(track_data->base.layout, "floor");
152 evas_object_move(floor_obj, - WIDTH / 2, FLOOR_Y - 20);
153 floor_obj = evas_object_data_get(track_data->base.layout, "floor2");
154 evas_object_move(floor_obj, FLOOR_WIDTH - WIDTH / 2, FLOOR_Y - 20);
155
156 camera = ephysics_world_camera_get(track_data->base.world);
157 ephysics_camera_position_set(camera, 50, 40);
158 track_data->old_cx = 50;
159
160 _track_apply(track_data);
161}
162
163static void
164_win_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
165{
166 Track_Data *track_data = data;
167
168 test_clean((Test_Data *)track_data);
169 evas_object_del(track_data->base.layout);
170 ephysics_world_del(track_data->base.world);
171 free(track_data);
172 ephysics_shutdown();
173}
174
175void
176test_camera_track(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
177{
178 EPhysics_Body *boundary;
179 Evas_Object *sp, *floor_obj;
180 Track_Data *track_data;
181 EPhysics_World *world;
182
183 if (!ephysics_init())
184 return;
185
186 track_data = calloc(1, sizeof(Track_Data));
187 if (!track_data)
188 {
189 ERR("Failed to create test data");
190 ephysics_shutdown();
191 return;
192 }
193
194 test_win_add((Test_Data *) track_data, "Camera Track", EINA_FALSE);
195 evas_object_event_callback_add(track_data->base.win, EVAS_CALLBACK_DEL,
196 _win_del, track_data);
197
198 elm_layout_signal_callback_add(track_data->base.layout, "restart",
199 "test-theme", _restart, track_data);
200
201 sp = elm_spinner_add(track_data->base.win);
202 elm_spinner_min_max_set(sp, 0, 3);
203 elm_spinner_step_set(sp, 1);
204 elm_spinner_wrap_set(sp, EINA_TRUE);
205 elm_spinner_special_value_add(sp, 0, "No tracking");
206 elm_spinner_special_value_add(sp, 1, "Horizontal tracking");
207 elm_spinner_special_value_add(sp, 2, "Vertical tracking");
208 elm_spinner_special_value_add(sp, 3, "Full tracking");
209 elm_spinner_editable_set(sp, EINA_FALSE);
210 elm_object_style_set(sp, "ephysics-test");
211 evas_object_smart_callback_add(sp, "delay,changed", _tracking_mode_cb,
212 track_data);
213 elm_layout_content_set(track_data->base.layout, "extra_input", sp);
214 track_data->sp = sp;
215
216 floor_obj = elm_image_add(track_data->base.win);
217 elm_image_file_set(
218 floor_obj, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "floor");
219 evas_object_move(floor_obj, - WIDTH / 2, FLOOR_Y - 20);
220 evas_object_resize(floor_obj, FLOOR_WIDTH, 224);
221 evas_object_show(floor_obj);
222 evas_object_data_set(track_data->base.layout, "floor", floor_obj);
223
224 floor_obj = elm_image_add(track_data->base.win);
225 elm_image_file_set(
226 floor_obj, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "floor");
227 evas_object_move(floor_obj, FLOOR_WIDTH - WIDTH / 2, FLOOR_Y - 20);
228 evas_object_resize(floor_obj, FLOOR_WIDTH, 224);
229 evas_object_show(floor_obj);
230 evas_object_data_set(track_data->base.layout, "floor2", floor_obj);
231
232 world = ephysics_world_new();
233 ephysics_world_render_geometry_set(world, 50, 40, -50,
234 WIDTH - 100, FLOOR_Y - 40, DEPTH);
235 track_data->base.world = world;
236 ephysics_world_event_callback_add(world,
237 EPHYSICS_CALLBACK_WORLD_CAMERA_MOVED,
238 _camera_moved_cb, track_data);
239
240 boundary = ephysics_body_box_add(track_data->base.world);
241 ephysics_body_mass_set(boundary, 0);
242 ephysics_body_geometry_set(boundary, 0, FLOOR_Y, -15, WIDTH * 4, 10, 30);
243 ephysics_body_restitution_set(boundary, 0.65);
244 ephysics_body_friction_set(boundary, 4);
245
246 track_data->old_cx = 50;
247 _world_populate(track_data);
248}
diff --git a/src/examples/ephysics/test_colliding_balls.c b/src/examples/ephysics/test_colliding_balls.c
new file mode 100644
index 0000000..6de20f3
--- /dev/null
+++ b/src/examples/ephysics/test_colliding_balls.c
@@ -0,0 +1,90 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7static void
8_world_populate(Test_Data *test_data)
9{
10 static const char *colors[] = {"blue-ball", "red-ball", "green-ball"};
11 Evas_Object *sphere, *shadow;
12 EPhysics_Body *fall_body;
13 int i, column, row;
14
15 for (i = 0; i < 9; i++)
16 {
17 column = i % 3;
18 row = i / 3;
19
20 shadow = elm_layout_add(test_data->win);
21 elm_layout_file_set(
22 shadow, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
23 "shadow-ball");
24 evas_object_move(shadow, (1 + column) * WIDTH / 4, FLOOR_Y);
25 evas_object_resize(shadow, 54, 3);
26 evas_object_show(shadow);
27 test_data->evas_objs = eina_list_append(test_data->evas_objs, shadow);
28
29 sphere = elm_image_add(test_data->win);
30 elm_image_file_set(
31 sphere, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
32 colors[column]);
33 evas_object_move(sphere, (1 + column) * WIDTH / 4,
34 100 + row * 60 + row / 2 * 20);
35 evas_object_resize(sphere, 54, 54);
36 evas_object_show(sphere);
37 test_data->evas_objs = eina_list_append(test_data->evas_objs, sphere);
38
39 fall_body = ephysics_body_sphere_add(test_data->world);
40 ephysics_body_evas_object_set(fall_body, sphere, EINA_TRUE);
41 ephysics_body_event_callback_add(fall_body,
42 EPHYSICS_CALLBACK_BODY_UPDATE,
43 update_object_cb, shadow);
44 ephysics_body_restitution_set(fall_body, 0.95);
45 ephysics_body_friction_set(fall_body, 1);
46 test_data->bodies = eina_list_append(test_data->bodies, fall_body);
47 }
48}
49
50static void
51_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
52{
53 Test_Data *test_data = data;
54
55 DBG("Restart pressed");
56 test_clean(test_data);
57 _world_populate(test_data);
58}
59
60void
61test_colliding_balls(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
62{
63 EPhysics_Body *boundary;
64 EPhysics_World *world;
65 Test_Data *test_data;
66
67 if (!ephysics_init())
68 return;
69
70 test_data = test_data_new();
71 test_win_add(test_data, "Colliding Balls", EINA_TRUE);
72 elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test");
73 elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme",
74 _restart, test_data);
75
76 world = ephysics_world_new();
77 ephysics_world_render_geometry_set(world, 50, 40, -50,
78 WIDTH - 100, FLOOR_Y - 40, DEPTH);
79 test_data->world = world;
80
81 boundary = ephysics_body_bottom_boundary_add(test_data->world);
82 ephysics_body_restitution_set(boundary, 0.65);
83 ephysics_body_friction_set(boundary, 2);
84
85 ephysics_body_top_boundary_add(test_data->world);
86 ephysics_body_left_boundary_add(test_data->world);
87 ephysics_body_right_boundary_add(test_data->world);
88
89 _world_populate(test_data);
90}
diff --git a/src/examples/ephysics/test_collision_detection.c b/src/examples/ephysics/test_collision_detection.c
new file mode 100644
index 0000000..5e89163
--- /dev/null
+++ b/src/examples/ephysics/test_collision_detection.c
@@ -0,0 +1,187 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7typedef struct _Collision_Data Collision_Data;
8
9struct _Collision_Data {
10 Test_Data base;
11 Evas_Object *impact;
12 EPhysics_Body *sphere;
13 EPhysics_Body *sphere2;
14};
15
16static void
17_collision_cb(void *data, EPhysics_Body *body __UNUSED__, void *event_info)
18{
19 EPhysics_Body *contact_body;
20 Collision_Data *collision_data = data;
21 EPhysics_Body_Collision *collision = event_info;
22 int x, y, z;
23
24 contact_body = ephysics_body_collision_contact_body_get(collision);
25 if (contact_body != collision_data->sphere2) return;
26
27 ephysics_body_collision_position_get(collision, &x, &y, &z);
28
29 evas_object_move(collision_data->impact, x - 10, y - 40);
30 elm_object_signal_emit(collision_data->impact, "impact,show",
31 "ephysics_test");
32
33 INF("Collision Detected at (%i, %i, %i)", x, y, z);
34}
35
36static void
37_world_populate(Collision_Data *collision_data)
38{
39 Evas_Object *sphere1, *sphere2, *sh1, *sh2, *impact;
40 EPhysics_Body *sphere_body1, *sphere_body2;
41
42 sh1 = elm_layout_add(collision_data->base.win);
43 elm_layout_file_set(
44 sh1, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-ball");
45 evas_object_move(sh1, WIDTH / 2, FLOOR_Y);
46 evas_object_resize(sh1, 70, 3);
47 evas_object_show(sh1);
48 collision_data->base.evas_objs = eina_list_append(
49 collision_data->base.evas_objs, sh1);
50
51 sphere1 = elm_image_add(collision_data->base.win);
52 elm_image_file_set(
53 sphere1, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
54 "big-blue-ball");
55 evas_object_move(sphere1, WIDTH / 2, FLOOR_Y - 70 + 1);
56 evas_object_resize(sphere1, 70, 70);
57 evas_object_show(sphere1);
58 collision_data->base.evas_objs = eina_list_append(
59 collision_data->base.evas_objs, sphere1);
60
61 sphere_body1 = ephysics_body_sphere_add(collision_data->base.world);
62 ephysics_body_evas_object_set(sphere_body1, sphere1, EINA_TRUE);
63 ephysics_body_event_callback_add(sphere_body1,
64 EPHYSICS_CALLBACK_BODY_UPDATE,
65 update_object_cb, sh1);
66 ephysics_body_restitution_set(sphere_body1, 0.8);
67 ephysics_body_friction_set(sphere_body1, 0.4);
68 collision_data->base.bodies = eina_list_append(
69 collision_data->base.bodies, sphere_body1);
70 collision_data->sphere = sphere_body1;
71
72 sh2 = elm_layout_add(collision_data->base.win);
73 elm_layout_file_set(
74 sh2, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-ball");
75 evas_object_move(sh2, WIDTH / 8, FLOOR_Y);
76 evas_object_resize(sh2, 70, 3);
77 evas_object_show(sh2);
78 collision_data->base.evas_objs = eina_list_append(
79 collision_data->base.evas_objs, sh2);
80
81 sphere2 = elm_image_add(collision_data->base.win);
82 elm_image_file_set(
83 sphere2, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
84 "big-red-ball");
85 evas_object_move(sphere2, WIDTH / 8, FLOOR_Y - 70 + 1);
86 evas_object_resize(sphere2, 70, 70);
87 evas_object_show(sphere2);
88 collision_data->base.evas_objs = eina_list_append(
89 collision_data->base.evas_objs, sphere2);
90
91 sphere_body2 = ephysics_body_sphere_add(collision_data->base.world);
92 ephysics_body_evas_object_set(sphere_body2, sphere2, EINA_TRUE);
93 ephysics_body_event_callback_add(sphere_body2,
94 EPHYSICS_CALLBACK_BODY_UPDATE,
95 update_object_cb, sh2);
96 ephysics_body_restitution_set(sphere_body2, 1);
97 ephysics_body_friction_set(sphere_body2, 0.4);
98 ephysics_body_central_impulse_apply(sphere_body2, 360, 0, 0);
99 collision_data->sphere2 = sphere_body2;
100 collision_data->base.bodies = eina_list_append(
101 collision_data->base.bodies, sphere_body2);
102
103 impact = elm_layout_add(collision_data->base.win);
104 elm_layout_file_set(
105 impact, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "impact");
106 evas_object_resize(impact, 20, 20);
107 evas_object_show(impact);
108 collision_data->base.evas_objs = eina_list_append(
109 collision_data->base.evas_objs, impact);
110 collision_data->impact = impact;
111}
112
113static void
114_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
115{
116 Collision_Data *collision_data = data;
117
118 DBG("Restart pressed");
119 ephysics_body_event_callback_del(collision_data->sphere,
120 EPHYSICS_CALLBACK_BODY_COLLISION,
121 _collision_cb);
122 test_clean((Test_Data *)collision_data);
123 _world_populate(collision_data);
124 ephysics_body_event_callback_add(collision_data->sphere,
125 EPHYSICS_CALLBACK_BODY_COLLISION,
126 _collision_cb, collision_data);
127}
128
129static void
130_win_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
131{
132 Collision_Data *collision_data = data;
133
134 test_clean((Test_Data *)collision_data);
135 evas_object_del(collision_data->base.layout);
136 ephysics_world_del(collision_data->base.world);
137 free(collision_data);
138 ephysics_shutdown();
139}
140
141void
142test_collision(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
143{
144 Collision_Data *collision_data;
145 EPhysics_Body *boundary;
146 EPhysics_World *world;
147
148 if (!ephysics_init())
149 return;
150
151 collision_data = calloc(1, sizeof(Collision_Data));
152 if (!collision_data)
153 {
154 ERR("Failed to create test data");
155 ephysics_shutdown();
156 return;
157 }
158
159 test_win_add((Test_Data *)collision_data, "Collision Detection", EINA_FALSE);
160 elm_object_signal_emit(collision_data->base.layout, "borders,show",
161 "ephysics_test");
162 evas_object_event_callback_add(collision_data->base.win, EVAS_CALLBACK_DEL,
163 _win_del, collision_data);
164 elm_layout_signal_callback_add(collision_data->base.layout, "restart",
165 "test-theme", _restart, collision_data);
166
167 world = ephysics_world_new();
168 ephysics_world_render_geometry_set(world, 50, 40, -50,
169 WIDTH - 100, FLOOR_Y - 40, DEPTH);
170 collision_data->base.world = world;
171
172 boundary = ephysics_body_bottom_boundary_add(collision_data->base.world);
173 ephysics_body_restitution_set(boundary, 0);
174 ephysics_body_friction_set(boundary, 20);
175
176 boundary = ephysics_body_right_boundary_add(collision_data->base.world);
177 ephysics_body_restitution_set(boundary, 0.8);
178
179 ephysics_body_left_boundary_add(collision_data->base.world);
180 ephysics_body_top_boundary_add(collision_data->base.world);
181
182 _world_populate(collision_data);
183
184 ephysics_body_event_callback_add(collision_data->sphere,
185 EPHYSICS_CALLBACK_BODY_COLLISION,
186 _collision_cb, collision_data);
187}
diff --git a/src/examples/ephysics/test_collision_filter.c b/src/examples/ephysics/test_collision_filter.c
new file mode 100644
index 0000000..b224ff2
--- /dev/null
+++ b/src/examples/ephysics/test_collision_filter.c
@@ -0,0 +1,91 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7static void
8_world_populate(Test_Data *test_data)
9{
10 static const char *colors[] = {"blue-ball", "big-red-ball"};
11 static const int sizes[] = {54, 70};
12 EPhysics_Body *fall_body;
13 Evas_Object *sphere;
14 int i, column, row;
15
16 for (i = 0; i < 4; i++)
17 {
18 column = i % 2;
19 row = i / 2;
20
21 sphere = elm_image_add(test_data->win);
22 elm_image_file_set(
23 sphere, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
24 colors[row]);
25 evas_object_move(sphere, (1 + column) * WIDTH / 4 +
26 (70 - sizes[row]) / 2,
27 100 + row * 60 + row / 2 * 20);
28 evas_object_resize(sphere, sizes[row], sizes[row]);
29 evas_object_show(sphere);
30 test_data->evas_objs = eina_list_append(test_data->evas_objs, sphere);
31
32 fall_body = ephysics_body_sphere_add(test_data->world);
33 ephysics_body_evas_object_set(fall_body, sphere, EINA_TRUE);
34 ephysics_body_restitution_set(fall_body, 0.95);
35 ephysics_body_friction_set(fall_body, 0.1);
36 ephysics_body_collision_group_add(fall_body, colors[row]);
37 if (column + row == 1)
38 ephysics_body_central_impulse_apply(fall_body, 600 * row - 300, 0, 0);
39 test_data->bodies = eina_list_append(test_data->bodies, fall_body);
40 }
41}
42
43static void
44_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
45{
46 Test_Data *test_data = data;
47
48 DBG("Restart pressed");
49 test_clean(test_data);
50 _world_populate(test_data);
51}
52
53void
54test_collision_filter(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
55{
56 EPhysics_Body *boundary;
57 EPhysics_World *world;
58 Test_Data *test_data;
59
60 if (!ephysics_init())
61 return;
62
63 test_data = test_data_new();
64 test_win_add(test_data, "Collision Filter", EINA_TRUE);
65 elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test");
66 elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme",
67 _restart, test_data);
68
69 world = ephysics_world_new();
70 ephysics_world_render_geometry_set(world, 60, 40, -50,
71 WIDTH - 120, FLOOR_Y - 32, DEPTH);
72 test_data->world = world;
73
74 boundary = ephysics_body_bottom_boundary_add(test_data->world);
75 ephysics_body_restitution_set(boundary, 0.65);
76 ephysics_body_friction_set(boundary, 2);
77
78 boundary = ephysics_body_box_add(test_data->world);
79 ephysics_body_geometry_set(boundary, 60, FLOOR_Y - 16, -15, WIDTH - 120, 10,
80 30);
81 ephysics_body_restitution_set(boundary, 0.65);
82 ephysics_body_friction_set(boundary, 2);
83 ephysics_body_mass_set(boundary, 0);
84 ephysics_body_collision_group_add(boundary, "blue-ball");
85
86 ephysics_body_top_boundary_add(test_data->world);
87 ephysics_body_left_boundary_add(test_data->world);
88 ephysics_body_right_boundary_add(test_data->world);
89
90 _world_populate(test_data);
91}
diff --git a/src/examples/ephysics/test_constraint.c b/src/examples/ephysics/test_constraint.c
new file mode 100644
index 0000000..988ed7b
--- /dev/null
+++ b/src/examples/ephysics/test_constraint.c
@@ -0,0 +1,117 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7static void
8_constraint_set(Test_Data *test_data, EPhysics_Body *body1, EPhysics_Body *body2)
9{
10 EPhysics_Constraint *constraint;
11 Evas_Coord b1x, b1y, b1z, b1w, b1h, b1d, b2x, b2y, b2z, b2w, b2h, b2d;
12
13 ephysics_body_geometry_get(body1, &b1x, &b1y, &b1z, &b1w, &b1h, &b1d);
14 ephysics_body_geometry_get(body2, &b2x, &b2y, &b2z, &b2w, &b2h, &b2d);
15
16 constraint = ephysics_constraint_linked_add(body1, body2);
17
18 ephysics_constraint_anchor_set(constraint, b1x + b1w / 2, b1y + b1h / 2 + 100,
19 b1z, b2x + b2w / 2, b2y + b2h / 2, b2z);
20 test_data->constraints = eina_list_append(test_data->constraints, constraint);
21}
22
23static void
24_world_populate(Test_Data *test_data)
25{
26 EPhysics_Body *box_body1, *box_body2;
27 Evas_Object *box1, *box2, *sh1, *sh2;
28
29 sh1 = elm_layout_add(test_data->win);
30 elm_layout_file_set(
31 sh1, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-cube");
32 evas_object_move(sh1, WIDTH / 3, FLOOR_Y);
33 evas_object_resize(sh1, 70, 3);
34 evas_object_show(sh1);
35 test_data->evas_objs = eina_list_append(test_data->evas_objs, sh1);
36
37 box1 = elm_image_add(test_data->win);
38 elm_image_file_set(
39 box1, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "blue-cube");
40 evas_object_move(box1, WIDTH / 3, HEIGHT / 8);
41 evas_object_resize(box1, 70, 70);
42 evas_object_show(box1);
43 test_data->evas_objs = eina_list_append(test_data->evas_objs, box1);
44
45 box_body1 = ephysics_body_box_add(test_data->world);
46 ephysics_body_evas_object_set(box_body1, box1, EINA_TRUE);
47 ephysics_body_event_callback_add(box_body1, EPHYSICS_CALLBACK_BODY_UPDATE,
48 update_object_cb, sh1);
49 ephysics_body_restitution_set(box_body1, 0.3);
50 ephysics_body_friction_set(box_body1, 0.1);
51 test_data->bodies = eina_list_append(test_data->bodies, box_body1);
52
53 sh2 = elm_layout_add(test_data->win);
54 elm_layout_file_set(
55 sh2, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-cube");
56 evas_object_move(sh2, WIDTH / 3 + 110, FLOOR_Y);
57 evas_object_resize(sh2, 70, 3);
58 evas_object_show(sh2);
59 test_data->evas_objs = eina_list_append(test_data->evas_objs, sh2);
60
61 box2 = elm_image_add(test_data->win);
62 elm_image_file_set(
63 box2, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "purple-cube");
64 evas_object_move(box2, WIDTH / 3 + 110, HEIGHT / 8);
65 evas_object_resize(box2, 70, 70);
66 evas_object_show(box2);
67 test_data->evas_objs = eina_list_append(test_data->evas_objs, box2);
68
69 box_body2 = ephysics_body_box_add(test_data->world);
70 ephysics_body_evas_object_set(box_body2, box2, EINA_TRUE);
71 ephysics_body_mass_set(box_body2, 5);
72 ephysics_body_event_callback_add(box_body2, EPHYSICS_CALLBACK_BODY_UPDATE,
73 update_object_cb, sh2);
74 ephysics_body_restitution_set(box_body2, 0.5);
75 ephysics_body_friction_set(box_body2, 0.1);
76 test_data->bodies = eina_list_append(test_data->bodies, box_body2);
77
78 _constraint_set(test_data, box_body1, box_body2);
79}
80
81static void
82_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
83{
84 Test_Data *test_data = data;
85
86 DBG("Restart pressed");
87 test_clean(test_data);
88 _world_populate(test_data);
89}
90
91void
92test_constraint(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
93{
94 EPhysics_Body *boundary;
95 EPhysics_World *world;
96 Test_Data *test_data;
97
98 if (!ephysics_init())
99 return;
100
101 test_data = test_data_new();
102 test_win_add(test_data, "Constraint", EINA_TRUE);
103 elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test");
104 elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme",
105 _restart, test_data);
106
107 world = ephysics_world_new();
108 ephysics_world_render_geometry_set(world, 50, 40, -50,
109 WIDTH - 100, FLOOR_Y - 40, DEPTH);
110 test_data->world = world;
111
112 boundary = ephysics_body_bottom_boundary_add(test_data->world);
113 ephysics_body_restitution_set(boundary, 0.65);
114 ephysics_body_friction_set(boundary, 3);
115
116 _world_populate(test_data);
117}
diff --git a/src/examples/ephysics/test_delete.c b/src/examples/ephysics/test_delete.c
new file mode 100644
index 0000000..a9d3fc4
--- /dev/null
+++ b/src/examples/ephysics/test_delete.c
@@ -0,0 +1,193 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7typedef struct _Collision_Data Collision_Data;
8
9struct _Collision_Data {
10 Test_Data base;
11 EPhysics_Body *sphere;
12 EPhysics_Body *sphere2;
13};
14
15static void
16_del_cb(void *data, EPhysics_Body *body, void *event_info __UNUSED__)
17{
18 Collision_Data *collision_data = data;
19 Evas_Object *obj, *shadow;
20
21 obj = ephysics_body_evas_object_get(body);
22 shadow = evas_object_data_get(obj, "shadow");
23
24 collision_data->base.evas_objs = eina_list_remove(
25 collision_data->base.evas_objs, shadow);
26 collision_data->base.evas_objs = eina_list_remove(
27 collision_data->base.evas_objs, obj);
28
29 evas_object_del(shadow);
30 evas_object_del(obj);
31
32 collision_data->sphere = NULL;
33}
34
35static void
36_collision_cb(void *data, EPhysics_Body *body, void *event_info)
37{
38 EPhysics_Body_Collision *collision = event_info;
39 Collision_Data *collision_data = data;
40 EPhysics_Body *contact_body;
41
42 contact_body = ephysics_body_collision_contact_body_get(collision);
43 if (contact_body != collision_data->sphere2) return;
44
45 collision_data->base.bodies = eina_list_remove(collision_data->base.bodies,
46 body);
47
48 INF("Collision Detected");
49 ephysics_body_del(body);
50}
51
52static void
53_world_populate(Collision_Data *collision_data)
54{
55 Evas_Object *sphere1, *sphere2, *sh1, *sh2;
56 EPhysics_Body *sphere_body1, *sphere_body2;
57
58 sh1 = elm_layout_add(collision_data->base.win);
59 elm_layout_file_set(
60 sh1, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-ball");
61 evas_object_move(sh1, WIDTH / 2, FLOOR_Y);
62 evas_object_resize(sh1, 70, 3);
63 evas_object_show(sh1);
64 collision_data->base.evas_objs = eina_list_append(
65 collision_data->base.evas_objs, sh1);
66
67 sphere1 = elm_image_add(collision_data->base.win);
68 elm_image_file_set(
69 sphere1, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
70 "big-red-ball");
71 evas_object_move(sphere1, WIDTH / 2, FLOOR_Y - 70 + 1);
72 evas_object_resize(sphere1, 70, 70);
73 evas_object_show(sphere1);
74 evas_object_data_set(sphere1, "shadow", sh1);
75 collision_data->base.evas_objs = eina_list_append(
76 collision_data->base.evas_objs, sphere1);
77
78 sphere_body1 = ephysics_body_sphere_add(collision_data->base.world);
79 ephysics_body_evas_object_set(sphere_body1, sphere1, EINA_TRUE);
80 ephysics_body_event_callback_add(sphere_body1,
81 EPHYSICS_CALLBACK_BODY_UPDATE,
82 update_object_cb, sh1);
83 ephysics_body_event_callback_add(sphere_body1,
84 EPHYSICS_CALLBACK_BODY_DEL,
85 _del_cb, collision_data);
86 ephysics_body_restitution_set(sphere_body1, 0.8);
87 ephysics_body_friction_set(sphere_body1, 0.4);
88 collision_data->base.bodies = eina_list_append(
89 collision_data->base.bodies, sphere_body1);
90 collision_data->sphere = sphere_body1;
91
92 sh2 = elm_layout_add(collision_data->base.win);
93 elm_layout_file_set(
94 sh2, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-ball");
95 evas_object_move(sh2, WIDTH / 8, FLOOR_Y);
96 evas_object_resize(sh2, 70, 3);
97 evas_object_show(sh2);
98 collision_data->base.evas_objs = eina_list_append(
99 collision_data->base.evas_objs, sh2);
100
101 sphere2 = elm_image_add(collision_data->base.win);
102 elm_image_file_set(
103 sphere2, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
104 "big-green-ball");
105 evas_object_move(sphere2, WIDTH / 8, FLOOR_Y - 70 + 1);
106 evas_object_resize(sphere2, 70, 70);
107 evas_object_show(sphere2);
108 collision_data->base.evas_objs = eina_list_append(
109 collision_data->base.evas_objs, sphere2);
110
111 sphere_body2 = ephysics_body_sphere_add(collision_data->base.world);
112 ephysics_body_evas_object_set(sphere_body2, sphere2, EINA_TRUE);
113 ephysics_body_event_callback_add(sphere_body2,
114 EPHYSICS_CALLBACK_BODY_UPDATE,
115 update_object_cb, sh2);
116 ephysics_body_restitution_set(sphere_body2, 1);
117 ephysics_body_friction_set(sphere_body2, 0.4);
118 ephysics_body_central_impulse_apply(sphere_body2, 360, 0, 0);
119 collision_data->sphere2 = sphere_body2;
120 collision_data->base.bodies = eina_list_append(
121 collision_data->base.bodies, sphere_body2);
122
123 ephysics_body_event_callback_add(collision_data->sphere,
124 EPHYSICS_CALLBACK_BODY_COLLISION,
125 _collision_cb, collision_data);
126}
127
128static void
129_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
130{
131 Collision_Data *collision_data = data;
132
133 DBG("Restart pressed");
134 test_clean((Test_Data *)collision_data);
135 _world_populate(collision_data);
136}
137
138static void
139_win_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
140{
141 Collision_Data *collision_data = data;
142
143 test_clean((Test_Data *)collision_data);
144 evas_object_del(collision_data->base.layout);
145 ephysics_world_del(collision_data->base.world);
146 free(collision_data);
147 ephysics_shutdown();
148}
149
150void
151test_delete(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
152{
153 Collision_Data *collision_data;
154 EPhysics_Body *boundary;
155 EPhysics_World *world;
156
157 if (!ephysics_init())
158 return;
159
160 collision_data = calloc(1, sizeof(Collision_Data));
161 if (!collision_data)
162 {
163 ERR("Failed to create test data");
164 ephysics_shutdown();
165 return;
166 }
167
168 test_win_add((Test_Data *)collision_data, "Delete Body", EINA_FALSE);
169 evas_object_event_callback_add(collision_data->base.win, EVAS_CALLBACK_DEL,
170 _win_del, collision_data);
171 elm_object_signal_emit(collision_data->base.layout, "borders,show",
172 "ephysics_test");
173 elm_layout_signal_callback_add(collision_data->base.layout, "restart",
174 "test-theme", _restart, collision_data);
175
176 world = ephysics_world_new();
177 ephysics_world_render_geometry_set(world, 50, 40, -50,
178 WIDTH - 100, FLOOR_Y - 40, DEPTH);
179 collision_data->base.world = world;
180
181 boundary = ephysics_body_bottom_boundary_add(collision_data->base.world);
182 ephysics_body_restitution_set(boundary, 0);
183 ephysics_body_friction_set(boundary, 20);
184
185 ephysics_body_left_boundary_add(collision_data->base.world);
186 ephysics_body_right_boundary_add(collision_data->base.world);
187
188 _world_populate(collision_data);
189
190 ephysics_body_event_callback_add(collision_data->sphere,
191 EPHYSICS_CALLBACK_BODY_COLLISION,
192 _collision_cb, collision_data);
193}
diff --git a/src/examples/ephysics/test_forces.c b/src/examples/ephysics/test_forces.c
new file mode 100644
index 0000000..44870cc
--- /dev/null
+++ b/src/examples/ephysics/test_forces.c
@@ -0,0 +1,114 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7static void
8_update_object_cb(void *data __UNUSED__, EPhysics_Body *body, void *event_info __UNUSED__)
9{
10 double x, y, z, torque, vx, vy, vz, va;
11
12 ephysics_body_evas_object_update(body);
13 ephysics_body_forces_get(body, &x, &y, &z);
14 ephysics_body_torques_get(body, NULL, NULL, &torque);
15 DBG("Body %p, fx: %lf, fy: %lf, fz: %lf, torque: %lf", body, x, y, z,
16 torque);
17
18 ephysics_body_linear_velocity_get(body, &vx, &vy, &vz);
19 ephysics_body_angular_velocity_get(body, NULL, NULL, &va);
20 DBG("Body %p, vx: %lf, vy: %lf, vz: %lf, va: %lf", body, vx, vy, vz, va);
21}
22
23static void
24_world_populate(Test_Data *test_data)
25{
26 EPhysics_Body *box_body1, *box_body2;
27 Evas_Object *box1, *box2;
28
29 box1 = elm_image_add(test_data->win);
30 elm_image_file_set(
31 box1, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "blue-cube");
32 evas_object_move(box1, 80, FLOOR_Y - 200);
33 evas_object_resize(box1, 70, 70);
34 evas_object_show(box1);
35 test_data->evas_objs = eina_list_append(test_data->evas_objs, box1);
36
37 box_body1 = ephysics_body_box_add(test_data->world);
38 ephysics_body_evas_object_set(box_body1, box1, EINA_TRUE);
39 ephysics_body_event_callback_add(box_body1, EPHYSICS_CALLBACK_BODY_UPDATE,
40 _update_object_cb, NULL);
41 ephysics_body_restitution_set(box_body1, 0.7);
42 ephysics_body_friction_set(box_body1, 0);
43 ephysics_body_force_apply(box_body1, 60, 0, 0, 0, 10, 0);
44 test_data->bodies = eina_list_append(test_data->bodies, box_body1);
45
46 box2 = elm_image_add(test_data->win);
47 elm_image_file_set(
48 box2, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "purple-cube");
49 evas_object_move(box2, 80, FLOOR_Y - 100);
50 evas_object_resize(box2, 70, 70);
51 evas_object_show(box2);
52 test_data->evas_objs = eina_list_append(test_data->evas_objs, box2);
53
54 box_body2 = ephysics_body_box_add(test_data->world);
55 ephysics_body_evas_object_set(box_body2, box2, EINA_TRUE);
56 ephysics_body_event_callback_add(box_body2, EPHYSICS_CALLBACK_BODY_UPDATE,
57 _update_object_cb, NULL);
58 ephysics_body_restitution_set(box_body2, 0.7);
59 ephysics_body_friction_set(box_body2, 0);
60 ephysics_body_central_force_apply(box_body2, 60, 0, 0);
61 test_data->bodies = eina_list_append(test_data->bodies, box_body2);
62 test_data->data = box_body2;
63}
64
65static void
66_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
67{
68 Test_Data *test_data = data;
69
70 DBG("Restart pressed");
71 test_clean(test_data);
72 _world_populate(test_data);
73}
74
75void
76test_forces(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
77{
78 EPhysics_Body *boundary;
79 EPhysics_World *world;
80 Test_Data *test_data;
81
82 if (!ephysics_init())
83 return;
84
85 test_data = test_data_new();
86 test_win_add(test_data, "Forces", EINA_TRUE);
87 elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test");
88 elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme",
89 _restart, test_data);
90
91 world = ephysics_world_new();
92 ephysics_world_render_geometry_set(world, 50, 40, -50,
93 WIDTH - 100, FLOOR_Y - 40, DEPTH);
94 test_data->world = world;
95
96 boundary = ephysics_body_bottom_boundary_add(test_data->world);
97 ephysics_body_restitution_set(boundary, 1);
98 ephysics_body_friction_set(boundary, 0);
99
100 boundary = ephysics_body_top_boundary_add(test_data->world);
101 ephysics_body_restitution_set(boundary, 1);
102 ephysics_body_friction_set(boundary, 0);
103
104 boundary = ephysics_body_left_boundary_add(test_data->world);
105 ephysics_body_restitution_set(boundary, 1);
106 ephysics_body_friction_set(boundary, 0);
107
108 boundary = ephysics_body_right_boundary_add(test_data->world);
109 ephysics_body_restitution_set(boundary, 1);
110 ephysics_body_friction_set(boundary, 0);
111
112 _world_populate(test_data);
113 ephysics_world_gravity_set(world, 0, 0, 0);
114}
diff --git a/src/examples/ephysics/test_growing_balls.c b/src/examples/ephysics/test_growing_balls.c
new file mode 100644
index 0000000..f92de5f
--- /dev/null
+++ b/src/examples/ephysics/test_growing_balls.c
@@ -0,0 +1,122 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7static Eina_Bool
8_grow_cb(void *data)
9{
10 Test_Data *test_data = data;
11 Evas_Object *obj;
12 Eina_List *l;
13 int size, i = -1;
14
15 EINA_LIST_FOREACH(test_data->evas_objs, l, obj)
16 {
17 evas_object_geometry_get(obj, NULL, NULL, &size, NULL);
18 size += i * 8;
19 i++;
20
21 if ((size < 20) || (size > 120))
22 continue;
23
24 evas_object_resize(obj, size, size);
25 }
26
27 return EINA_TRUE;
28}
29
30static void
31_add_sphere(Test_Data *test_data, const char *group, int size, int x, int y)
32{
33 EPhysics_Body *sphere_body;
34 Evas_Object *sphere;
35
36 sphere = elm_image_add(test_data->win);
37 elm_image_file_set(sphere, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
38 group);
39 evas_object_move(sphere, x, y);
40 evas_object_resize(sphere, size, size);
41 evas_object_show(sphere);
42 test_data->evas_objs = eina_list_append(test_data->evas_objs, sphere);
43
44 sphere_body = ephysics_body_sphere_add(test_data->world);
45 ephysics_body_evas_object_set(sphere_body, sphere, EINA_TRUE);
46 ephysics_body_restitution_set(sphere_body, 0.5);
47 ephysics_body_central_impulse_apply(sphere_body, 300, 0, 0);
48 test_data->bodies = eina_list_append(test_data->bodies, sphere_body);
49}
50
51static void
52_world_populate(Test_Data *test_data)
53{
54 _add_sphere(test_data, "big-red-ball", 60, 100, 100);
55 _add_sphere(test_data, "big-blue-ball", 60, 150, 150);
56 _add_sphere(test_data, "big-green-ball", 60, 200, 200);
57 test_data->data = ecore_timer_add(1, _grow_cb, test_data);
58}
59
60static void
61_win_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
62{
63 Test_Data *test_data = data;
64 Ecore_Timer *timer = test_data->data;
65
66 if (timer)
67 ecore_timer_del(timer);
68
69 test_data_del(test_data);
70 ephysics_shutdown();
71}
72
73static void
74_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
75{
76 Test_Data *test_data = data;
77 Ecore_Timer *timer = test_data->data;
78
79 if (timer)
80 ecore_timer_del(timer);
81
82 DBG("Restart pressed");
83 test_clean(test_data);
84 _world_populate(test_data);
85}
86
87void
88test_growing_balls(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
89{
90 EPhysics_Body *boundary;
91 EPhysics_World *world;
92 Test_Data *test_data;
93
94 if (!ephysics_init())
95 return;
96
97 test_data = test_data_new();
98 test_win_add(test_data, "Growing Balls", EINA_FALSE);
99 evas_object_event_callback_add(test_data->win, EVAS_CALLBACK_DEL,
100 _win_del, test_data);
101
102 elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test");
103 elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme",
104 _restart, test_data);
105
106 world = ephysics_world_new();
107 ephysics_world_render_geometry_set(world, 50, 40, -50,
108 WIDTH - 100, FLOOR_Y - 40, DEPTH);
109 test_data->world = world;
110
111 boundary = ephysics_body_bottom_boundary_add(test_data->world);
112 ephysics_body_restitution_set(boundary, 0.8);
113
114 boundary = ephysics_body_top_boundary_add(test_data->world);
115 ephysics_body_restitution_set(boundary, 0.8);
116 boundary = ephysics_body_left_boundary_add(test_data->world);
117 ephysics_body_restitution_set(boundary, 0.8);
118 boundary = ephysics_body_right_boundary_add(test_data->world);
119 ephysics_body_restitution_set(boundary, 0.8);
120
121 _world_populate(test_data);
122}
diff --git a/src/examples/ephysics/test_no_gravity.c b/src/examples/ephysics/test_no_gravity.c
new file mode 100644
index 0000000..0052bd2
--- /dev/null
+++ b/src/examples/ephysics/test_no_gravity.c
@@ -0,0 +1,133 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7static void
8_stop(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
9{
10 Test_Data *test_data = data;
11 EPhysics_Body *body = test_data->data;
12
13 ephysics_body_stop(body);
14 INF("Stop purple cube");
15}
16
17static void
18_world_populate(Test_Data *test_data)
19{
20 EPhysics_Body *box_body1, *box_body2;
21 Evas_Object *box1, *box2, *sh1, *sh2;
22
23 sh1 = elm_layout_add(test_data->win);
24 elm_layout_file_set(
25 sh1, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-cube");
26 evas_object_move(sh1, WIDTH / 2 - 80, FLOOR_Y);
27 evas_object_resize(sh1, 70, 3);
28 evas_object_show(sh1);
29 test_data->evas_objs = eina_list_append(test_data->evas_objs, sh1);
30
31 box1 = elm_image_add(test_data->win);
32 elm_image_file_set(
33 box1, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "blue-cube");
34 evas_object_move(box1, WIDTH / 2 - 80, FLOOR_Y - 200);
35 evas_object_resize(box1, 70, 70);
36 evas_object_show(box1);
37 test_data->evas_objs = eina_list_append(test_data->evas_objs, box1);
38
39 box_body1 = ephysics_body_box_add(test_data->world);
40 ephysics_body_evas_object_set(box_body1, box1, EINA_TRUE);
41 ephysics_body_event_callback_add(box_body1, EPHYSICS_CALLBACK_BODY_UPDATE,
42 update_object_cb, sh1);
43 ephysics_body_restitution_set(box_body1, 0.7);
44 ephysics_body_friction_set(box_body1, 0);
45 ephysics_body_linear_velocity_set(box_body1, -30, 40, 0);
46 ephysics_body_sleeping_threshold_set(box_body1, 0.1, 0.1);
47 test_data->bodies = eina_list_append(test_data->bodies, box_body1);
48
49 sh2 = elm_layout_add(test_data->win);
50 elm_layout_file_set(
51 sh2, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-cube");
52 evas_object_move(sh2, WIDTH / 2 - 80, FLOOR_Y);
53 evas_object_resize(sh2, 70, 3);
54 evas_object_show(sh2);
55 test_data->evas_objs = eina_list_append(test_data->evas_objs, sh2);
56
57 box2 = elm_image_add(test_data->win);
58 elm_image_file_set(
59 box2, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "purple-cube");
60 evas_object_move(box2, WIDTH / 2 + 10, FLOOR_Y - 200);
61 evas_object_resize(box2, 70, 70);
62 evas_object_show(box2);
63 test_data->evas_objs = eina_list_append(test_data->evas_objs, box2);
64
65 box_body2 = ephysics_body_box_add(test_data->world);
66 ephysics_body_evas_object_set(box_body2, box2, EINA_TRUE);
67 ephysics_body_event_callback_add(box_body2, EPHYSICS_CALLBACK_BODY_UPDATE,
68 update_object_cb, sh2);
69 ephysics_body_restitution_set(box_body2, 0.7);
70 ephysics_body_friction_set(box_body2, 0);
71 ephysics_body_linear_velocity_set(box_body2, 40, -30, 0);
72 ephysics_body_angular_velocity_set(box_body2, 0, 0, 36);
73 ephysics_body_sleeping_threshold_set(box_body2, 0.1, 0.1);
74 test_data->bodies = eina_list_append(test_data->bodies, box_body2);
75 test_data->data = box_body2;
76}
77
78static void
79_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
80{
81 Test_Data *test_data = data;
82
83 DBG("Restart pressed");
84 test_clean(test_data);
85 _world_populate(test_data);
86}
87
88void
89test_no_gravity(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
90{
91 EPhysics_Body *boundary;
92 EPhysics_World *world;
93 Test_Data *test_data;
94 Evas_Object *bt;
95
96 if (!ephysics_init())
97 return;
98
99 test_data = test_data_new();
100 test_win_add(test_data, "No Gravity", EINA_TRUE);
101 elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test");
102 elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme",
103 _restart, test_data);
104
105 bt = elm_button_add(test_data->win);
106 elm_object_style_set(bt, "ephysics-test-stop");
107 elm_layout_content_set(test_data->layout, "extra_button", bt);
108 evas_object_smart_callback_add(bt, "clicked", _stop, test_data);
109
110 world = ephysics_world_new();
111 ephysics_world_render_geometry_set(world, 50, 40, -50,
112 WIDTH - 100, FLOOR_Y - 40, DEPTH);
113 test_data->world = world;
114
115 boundary = ephysics_body_bottom_boundary_add(test_data->world);
116 ephysics_body_restitution_set(boundary, 1);
117 ephysics_body_friction_set(boundary, 0);
118
119 boundary = ephysics_body_top_boundary_add(test_data->world);
120 ephysics_body_restitution_set(boundary, 1);
121 ephysics_body_friction_set(boundary, 0);
122
123 boundary = ephysics_body_left_boundary_add(test_data->world);
124 ephysics_body_restitution_set(boundary, 1);
125 ephysics_body_friction_set(boundary, 0);
126
127 boundary = ephysics_body_right_boundary_add(test_data->world);
128 ephysics_body_restitution_set(boundary, 1);
129 ephysics_body_friction_set(boundary, 0);
130
131 _world_populate(test_data);
132 ephysics_world_gravity_set(world, 0, 0, 0);
133}
diff --git a/src/examples/ephysics/test_rotating_forever.c b/src/examples/ephysics/test_rotating_forever.c
new file mode 100644
index 0000000..f044588
--- /dev/null
+++ b/src/examples/ephysics/test_rotating_forever.c
@@ -0,0 +1,206 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7static Eina_Bool
8_rotate_cb(void *data)
9{
10 EPhysics_Quaternion *quat_prev, quat_delta, quat;
11 EPhysics_Body *body = data;
12
13 quat_prev = ephysics_body_rotation_get(body, NULL);
14 ephysics_quaternion_set(&quat_delta, 0, 0, -0.15, 0.98);
15 ephysics_quaternion_normalize(&quat_delta);
16 ephysics_body_rotation_set(
17 body, ephysics_quaternion_multiply(&quat_delta, quat_prev, &quat));
18 free(quat_prev);
19
20 return EINA_TRUE;
21}
22
23static Eina_Bool
24_increase_torque_cb(void *data)
25{
26 EPhysics_Body *body = data;
27 Evas_Object *obj;
28
29 ephysics_body_torque_apply(body, 0, 0, 2);
30 obj = ephysics_body_evas_object_get(body);
31 evas_object_data_set(obj, "increase_timer", NULL);
32
33 return EINA_FALSE;
34}
35
36static Eina_Bool
37_stop_torque_cb(void *data)
38{
39 EPhysics_Body *body = data;
40 Evas_Object *obj;
41
42 ephysics_body_forces_clear(body);
43 obj = ephysics_body_evas_object_get(body);
44 evas_object_data_set(obj, "stop_timer", NULL);
45
46 return EINA_FALSE;
47}
48
49static void
50_del_cb(void *data, EPhysics_Body *body __UNUSED__, void *event_info __UNUSED__)
51{
52 ecore_timer_del(data);
53}
54
55static void
56_del_torque_cb(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
57{
58 Ecore_Timer *timer;
59
60 timer = evas_object_data_get(obj, "increase_timer");
61 if (timer)
62 ecore_timer_del(timer);
63
64 timer = evas_object_data_get(obj, "stop_timer");
65 if (timer)
66 ecore_timer_del(timer);
67}
68
69static void
70_update_object_cb(void *data __UNUSED__, EPhysics_Body *body, void *event_info __UNUSED__)
71{
72 double rx, ry, rz, rw, vrot, torque;
73 EPhysics_Quaternion quat;
74
75 ephysics_body_angular_velocity_get(body, NULL, NULL, &vrot);
76 ephysics_body_torques_get(body, NULL, NULL, &torque);
77
78 ephysics_body_rotation_get(body, &quat);
79 ephysics_quaternion_get(&quat, &rx, &ry, &rz, &rw);
80
81 ephysics_body_evas_object_update(body);
82
83 DBG("body: %p, rot: (%lf, %lf, %lf, %lf), vrot: %lf, torque: %lf", body,
84 rx, ry, rz, rw, vrot, torque);
85}
86
87static void
88_world_populate(Test_Data *test_data)
89{
90 EPhysics_Body *body;
91 Ecore_Timer *timer;
92 Evas_Object *cube;
93
94 cube = elm_image_add(test_data->win);
95 elm_image_file_set(
96 cube, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "blue-cube");
97 evas_object_move(cube, WIDTH / 3, HEIGHT / 2 - 30);
98 evas_object_resize(cube, 70, 70);
99 evas_object_show(cube);
100 test_data->evas_objs = eina_list_append(test_data->evas_objs, cube);
101
102 body = ephysics_body_box_add(test_data->world);
103 ephysics_body_evas_object_set(body, cube, EINA_TRUE);
104 test_data->bodies = eina_list_append(test_data->bodies, body);
105 ephysics_body_event_callback_add(body,
106 EPHYSICS_CALLBACK_BODY_UPDATE,
107 _update_object_cb, NULL);
108
109 ephysics_body_torque_impulse_apply(body, 0, 0, 1);
110
111 cube = elm_image_add(test_data->win);
112 elm_image_file_set(
113 cube, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "purple-cube");
114 evas_object_move(cube, WIDTH / 3, FLOOR_Y - 70);
115 evas_object_resize(cube, 70, 70);
116 evas_object_show(cube);
117 test_data->evas_objs = eina_list_append(test_data->evas_objs, cube);
118
119 body = ephysics_body_box_add(test_data->world);
120 ephysics_body_evas_object_set(body, cube, EINA_TRUE);
121 test_data->bodies = eina_list_append(test_data->bodies, body);
122 ephysics_body_event_callback_add(body,
123 EPHYSICS_CALLBACK_BODY_UPDATE,
124 _update_object_cb, NULL);
125
126 ephysics_body_impulse_apply(body, 30, 0, 0, 0, -10, 0);
127
128 cube = elm_image_add(test_data->win);
129 elm_image_file_set(
130 cube, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "purple-cube");
131 evas_object_move(cube, WIDTH / 3, 60);
132 evas_object_resize(cube, 70, 70);
133 evas_object_show(cube);
134 test_data->evas_objs = eina_list_append(test_data->evas_objs, cube);
135
136 body = ephysics_body_box_add(test_data->world);
137 ephysics_body_evas_object_set(body, cube, EINA_TRUE);
138 test_data->bodies = eina_list_append(test_data->bodies, body);
139 ephysics_body_event_callback_add(body,
140 EPHYSICS_CALLBACK_BODY_UPDATE,
141 _update_object_cb, NULL);
142
143 timer = ecore_timer_add(1, _rotate_cb, body);
144
145 ephysics_body_event_callback_add(body,
146 EPHYSICS_CALLBACK_BODY_DEL,
147 _del_cb, timer);
148
149 cube = elm_image_add(test_data->win);
150 elm_image_file_set(
151 cube, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "blue-cube");
152 evas_object_move(cube, WIDTH * 2 / 3, 60);
153 evas_object_resize(cube, 70, 70);
154 evas_object_show(cube);
155 test_data->evas_objs = eina_list_append(test_data->evas_objs, cube);
156
157 body = ephysics_body_box_add(test_data->world);
158 ephysics_body_evas_object_set(body, cube, EINA_TRUE);
159 test_data->bodies = eina_list_append(test_data->bodies, body);
160 ephysics_body_event_callback_add(body,
161 EPHYSICS_CALLBACK_BODY_UPDATE,
162 _update_object_cb, NULL);
163
164 ephysics_body_torque_apply(body, 0, 0, 2);
165 evas_object_event_callback_add(cube, EVAS_CALLBACK_DEL,
166 _del_torque_cb, NULL);
167
168 timer = ecore_timer_add(3, _increase_torque_cb, body);
169 evas_object_data_set(cube, "increase_timer", timer);
170 timer = ecore_timer_add(5, _stop_torque_cb, body);
171 evas_object_data_set(cube, "stop_timer", timer);
172}
173
174static void
175_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
176{
177 Test_Data *test_data = data;
178
179 DBG("Restart pressed");
180 test_clean(test_data);
181 _world_populate(test_data);
182}
183
184void
185test_rotating_forever(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
186{
187 EPhysics_World *world;
188 Test_Data *test_data;
189
190 if (!ephysics_init())
191 return;
192
193 test_data = test_data_new();
194 test_win_add(test_data, "Rotating Forever", EINA_TRUE);
195 elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test");
196 elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme",
197 _restart, test_data);
198
199 world = ephysics_world_new();
200 ephysics_world_render_geometry_set(world, 50, 40, -50,
201 WIDTH - 100, FLOOR_Y - 40, DEPTH);
202 test_data->world = world;
203 ephysics_world_gravity_set(world, 0, 0, 0);
204
205 _world_populate(test_data);
206}
diff --git a/src/examples/ephysics/test_shapes.c b/src/examples/ephysics/test_shapes.c
new file mode 100644
index 0000000..8b01222
--- /dev/null
+++ b/src/examples/ephysics/test_shapes.c
@@ -0,0 +1,110 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7static void
8_world_populate(Test_Data *test_data)
9{
10 EPhysics_Shape *pentagon_shape, *hexagon_shape;
11 EPhysics_Body *pentagon_body, *hexagon_body;
12 Evas_Object *pentagon, *hexagon;
13
14 pentagon = elm_image_add(test_data->win);
15 elm_image_file_set(
16 pentagon, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "pentagon");
17 evas_object_move(pentagon, WIDTH / 3, HEIGHT / 2 - 80);
18 evas_object_resize(pentagon, 70, 68);
19 evas_object_show(pentagon);
20 test_data->evas_objs = eina_list_append(test_data->evas_objs, pentagon);
21
22 pentagon_shape = ephysics_shape_new();
23 ephysics_shape_point_add(pentagon_shape, -1, -9/33., -1);
24 ephysics_shape_point_add(pentagon_shape, -1, -9/33., 1);
25 ephysics_shape_point_add(pentagon_shape, 0, -1, -1);
26 ephysics_shape_point_add(pentagon_shape, 0, -1, 1);
27 ephysics_shape_point_add(pentagon_shape, 1, -9/33., -1);
28 ephysics_shape_point_add(pentagon_shape, 1, -9/33., 1);
29 ephysics_shape_point_add(pentagon_shape, -21/35., 1, -1);
30 ephysics_shape_point_add(pentagon_shape, -21/35., 1, 1);
31 ephysics_shape_point_add(pentagon_shape, 21/35., 1, -1);
32 ephysics_shape_point_add(pentagon_shape, 21/35., 1, 1);
33
34 pentagon_body = ephysics_body_shape_add(test_data->world, pentagon_shape);
35 ephysics_body_evas_object_set(pentagon_body, pentagon, EINA_TRUE);
36 ephysics_body_restitution_set(pentagon_body, 1);
37 test_data->bodies = eina_list_append(test_data->bodies, pentagon_body);
38
39 hexagon = elm_image_add(test_data->win);
40 elm_image_file_set(
41 hexagon, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "hexagon");
42 evas_object_move(hexagon, WIDTH / 3 + 100, HEIGHT / 2 - 100);
43 evas_object_resize(hexagon, 70, 60);
44 evas_object_show(hexagon);
45 test_data->evas_objs = eina_list_append(test_data->evas_objs, hexagon);
46
47 hexagon_shape = ephysics_shape_new();
48 ephysics_shape_point_add(hexagon_shape, 0, 30, -10);
49 ephysics_shape_point_add(hexagon_shape, 0, 30, 10);
50 ephysics_shape_point_add(hexagon_shape, 18, 0, -10);
51 ephysics_shape_point_add(hexagon_shape, 18, 0, 10);
52 ephysics_shape_point_add(hexagon_shape, 52, 0, -10);
53 ephysics_shape_point_add(hexagon_shape, 52, 0, 10);
54 ephysics_shape_point_add(hexagon_shape, 70, 30, -10);
55 ephysics_shape_point_add(hexagon_shape, 70, 30, 10);
56 ephysics_shape_point_add(hexagon_shape, 52, 60, -10);
57 ephysics_shape_point_add(hexagon_shape, 52, 60, 10);
58 ephysics_shape_point_add(hexagon_shape, 18, 60, -10);
59 ephysics_shape_point_add(hexagon_shape, 18, 60, 10);
60
61 hexagon_body = ephysics_body_shape_add(test_data->world, hexagon_shape);
62 ephysics_body_evas_object_set(hexagon_body, hexagon, EINA_TRUE);
63 ephysics_body_restitution_set(hexagon_body, 1);
64 test_data->bodies = eina_list_append(test_data->bodies, hexagon_body);
65 ephysics_body_torque_apply(hexagon_body, 0, 0, -3);
66
67 ephysics_shape_del(pentagon_shape);
68 ephysics_shape_del(hexagon_shape);
69}
70
71static void
72_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
73{
74 Test_Data *test_data = data;
75
76 DBG("Restart pressed");
77 test_clean(test_data);
78 _world_populate(test_data);
79}
80
81void
82test_shapes(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
83{
84 EPhysics_Body *boundary;
85 EPhysics_World *world;
86 Test_Data *test_data;
87
88 if (!ephysics_init())
89 return;
90
91 test_data = test_data_new();
92 test_win_add(test_data, "Shapes", EINA_TRUE);
93 elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test");
94 elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme",
95 _restart, test_data);
96
97 world = ephysics_world_new();
98 ephysics_world_render_geometry_set(world, 50, 40, -50,
99 WIDTH - 100, FLOOR_Y - 40, DEPTH);
100 test_data->world = world;
101
102 boundary = ephysics_body_bottom_boundary_add(test_data->world);
103 ephysics_body_restitution_set(boundary, 0.65);
104
105 ephysics_body_top_boundary_add(test_data->world);
106 ephysics_body_left_boundary_add(test_data->world);
107 ephysics_body_right_boundary_add(test_data->world);
108
109 _world_populate(test_data);
110}
diff --git a/src/examples/ephysics/test_sleeping_threshold.c b/src/examples/ephysics/test_sleeping_threshold.c
new file mode 100644
index 0000000..e6b2d51
--- /dev/null
+++ b/src/examples/ephysics/test_sleeping_threshold.c
@@ -0,0 +1,119 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7static void
8_world_populate(Test_Data *test_data)
9{
10 Evas_Object *sphere1, *sphere2, *sh1, *sh2;
11 EPhysics_Body *sphere_body1, *sphere_body2;
12 double linear, angular;
13
14 sh1 = elm_layout_add(test_data->win);
15 elm_layout_file_set(
16 sh1, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-ball");
17 evas_object_move(sh1, 50, FLOOR_Y);
18 evas_object_resize(sh1, 70, 3);
19 evas_object_show(sh1);
20 test_data->evas_objs = eina_list_append(test_data->evas_objs, sh1);
21
22 sphere1 = elm_image_add(test_data->win);
23 elm_image_file_set(sphere1, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
24 "big-blue-ball");
25 evas_object_move(sphere1, 50, FLOOR_Y - 70);
26 evas_object_resize(sphere1, 70, 70);
27 evas_object_show(sphere1);
28 test_data->evas_objs = eina_list_append(test_data->evas_objs, sphere1);
29
30 sphere_body1 = ephysics_body_sphere_add(test_data->world);
31 ephysics_body_evas_object_set(sphere_body1, sphere1, EINA_TRUE);
32 ephysics_body_event_callback_add(sphere_body1,
33 EPHYSICS_CALLBACK_BODY_UPDATE,
34 update_object_cb, sh1);
35 ephysics_body_restitution_set(sphere_body1, 1);
36 ephysics_body_friction_set(sphere_body1, 0.4);
37 ephysics_body_sleeping_threshold_set(sphere_body1, 60, 360);
38 ephysics_body_linear_velocity_set(sphere_body1, 100, 0, 0);
39 ephysics_body_damping_set(sphere_body1, 0.5, 0.5);
40 test_data->bodies = eina_list_append(test_data->bodies, sphere_body1);
41
42 sh2 = elm_layout_add(test_data->win);
43 elm_layout_file_set(
44 sh2, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-ball");
45 evas_object_move(sh2, WIDTH - 120, FLOOR_Y);
46 evas_object_resize(sh2, 70, 3);
47 evas_object_show(sh2);
48 test_data->evas_objs = eina_list_append(test_data->evas_objs, sh2);
49
50 sphere2 = elm_image_add(test_data->win);
51 elm_image_file_set(sphere2, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj",
52 "big-red-ball");
53 evas_object_move(sphere2, WIDTH - 120, FLOOR_Y - 70);
54 evas_object_resize(sphere2, 70, 70);
55 evas_object_show(sphere2);
56 test_data->evas_objs = eina_list_append(test_data->evas_objs, sphere2);
57
58 sphere_body2 = ephysics_body_sphere_add(test_data->world);
59 ephysics_body_evas_object_set(sphere_body2, sphere2, EINA_TRUE);
60 ephysics_body_event_callback_add(sphere_body2,
61 EPHYSICS_CALLBACK_BODY_UPDATE,
62 update_object_cb, sh2);
63 ephysics_body_restitution_set(sphere_body2, 1);
64 ephysics_body_friction_set(sphere_body2, 0.4);
65 ephysics_body_sleeping_threshold_set(sphere_body2, 10, 360);
66 ephysics_body_linear_velocity_set(sphere_body2, -100, 0, 0);
67 ephysics_body_damping_set(sphere_body2, 0.5, 0.5);
68 test_data->bodies = eina_list_append(test_data->bodies, sphere_body2);
69
70 ephysics_body_sleeping_threshold_get(sphere_body1, &linear, &angular);
71 INF("Body 1: linear threshold: %.2f, angular: %.2f", linear, angular);
72 ephysics_body_sleeping_threshold_get(sphere_body2, &linear, &angular);
73 INF("Body 2: linear threshold: %.2f, angular: %.2f", linear, angular);
74}
75
76static void
77_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
78{
79 Test_Data *test_data = data;
80
81 DBG("Restart pressed");
82 test_clean(test_data);
83 _world_populate(test_data);
84}
85
86void
87test_sleeping(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
88{
89 EPhysics_Body *boundary;
90 EPhysics_World *world;
91 Test_Data *test_data;
92
93 if (!ephysics_init())
94 return;
95
96 test_data = test_data_new();
97 test_win_add(test_data, "Sleeping Threshold", EINA_TRUE);
98 elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test");
99 elm_layout_signal_callback_add(test_data->layout, "restart",
100 "test-theme", _restart, test_data);
101
102 world = ephysics_world_new();
103 ephysics_world_render_geometry_set(world, 50, 40, -50,
104 WIDTH - 100, FLOOR_Y - 40, DEPTH);
105 test_data->world = world;
106 ephysics_world_max_sleeping_time_set(world, 0.3);
107
108 boundary = ephysics_body_bottom_boundary_add(test_data->world);
109 ephysics_body_restitution_set(boundary, 0);
110 ephysics_body_friction_set(boundary, 20);
111
112 boundary = ephysics_body_right_boundary_add(test_data->world);
113 ephysics_body_restitution_set(boundary, 0.8);
114
115 ephysics_body_left_boundary_add(test_data->world);
116 ephysics_body_top_boundary_add(test_data->world);
117
118 _world_populate(test_data);
119}
diff --git a/src/examples/ephysics/test_slider.c b/src/examples/ephysics/test_slider.c
new file mode 100644
index 0000000..f17ca01
--- /dev/null
+++ b/src/examples/ephysics/test_slider.c
@@ -0,0 +1,170 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7static void
8_world_populate(Test_Data *test_data)
9{
10 EPhysics_Body *box_body1, *box_body2, *box_body3, *box_body4;
11 Evas_Object *box1, *box2, *box3, *box4, *sh1, *sh2, *sh3, *sh4;
12 EPhysics_Constraint *constraint;
13
14 sh1 = elm_layout_add(test_data->win);
15 elm_layout_file_set(
16 sh1, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-cube");
17 evas_object_move(sh1, WIDTH / 6, FLOOR_Y);
18 evas_object_resize(sh1, 70, 3);
19 evas_object_show(sh1);
20 test_data->evas_objs = eina_list_append(test_data->evas_objs, sh1);
21
22 box1 = elm_image_add(test_data->win);
23 elm_image_file_set(
24 box1, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "blue-cube");
25 evas_object_move(box1, WIDTH / 6, FLOOR_Y - 70);
26 evas_object_resize(box1, 70, 70);
27 evas_object_show(box1);
28 test_data->evas_objs = eina_list_append(test_data->evas_objs, box1);
29
30 box_body1 = ephysics_body_box_add(test_data->world);
31 ephysics_body_mass_set(box_body1, 0);
32 ephysics_body_evas_object_set(box_body1, box1, EINA_TRUE);
33 ephysics_body_event_callback_add(box_body1, EPHYSICS_CALLBACK_BODY_UPDATE,
34 update_object_cb, sh1);
35 ephysics_body_restitution_set(box_body1, 0.3);
36 ephysics_body_friction_set(box_body1, 0.1);
37 test_data->bodies = eina_list_append(test_data->bodies, box_body1);
38
39 sh2 = elm_layout_add(test_data->win);
40 elm_layout_file_set(
41 sh2, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-cube");
42 evas_object_move(sh2, WIDTH / 6 + 60, FLOOR_Y);
43 evas_object_resize(sh2, 70, 3);
44 evas_object_show(sh2);
45 test_data->evas_objs = eina_list_append(test_data->evas_objs, sh2);
46
47 box2 = elm_image_add(test_data->win);
48 elm_image_file_set(
49 box2, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "purple-cube");
50 evas_object_move(box2, WIDTH / 6 + 65, HEIGHT / 8);
51 evas_object_resize(box2, 70, 70);
52 evas_object_show(box2);
53 test_data->evas_objs = eina_list_append(test_data->evas_objs, box2);
54
55 box_body2 = ephysics_body_box_add(test_data->world);
56 ephysics_body_evas_object_set(box_body2, box2, EINA_TRUE);
57 ephysics_body_event_callback_add(box_body2, EPHYSICS_CALLBACK_BODY_UPDATE,
58 update_object_cb, sh2);
59 ephysics_body_restitution_set(box_body2, 0.5);
60 ephysics_body_friction_set(box_body2, 0.1);
61 test_data->bodies = eina_list_append(test_data->bodies, box_body2);
62
63 constraint = ephysics_constraint_add(box_body2);
64 ephysics_constraint_linear_limit_set(constraint, 0, 0, 0,
65 HEIGHT - (HEIGHT / 8), 0, 0);
66 ephysics_constraint_angular_limit_set(constraint, 0, 0, 0, 0, 0, 45);
67 test_data->constraints = eina_list_append(test_data->constraints,
68 constraint);
69
70 sh3 = elm_layout_add(test_data->win);
71 elm_layout_file_set(
72 sh3, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-cube");
73 evas_object_move(sh3, WIDTH / 6 + 60, FLOOR_Y);
74 evas_object_resize(sh3, 70, 3);
75 evas_object_show(sh3);
76 test_data->evas_objs = eina_list_append(test_data->evas_objs, sh3);
77
78 box3 = elm_image_add(test_data->win);
79 elm_image_file_set(
80 box3, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "blue-cube");
81 evas_object_move(box3, WIDTH - 120, HEIGHT / 8);
82 evas_object_resize(box3, 70, 70);
83 evas_object_show(box3);
84 test_data->evas_objs = eina_list_append(test_data->evas_objs, box3);
85
86 box_body3 = ephysics_body_box_add(test_data->world);
87 ephysics_body_evas_object_set(box_body3, box3, EINA_TRUE);
88 ephysics_body_event_callback_add(box_body3, EPHYSICS_CALLBACK_BODY_UPDATE,
89 update_object_cb, sh3);
90 ephysics_body_restitution_set(box_body3, 0.5);
91 ephysics_body_friction_set(box_body3, 0.1);
92 test_data->bodies = eina_list_append(test_data->bodies, box_body3);
93
94 constraint = ephysics_constraint_add(box_body3);
95 ephysics_constraint_linear_limit_set(constraint, WIDTH - 120, 0, 0, 0, 0, 0);
96 ephysics_body_central_impulse_apply(box_body3, -240, 0, 0);
97 test_data->constraints = eina_list_append(test_data->constraints,
98 constraint);
99
100 sh4 = elm_layout_add(test_data->win);
101 elm_layout_file_set(
102 sh4, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-cube");
103 evas_object_move(sh4, WIDTH / 6 + 60, FLOOR_Y);
104 evas_object_resize(sh4, 70, 3);
105 evas_object_show(sh4);
106 test_data->evas_objs = eina_list_append(test_data->evas_objs, sh4);
107
108 box4 = elm_image_add(test_data->win);
109 elm_image_file_set(
110 box4, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "purple-cube");
111 evas_object_move(box4, WIDTH - 120, FLOOR_Y - 70);
112 evas_object_resize(box4, 70, 70);
113 evas_object_show(box4);
114 test_data->evas_objs = eina_list_append(test_data->evas_objs, box4);
115
116 box_body4 = ephysics_body_box_add(test_data->world);
117 ephysics_body_evas_object_set(box_body4, box4, EINA_TRUE);
118 ephysics_body_event_callback_add(box_body4, EPHYSICS_CALLBACK_BODY_UPDATE,
119 update_object_cb, sh4);
120 ephysics_body_restitution_set(box_body4, 0.5);
121 ephysics_body_friction_set(box_body4, 0.1);
122 test_data->bodies = eina_list_append(test_data->bodies, box_body4);
123
124 constraint = ephysics_constraint_add(box_body4);
125 ephysics_constraint_linear_limit_set(constraint, WIDTH / 3, 0, 0, 0, 0, 0);
126 ephysics_body_central_impulse_apply(box_body4, -600, 0, 0);
127 test_data->constraints = eina_list_append(test_data->constraints,
128 constraint);
129
130}
131
132static void
133_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
134{
135 Test_Data *test_data = data;
136
137 DBG("Restart pressed");
138 test_clean(test_data);
139 _world_populate(test_data);
140}
141
142void
143test_slider(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
144{
145 EPhysics_Body *boundary;
146 EPhysics_World *world;
147 Test_Data *test_data;
148
149 if (!ephysics_init())
150 return;
151
152 test_data = test_data_new();
153 test_win_add(test_data, "Slider", EINA_TRUE);
154 elm_object_signal_emit(test_data->layout, "borders,show", "ephysics_test");
155 elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme",
156 _restart, test_data);
157
158 world = ephysics_world_new();
159 ephysics_world_render_geometry_set(world, 50, 40, -50,
160 WIDTH - 100, FLOOR_Y - 40, DEPTH);
161 test_data->world = world;
162
163 boundary = ephysics_body_bottom_boundary_add(test_data->world);
164 ephysics_body_restitution_set(boundary, 0.65);
165 ephysics_body_friction_set(boundary, 3);
166
167 ephysics_body_left_boundary_add(test_data->world);
168
169 _world_populate(test_data);
170}
diff --git a/src/examples/ephysics/test_velocity.c b/src/examples/ephysics/test_velocity.c
new file mode 100644
index 0000000..785767b
--- /dev/null
+++ b/src/examples/ephysics/test_velocity.c
@@ -0,0 +1,211 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7typedef struct _Velocity_Data Velocity_Data;
8
9struct _Velocity_Data {
10 Test_Data base;
11 EPhysics_Body *body;
12 double old_vx;
13 double old_vy;
14 double old_vaz;
15 double last_time;
16};
17
18static Eina_Bool
19_on_keydown(void *data, Evas_Object *obj __UNUSED__, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
20{
21 Evas_Event_Key_Down *ev = event_info;
22 EPhysics_Body *body = data;
23
24 if (type != EVAS_CALLBACK_KEY_UP)
25 return EINA_FALSE;
26
27 if (strcmp(ev->keyname, "Up") == 0)
28 ephysics_body_central_impulse_apply(body, 0, -300, 0);
29 else if (strcmp(ev->keyname, "Down") == 0)
30 ephysics_body_central_impulse_apply(body, 0, 300, 0);
31 else if (strcmp(ev->keyname, "Right") == 0)
32 ephysics_body_central_impulse_apply(body, 300, 0, 0);
33 else if (strcmp(ev->keyname, "Left") == 0)
34 ephysics_body_central_impulse_apply(body, -300, 0, 0);
35
36 return EINA_TRUE;
37}
38
39static void
40_update_vel_cb(void *data, EPhysics_Body *body, void *event_info __UNUSED__)
41{
42 Velocity_Data *velocity_data = data;
43 Eina_Bool first_call = EINA_FALSE;
44 double vx, vy, ax, ay, vaz, aaz;
45 double time_now, delta_time;
46 char buff[64];
47
48 if (!velocity_data->last_time)
49 first_call = EINA_TRUE;
50
51 time_now = ecore_time_get();
52 delta_time = time_now - velocity_data->last_time;
53 velocity_data->last_time = time_now;
54
55 ephysics_body_angular_velocity_get(body, NULL, NULL, &vaz);
56 ephysics_body_linear_velocity_get(body, &vx, &vy, NULL);
57 vx = (vx > 0 || vx <= -0.01) ? vx : 0;
58 vy = (vy > 0 || vy <= -0.01) ? vy : 0;
59
60 aaz = (vaz - velocity_data->old_vaz) / delta_time;
61 velocity_data->old_vaz = vaz;
62 ax = (vx - velocity_data->old_vx) / delta_time;
63 velocity_data->old_vx = vx;
64 ay = (vy - velocity_data->old_vy) / delta_time;
65 velocity_data->old_vy = vy;
66
67 if (first_call) return;
68
69 DBG("Delta Time: %0.3lf", delta_time);
70
71 snprintf(buff, sizeof(buff), "Linear velocity: %.2f, %.2f", vx, vy);
72 elm_layout_text_set(velocity_data->base.layout, "linear_vel", buff);
73 snprintf(buff, sizeof(buff), "Linear acceleration: %.2f, %.2f", ax, ay);
74 elm_layout_text_set(velocity_data->base.layout, "linear_acc", buff);
75 DBG("Vx: %0.3lf p/s Ax: %0.3lf p/s/s", vx, ax);
76 DBG("Vy: %0.3lf p/s Ay: %0.3lf p/s/s", vy, ay);
77
78 snprintf(buff, sizeof(buff), "Angular velocity: %.2f", vaz);
79 elm_layout_text_set(velocity_data->base.layout, "angular_vel", buff);
80 snprintf(buff, sizeof(buff), "Angular acceleration: %.2f", aaz);
81 elm_layout_text_set(velocity_data->base.layout, "angular_acc", buff);
82 DBG("Va: %0.3lf d/s Aa: %0.3lf d/s/s", vaz, aaz);
83}
84
85static void
86_world_populate(Velocity_Data *velocity_data)
87{
88 Evas_Object *sphere, *shadow;
89 EPhysics_Body *sphere_body;
90
91 shadow = elm_layout_add(velocity_data->base.win);
92 elm_layout_file_set(
93 shadow, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-ball");
94 evas_object_move(shadow, WIDTH / 3, FLOOR_Y);
95 evas_object_resize(shadow, 54, 3);
96 evas_object_show(shadow);
97 velocity_data->base.evas_objs = eina_list_append(
98 velocity_data->base.evas_objs, shadow);
99
100 sphere = elm_image_add(velocity_data->base.win);
101 elm_image_file_set(
102 sphere, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "red-ball");
103 evas_object_move(sphere, WIDTH / 3, FLOOR_Y - 54);
104 evas_object_resize(sphere, 54, 54);
105 evas_object_show(sphere);
106 velocity_data->base.evas_objs = eina_list_append(
107 velocity_data->base.evas_objs, sphere);
108
109 sphere_body = ephysics_body_sphere_add(velocity_data->base.world);
110 ephysics_body_evas_object_set(sphere_body, sphere, EINA_TRUE);
111 ephysics_body_restitution_set(sphere_body, 0.8);
112 ephysics_body_friction_set(sphere_body, 1);
113 ephysics_body_sleeping_threshold_set(sphere_body, 0, 0);
114 ephysics_body_damping_set(sphere_body, 0.2, 0.2);
115 ephysics_body_event_callback_add(sphere_body, EPHYSICS_CALLBACK_BODY_UPDATE,
116 update_object_cb, shadow);
117 ephysics_body_event_callback_add(sphere_body, EPHYSICS_CALLBACK_BODY_UPDATE,
118 _update_vel_cb, velocity_data);
119 ephysics_body_event_callback_add(sphere_body, EPHYSICS_CALLBACK_BODY_STOPPED,
120 _update_vel_cb, velocity_data);
121 velocity_data->base.bodies = eina_list_append(velocity_data->base.bodies,
122 sphere_body);
123 velocity_data->body = sphere_body;
124}
125
126static void
127_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
128{
129 Velocity_Data *velocity_data = data;
130
131 elm_object_event_callback_del(velocity_data->base.win, _on_keydown,
132 velocity_data->body);
133
134 DBG("Restart pressed");
135 test_clean((Test_Data *)velocity_data);
136 _world_populate(velocity_data);
137 velocity_data->old_vx = 0;
138 velocity_data->old_vy = 0;
139 velocity_data->old_vaz = 0;
140 velocity_data->last_time = 0;
141
142 elm_object_event_callback_add(velocity_data->base.win, _on_keydown,
143 velocity_data->body);
144}
145
146static void
147_win_del(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
148{
149 Velocity_Data *velocity_data = data;
150
151 test_clean((Test_Data *)velocity_data);
152 evas_object_del(velocity_data->base.layout);
153 ephysics_world_del(velocity_data->base.world);
154 free(velocity_data);
155 ephysics_shutdown();
156}
157
158void
159test_velocity(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
160{
161 Velocity_Data *velocity_data;
162 EPhysics_Body *boundary;
163 EPhysics_World *world;
164
165 if (!ephysics_init())
166 return;
167
168 velocity_data = calloc(1, sizeof(Velocity_Data));
169 if (!velocity_data)
170 {
171 ERR("Failed to create test data");
172 ephysics_shutdown();
173 return;
174 }
175
176 test_win_add((Test_Data *)velocity_data, "Velocity Getters", EINA_FALSE);
177 evas_object_event_callback_add(velocity_data->base.win, EVAS_CALLBACK_DEL,
178 _win_del, velocity_data);
179 elm_layout_signal_callback_add(velocity_data->base.layout, "restart",
180 "test-theme", _restart, velocity_data);
181
182 elm_object_signal_emit(velocity_data->base.layout, "borders,show",
183 "ephysics_test");
184 elm_object_signal_emit(velocity_data->base.layout, "arrows,show",
185 "ephysics_test");
186 elm_object_signal_emit(velocity_data->base.layout, "velocity,show",
187 "ephysics_test");
188
189 world = ephysics_world_new();
190 ephysics_world_render_geometry_set(world, 50, 40, -50,
191 WIDTH - 100, FLOOR_Y - 40, DEPTH);
192 velocity_data->base.world = world;
193
194 boundary = ephysics_body_bottom_boundary_add(velocity_data->base.world);
195 ephysics_body_restitution_set(boundary, 0.65);
196 ephysics_body_friction_set(boundary, 4);
197
198 boundary = ephysics_body_right_boundary_add(velocity_data->base.world);
199 ephysics_body_restitution_set(boundary, 0.4);
200 ephysics_body_friction_set(boundary, 3);
201
202 boundary = ephysics_body_left_boundary_add(velocity_data->base.world);
203 ephysics_body_restitution_set(boundary, 0.4);
204 ephysics_body_friction_set(boundary, 3);
205
206 ephysics_body_top_boundary_add(velocity_data->base.world);
207
208 _world_populate(velocity_data);
209 elm_object_event_callback_add(velocity_data->base.win, _on_keydown,
210 velocity_data->body);
211}