summaryrefslogtreecommitdiff
path: root/legacy/ephysics
diff options
context:
space:
mode:
authorBruno Dilly <bdilly@profusion.mobi>2012-07-27 14:50:03 +0000
committerBruno Dilly <bdilly@profusion.mobi>2012-07-27 14:50:03 +0000
commit30a6d07376c85c2c230ec8b4d4c2eb6f4c152deb (patch)
tree993dbd8eb2b549fa06e2baa2cc67b8a0ccef14c9 /legacy/ephysics
parent7b6a83144f75bedc1cd1c7fae3d31fb889ff17fd (diff)
EPhysics: implement camera position handling
Now it's possibly to keep default updates of evas objects and has scenes larger than render areas. SVN revision: 74502
Diffstat (limited to 'legacy/ephysics')
-rw-r--r--legacy/ephysics/src/bin/Makefile.am1
-rw-r--r--legacy/ephysics/src/bin/test.c2
-rw-r--r--legacy/ephysics/src/bin/test_camera.c147
-rw-r--r--legacy/ephysics/src/lib/EPhysics.h18
-rw-r--r--legacy/ephysics/src/lib/ephysics_body.cpp15
-rw-r--r--legacy/ephysics/src/lib/ephysics_camera.cpp7
-rw-r--r--legacy/ephysics/src/lib/ephysics_world.cpp1
7 files changed, 177 insertions, 14 deletions
diff --git a/legacy/ephysics/src/bin/Makefile.am b/legacy/ephysics/src/bin/Makefile.am
index 7d6b212f20..fd93bdba7a 100644
--- a/legacy/ephysics/src/bin/Makefile.am
+++ b/legacy/ephysics/src/bin/Makefile.am
@@ -18,6 +18,7 @@ ephysics_test_SOURCES = \
18test.c \ 18test.c \
19test_bouncing_ball.c \ 19test_bouncing_ball.c \
20test_bouncing_text.c \ 20test_bouncing_text.c \
21test_camera.c \
21test_colliding_balls.c \ 22test_colliding_balls.c \
22test_collision_detection.c \ 23test_collision_detection.c \
23test_collision_speed.c \ 24test_collision_speed.c \
diff --git a/legacy/ephysics/src/bin/test.c b/legacy/ephysics/src/bin/test.c
index b9c8b24997..5302da2e48 100644
--- a/legacy/ephysics/src/bin/test.c
+++ b/legacy/ephysics/src/bin/test.c
@@ -11,6 +11,7 @@ int _ephysics_test_log_dom = -1;
11/* examples prototypes */ 11/* examples prototypes */
12void test_bouncing_ball(void *data, Evas_Object *obj, void *event_info); 12void test_bouncing_ball(void *data, Evas_Object *obj, void *event_info);
13void test_bouncing_text(void *data, Evas_Object *obj, void *event_info); 13void test_bouncing_text(void *data, Evas_Object *obj, void *event_info);
14void test_camera(void *data, Evas_Object *obj, void *event_info);
14void test_colliding_balls(void *data, Evas_Object *obj, void *event_info); 15void test_colliding_balls(void *data, Evas_Object *obj, void *event_info);
15void test_collision(void *data, Evas_Object *obj, void *event_info); 16void test_collision(void *data, Evas_Object *obj, void *event_info);
16void test_collision_speed(void *data, Evas_Object *obj, void *event_info); 17void test_collision_speed(void *data, Evas_Object *obj, void *event_info);
@@ -151,6 +152,7 @@ _main_win_add(char *autorun __UNUSED__, Eina_Bool test_win_only __UNUSED__)
151 152
152 ADD_TEST("BOUNCING BALL", test_bouncing_ball); 153 ADD_TEST("BOUNCING BALL", test_bouncing_ball);
153 ADD_TEST("BOUNCING TEXT", test_bouncing_text); 154 ADD_TEST("BOUNCING TEXT", test_bouncing_text);
155 ADD_TEST("CAMERA", test_camera);
154 ADD_TEST("COLLIDING BALLS", test_colliding_balls); 156 ADD_TEST("COLLIDING BALLS", test_colliding_balls);
155 ADD_TEST("COLLISION DETECTION", test_collision); 157 ADD_TEST("COLLISION DETECTION", test_collision);
156 ADD_TEST("COLLISION HIGH SPEED", test_collision_speed); 158 ADD_TEST("COLLISION HIGH SPEED", test_collision_speed);
diff --git a/legacy/ephysics/src/bin/test_camera.c b/legacy/ephysics/src/bin/test_camera.c
new file mode 100644
index 0000000000..1020050aad
--- /dev/null
+++ b/legacy/ephysics/src/bin/test_camera.c
@@ -0,0 +1,147 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ephysics_test.h"
6
7static Eina_Bool
8_camera_move_cb(void *data)
9{
10 Test_Data *test_data = data;
11 EPhysics_Camera *camera;
12 int x, y, w;
13
14 ephysics_world_render_geometry_get(test_data->world, NULL, NULL, &w, NULL);
15
16 camera = ephysics_world_camera_get(test_data->world);
17 ephysics_camera_position_get(camera, &x, &y);
18
19 if (x + w > WIDTH * 2)
20 {
21 test_data->data = NULL;
22 return EINA_FALSE;
23 }
24
25 ephysics_camera_position_set(camera, x + 2, y);
26 return EINA_TRUE;
27}
28
29static void
30_world_populate(Test_Data *test_data)
31{
32 Evas_Object *sphere, *shadow;
33 EPhysics_Body *fall_body;
34
35 shadow = elm_layout_add(test_data->win);
36 elm_layout_file_set(
37 shadow, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-ball");
38 evas_object_move(shadow, WIDTH / 3, FLOOR_Y);
39 evas_object_resize(shadow, 70, 3);
40 evas_object_show(shadow);
41 test_data->evas_objs = eina_list_append(test_data->evas_objs, shadow);
42
43 sphere = elm_image_add(test_data->win);
44 elm_image_file_set(
45 sphere, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "big-blue-ball");
46 evas_object_move(sphere, WIDTH / 3, FLOOR_Y - 70);
47 evas_object_resize(sphere, 70, 70);
48 evas_object_show(sphere);
49 test_data->evas_objs = eina_list_append(test_data->evas_objs, sphere);
50
51 fall_body = ephysics_body_circle_add(test_data->world);
52 ephysics_body_evas_object_set(fall_body, sphere, EINA_TRUE);
53 ephysics_body_restitution_set(fall_body, 0.2);
54 ephysics_body_friction_set(fall_body, 0.5);
55 ephysics_body_event_callback_add(fall_body, EPHYSICS_CALLBACK_BODY_UPDATE,
56 update_object_cb, shadow);
57 test_data->bodies = eina_list_append(test_data->bodies, fall_body);
58 ephysics_body_central_impulse_apply(fall_body, 10, 0);
59
60 shadow = elm_layout_add(test_data->win);
61 elm_layout_file_set(
62 shadow, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "shadow-ball");
63 evas_object_move(shadow, WIDTH * 4 / 3, FLOOR_Y);
64 evas_object_resize(shadow, 70, 3);
65 evas_object_show(shadow);
66 test_data->evas_objs = eina_list_append(test_data->evas_objs, shadow);
67
68 sphere = elm_image_add(test_data->win);
69 elm_image_file_set(
70 sphere, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "big-red-ball");
71 evas_object_move(sphere, WIDTH * 4 / 3, FLOOR_Y - 70);
72 evas_object_resize(sphere, 70, 70);
73 evas_object_show(sphere);
74 test_data->evas_objs = eina_list_append(test_data->evas_objs, sphere);
75
76 fall_body = ephysics_body_circle_add(test_data->world);
77 ephysics_body_mass_set(fall_body, 3.2);
78 ephysics_body_evas_object_set(fall_body, sphere, EINA_TRUE);
79 ephysics_body_restitution_set(fall_body, 0.2);
80 ephysics_body_friction_set(fall_body, 1);
81 ephysics_body_event_callback_add(fall_body, EPHYSICS_CALLBACK_BODY_UPDATE,
82 update_object_cb, shadow);
83 test_data->bodies = eina_list_append(test_data->bodies, fall_body);
84
85 test_data->data = ecore_animator_add(_camera_move_cb, test_data);
86}
87
88static void
89_win_del(void *data, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
90{
91 Test_Data *test_data = data;
92
93 if (test_data->data)
94 ecore_animator_del(test_data->data);
95
96 test_data_del(test_data);
97 ephysics_shutdown();
98}
99
100static void
101_restart(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
102{
103 Test_Data *test_data = data;
104 EPhysics_Camera *camera;
105
106 DBG("Restart pressed");
107
108 if (test_data->data)
109 ecore_animator_del(test_data->data);
110
111 camera = ephysics_world_camera_get(test_data->world);
112 ephysics_camera_position_set(camera, 50, 40);
113
114 test_clean(test_data);
115 _world_populate(test_data);
116}
117
118void
119test_camera(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
120{
121 EPhysics_Body *boundary;
122 EPhysics_World *world;
123 Test_Data *test_data;
124
125 if (!ephysics_init())
126 return;
127
128 test_data = test_data_new();
129 test_win_add(test_data, "Camera", EINA_FALSE);
130 evas_object_smart_callback_add(test_data->win, "delete,request",
131 _win_del, test_data);
132
133 elm_layout_signal_callback_add(test_data->layout, "restart", "test-theme",
134 _restart, test_data);
135
136 world = ephysics_world_new();
137 ephysics_world_render_geometry_set(world, 50, 40, WIDTH - 100, FLOOR_Y - 40);
138 test_data->world = world;
139
140 boundary = ephysics_body_box_add(test_data->world);
141 ephysics_body_mass_set(boundary, 0);
142 ephysics_body_geometry_set(boundary, 0, FLOOR_Y, WIDTH * 2, 10);
143 ephysics_body_restitution_set(boundary, 0.65);
144 ephysics_body_friction_set(boundary, 4);
145
146 _world_populate(test_data);
147}
diff --git a/legacy/ephysics/src/lib/EPhysics.h b/legacy/ephysics/src/lib/EPhysics.h
index 8448c882d9..78d9908333 100644
--- a/legacy/ephysics/src/lib/EPhysics.h
+++ b/legacy/ephysics/src/lib/EPhysics.h
@@ -133,7 +133,7 @@ EAPI int ephysics_shutdown(void);
133 * Its position can be set with @ref ephysics_camera_position_set() and zoom 133 * Its position can be set with @ref ephysics_camera_position_set() and zoom
134 * in / zoom out can be done with @ref ephysics_camera_zoom_set(). 134 * in / zoom out can be done with @ref ephysics_camera_zoom_set().
135 * 135 *
136 * @note WIP: IT'S NOT WORKING YET!! 136 * @note WIP: ZOOM API ISN'T WORKING YET!!
137 */ 137 */
138 138
139typedef struct _EPhysics_Camera EPhysics_Camera; /**< Camera handle, used to zoom in / out a scene, or change the frame position to be rendered. Every world have a camera that can be get with @ref ephysics_world_camera_get(). */ 139typedef struct _EPhysics_Camera EPhysics_Camera; /**< Camera handle, used to zoom in / out a scene, or change the frame position to be rendered. Every world have a camera that can be get with @ref ephysics_world_camera_get(). */
@@ -147,10 +147,16 @@ typedef struct _EPhysics_Camera EPhysics_Camera; /**< Camera handle, used to zoo
147 * 147 *
148 * By default a camera is created to map the first quadrant of physics 148 * By default a camera is created to map the first quadrant of physics
149 * world from the point (0, 0) to 149 * world from the point (0, 0) to
150 * (canvas width / world rate, canvas height / world rate). 150 * (render area width / world rate, render area height / world rate).
151 * 151 *
152 * It can be modified passing another top-left point position, so another 152 * When render area is set with @ref ephysics_world_render_geometry_set(),
153 * region of the physics world will be rendered on the canvas. 153 * the camera geometry is updated to match it. So, for most cases, camera
154 * won't need to be handled by the user.
155 *
156 * But it can be modified passing another top-left point position, so another
157 * region of the physics world will be rendered on the render area.
158 * So if you have a scene larger than the render area, camera handling can
159 * be very useful.
154 * 160 *
155 * @note This change will be noticed on the next physics tick, so evas objects 161 * @note This change will be noticed on the next physics tick, so evas objects
156 * will be updated taking the camera's new position in account. 162 * will be updated taking the camera's new position in account.
@@ -165,7 +171,7 @@ typedef struct _EPhysics_Camera EPhysics_Camera; /**< Camera handle, used to zoo
165 * 171 *
166 * @ingroup EPhysics_Camera 172 * @ingroup EPhysics_Camera
167 */ 173 */
168EAPI void ephysics_camera_position_set(EPhysics_Camera *camera, double x, double y); 174EAPI void ephysics_camera_position_set(EPhysics_Camera *camera, Evas_Coord x, Evas_Coord y);
169 175
170/** 176/**
171 * @brief 177 * @brief
@@ -179,7 +185,7 @@ EAPI void ephysics_camera_position_set(EPhysics_Camera *camera, double x, double
179 * 185 *
180 * @ingroup EPhysics_Camera 186 * @ingroup EPhysics_Camera
181 */ 187 */
182EAPI void ephysics_camera_position_get(const EPhysics_Camera *camera, double *x, double *y); 188EAPI void ephysics_camera_position_get(const EPhysics_Camera *camera, Evas_Coord *x, Evas_Coord *y);
183 189
184/** 190/**
185 * @brief 191 * @brief
diff --git a/legacy/ephysics/src/lib/ephysics_body.cpp b/legacy/ephysics/src/lib/ephysics_body.cpp
index 133c36aac1..063edc6942 100644
--- a/legacy/ephysics/src/lib/ephysics_body.cpp
+++ b/legacy/ephysics/src/lib/ephysics_body.cpp
@@ -247,8 +247,9 @@ _ephysics_body_geometry_set(EPhysics_Body *body, Evas_Coord x, Evas_Coord y, Eva
247static void 247static void
248_ephysics_body_evas_object_default_update(EPhysics_Body *body) 248_ephysics_body_evas_object_default_update(EPhysics_Body *body)
249{ 249{
250 int x, y, w, h, wx, wy, wh, cx, cy;
251 EPhysics_Camera *camera;
250 btTransform trans; 252 btTransform trans;
251 int wy, height, x, y, w, h;
252 double rate, rot; 253 double rate, rot;
253 Evas_Map *map; 254 Evas_Map *map;
254 255
@@ -256,13 +257,17 @@ _ephysics_body_evas_object_default_update(EPhysics_Body *body)
256 return; 257 return;
257 258
258 body->rigid_body->getMotionState()->getWorldTransform(trans); 259 body->rigid_body->getMotionState()->getWorldTransform(trans);
259 ephysics_world_render_geometry_get(body->world, NULL, &wy, NULL, &height); 260 ephysics_world_render_geometry_get(body->world, &wx, &wy, NULL, &wh);
260 height += wy; 261
262 camera = ephysics_world_camera_get(body->world);
263 ephysics_camera_position_get(camera, &cx, &cy);
264 cx -= wx;
265 cy -= wy;
261 266
262 evas_object_geometry_get(body->evas_obj, NULL, NULL, &w, &h); 267 evas_object_geometry_get(body->evas_obj, NULL, NULL, &w, &h);
263 rate = ephysics_world_rate_get(body->world); 268 rate = ephysics_world_rate_get(body->world);
264 x = (int) (trans.getOrigin().getX() * rate) - w / 2; 269 x = (int) (trans.getOrigin().getX() * rate) - w / 2 - cx;
265 y = height - (int) (trans.getOrigin().getY() * rate) - h / 2; 270 y = wh + wy - (int) (trans.getOrigin().getY() * rate) - h / 2 - cy;
266 271
267 evas_object_move(body->evas_obj, x, y); 272 evas_object_move(body->evas_obj, x, y);
268 273
diff --git a/legacy/ephysics/src/lib/ephysics_camera.cpp b/legacy/ephysics/src/lib/ephysics_camera.cpp
index ef03f4f0e7..01f62a471a 100644
--- a/legacy/ephysics/src/lib/ephysics_camera.cpp
+++ b/legacy/ephysics/src/lib/ephysics_camera.cpp
@@ -12,7 +12,8 @@ extern "C" {
12 12
13struct _EPhysics_Camera { 13struct _EPhysics_Camera {
14 EPhysics_World *world; 14 EPhysics_World *world;
15 double x, y, zoom; 15 double zoom;
16 int x, y;
16}; 17};
17 18
18EPhysics_Camera * 19EPhysics_Camera *
@@ -47,7 +48,7 @@ ephysics_camera_del(EPhysics_Camera *camera)
47} 48}
48 49
49EAPI void 50EAPI void
50ephysics_camera_position_set(EPhysics_Camera *camera, double x, double y) 51ephysics_camera_position_set(EPhysics_Camera *camera, Evas_Coord x, Evas_Coord y)
51{ 52{
52 if (!camera) 53 if (!camera)
53 { 54 {
@@ -60,7 +61,7 @@ ephysics_camera_position_set(EPhysics_Camera *camera, double x, double y)
60} 61}
61 62
62EAPI void 63EAPI void
63ephysics_camera_position_get(const EPhysics_Camera *camera, double *x, double *y) 64ephysics_camera_position_get(const EPhysics_Camera *camera, Evas_Coord *x, Evas_Coord *y)
64{ 65{
65 if (!camera) 66 if (!camera)
66 { 67 {
diff --git a/legacy/ephysics/src/lib/ephysics_world.cpp b/legacy/ephysics/src/lib/ephysics_world.cpp
index 2b1e04a3b6..64c4c6ebd6 100644
--- a/legacy/ephysics/src/lib/ephysics_world.cpp
+++ b/legacy/ephysics/src/lib/ephysics_world.cpp
@@ -762,6 +762,7 @@ ephysics_world_render_geometry_set(EPhysics_World *world, Evas_Coord x, Evas_Coo
762 world->h = h; 762 world->h = h;
763 763
764 ephysics_body_world_boundaries_resize(world); 764 ephysics_body_world_boundaries_resize(world);
765 ephysics_camera_position_set(world->camera, x, y);
765} 766}
766 767
767EAPI void 768EAPI void