summaryrefslogtreecommitdiff
path: root/legacy/ephysics
diff options
context:
space:
mode:
authorLeandro Dorileo <dorileo@profusion.mobi>2012-07-27 14:49:37 +0000
committerBruno Dilly <bdilly@profusion.mobi>2012-07-27 14:49:37 +0000
commit6a2d290fcec8793cf0777deaf3cbaf33c70edb48 (patch)
treeec66005fd5fc206bc18b83be3aa441362abf854f /legacy/ephysics
parent34734a4a72ccf408ae7ed264f014f42dad2a05d8 (diff)
EPhysics: body collision data
This patch introduces a wrapper(EPhysics_Body_Collision) to hold the collision data(the contact body and collision position). This data type is used by EPHYSICS_CALLBACK_BODY_COLLISION callback. The collision detection test is migrated to the new API as well. Patch by: Leandro Dorileo <dorileo@profusion.mobi> SVN revision: 74500
Diffstat (limited to 'legacy/ephysics')
-rw-r--r--legacy/ephysics/src/bin/test_collision_detection.c15
-rw-r--r--legacy/ephysics/src/lib/EPhysics.h72
-rw-r--r--legacy/ephysics/src/lib/ephysics_body.cpp61
-rw-r--r--legacy/ephysics/src/lib/ephysics_private.h2
-rw-r--r--legacy/ephysics/src/lib/ephysics_world.cpp4
5 files changed, 138 insertions, 16 deletions
diff --git a/legacy/ephysics/src/bin/test_collision_detection.c b/legacy/ephysics/src/bin/test_collision_detection.c
index c87d599e52..7f3b2b534e 100644
--- a/legacy/ephysics/src/bin/test_collision_detection.c
+++ b/legacy/ephysics/src/bin/test_collision_detection.c
@@ -14,19 +14,20 @@ struct _Collision_Data {
14}; 14};
15 15
16static void 16static void
17_collision_cb(void *data, EPhysics_Body *body __UNUSED__, void *event_info __UNUSED__) 17_collision_cb(void *data, EPhysics_Body *body __UNUSED__, void *event_info)
18{ 18{
19 EPhysics_Body *contact_body;
19 Collision_Data *collision_data = data; 20 Collision_Data *collision_data = data;
20 Evas_Object *obj; 21 EPhysics_Body_Collision *collision = event_info;
21 int x; 22 int x, y;
22 23
23 if (event_info != collision_data->sphere2) return; 24 contact_body = ephysics_body_collision_contact_body_get(collision);
25 if (contact_body != collision_data->sphere2) return;
24 26
27 ephysics_body_collision_position_get(collision, &x, &y);
25 INF("Collision Detected"); 28 INF("Collision Detected");
26 29
27 obj = ephysics_body_evas_object_get(collision_data->sphere); 30 evas_object_move(collision_data->impact, x, y);
28 evas_object_geometry_get(obj, &x, NULL, NULL, NULL);
29 evas_object_move(collision_data->impact, x - 4, FLOOR_Y - 80);
30 elm_object_signal_emit(collision_data->impact, "impact,show", 31 elm_object_signal_emit(collision_data->impact, "impact,show",
31 "ephysics_test"); 32 "ephysics_test");
32} 33}
diff --git a/legacy/ephysics/src/lib/EPhysics.h b/legacy/ephysics/src/lib/EPhysics.h
index 0732be68fa..0be3ec4dec 100644
--- a/legacy/ephysics/src/lib/EPhysics.h
+++ b/legacy/ephysics/src/lib/EPhysics.h
@@ -923,6 +923,24 @@ EAPI Eina_Bool ephysics_world_bodies_outside_left_autodel_get(const EPhysics_Wor
923typedef struct _EPhysics_Body EPhysics_Body; /**< Body handle, represents an object on EPhysics world. Created with @ref ephysics_body_circle_add() or @ref ephysics_body_box_add() and deleted with @ref ephysics_body_del(). */ 923typedef struct _EPhysics_Body EPhysics_Body; /**< Body handle, represents an object on EPhysics world. Created with @ref ephysics_body_circle_add() or @ref ephysics_body_box_add() and deleted with @ref ephysics_body_del(). */
924 924
925/** 925/**
926 * @typedef EPhysics_Body_Collision
927 *
928 * Body collision wraps collision informations.
929 *
930 * EPhysics_Body_Collision is used on EPHYSICS_CALLBACK_BODY_COLLISION callback
931 * and is mostly interested to hold informations like:
932 * @li contact_body - the body which the collision occurred against;
933 * @li position - points the position where the collision happened;
934 *
935 * @see ephysics_body_collision_position_get()
936 * @see ephysics_body_collision_contact_body_get()
937 * @see EPHYSICS_CALLBACK_BODY_COLLISION and @ref
938 * ephysics_body_event_callback_add() for collision callback.
939 * @ingroup EPhysics_Body
940 */
941typedef struct _EPhysics_Body_Collision EPhysics_Body_Collision;
942
943/**
926 * @enum _EPhysics_Callback_Body_Type 944 * @enum _EPhysics_Callback_Body_Type
927 * @typedef EPhysics_Callback_Body_Type 945 * @typedef EPhysics_Callback_Body_Type
928 * 946 *
@@ -1504,12 +1522,25 @@ EAPI void ephysics_body_evas_object_update(EPhysics_Body *body);
1504 * 1522 *
1505 * Update callbacks receives evas object set to body as event_info argument. 1523 * Update callbacks receives evas object set to body as event_info argument.
1506 * 1524 *
1507 * Regarding EPHYSICS_CALLBACK_BODY_COLLISION: 1525 * What follows is a list of details about each callback type:
1508 * 1526 *
1509 * Callbacks are called just after the collision has been actually processed 1527 * - #EPHYSICS_CALLBACK_BODY_UPDATE: Called after every physics iteration. @p
1510 * by the physics engine. 1528 * body points to the EPhysics_Body itself and @p event_info points to the
1529 * evas object associated to the body.
1511 * 1530 *
1512 * The other body involved in the collision is passed as event_info argument. 1531 * - #EPHYSICS_CALLBACK_BODY_COLLISION: Called just after the collision has
1532 * been actually processed by the physics engine. The body involved in the
1533 * collision is passed as @p body argument. @p event_info is a pointer to
1534 * @ref EPhysics_Body_Collision - note, this structure(@p event_info) is
1535 * discarded/freed right after callback returns.
1536 *
1537 * - #EPHYSICS_CALLBACK_BODY_DEL: Called when a body deletion has been issued
1538 * and just before the deletion actually happens. @p body points to the body
1539 * being deleted and no @p event_info is provided.
1540 *
1541 * - #EPHYSICS_CALLBACK_BODY_STOPPED: Called when a body is found to be
1542 * stopped. @p body points to the body of interest and @p event_info is a
1543 * pointer to the evas object associated to it.
1513 * 1544 *
1514 * @param body The physics body. 1545 * @param body The physics body.
1515 * @param type Type of callback to be listened by @p func. 1546 * @param type Type of callback to be listened by @p func.
@@ -1566,6 +1597,39 @@ EAPI void *ephysics_body_event_callback_del_full(EPhysics_Body *body, EPhysics_C
1566 1597
1567/** 1598/**
1568 * @brief 1599 * @brief
1600 * Get the position(x, y) of a body's collision.
1601 *
1602 * Given a body collision data, fills @p x and @p y pointers with the position
1603 * where the collision occurred.
1604 *
1605 * @param collision The body collision data of interest.
1606 * @param x The x pointer to set the x coordinate to.
1607 * @param y The y pointer to set the y coordinate to.
1608 *
1609 * @see EPHYSICS_CALLBACK_BODY_COLLISION and @ref
1610 * ephysics_body_event_callback_add() for collision callback.
1611 * @ingroup EPhysics_Body
1612 */
1613EAPI void ephysics_body_collision_position_get(const EPhysics_Body_Collision *collision, Evas_Coord *x, Evas_Coord *y);
1614
1615/**
1616 * @brief
1617 * Get the body's collision contact body.
1618 *
1619 * Given a body collision data returns the contact body which a collision
1620 * occurred against.
1621 *
1622 * @param collision The body collision of interest.
1623 * @return The contact body of @p collision.
1624 *
1625 * @see EPHYSICS_CALLBACK_BODY_COLLISION and @ref
1626 * ephysics_body_event_callback_add() for collision callback.
1627 * @ingroup EPhysics_Body
1628 */
1629EAPI EPhysics_Body *ephysics_body_collision_contact_body_get(const EPhysics_Body_Collision *collision);
1630
1631/**
1632 * @brief
1569 * Set body's coefficient of restitution. 1633 * Set body's coefficient of restitution.
1570 * 1634 *
1571 * The coefficient of restitution is proporcion between speed after and 1635 * The coefficient of restitution is proporcion between speed after and
diff --git a/legacy/ephysics/src/lib/ephysics_body.cpp b/legacy/ephysics/src/lib/ephysics_body.cpp
index 3d351bbe4e..f7be1878b8 100644
--- a/legacy/ephysics/src/lib/ephysics_body.cpp
+++ b/legacy/ephysics/src/lib/ephysics_body.cpp
@@ -32,6 +32,12 @@ struct _EPhysics_Body {
32 Eina_Bool active:1; 32 Eina_Bool active:1;
33}; 33};
34 34
35struct _EPhysics_Body_Collision {
36 EPhysics_Body *contact_body;
37 Evas_Coord x;
38 Evas_Coord y;
39};
40
35void 41void
36ephysics_body_active_set(EPhysics_Body *body, Eina_Bool active) 42ephysics_body_active_set(EPhysics_Body *body, Eina_Bool active)
37{ 43{
@@ -237,10 +243,50 @@ ephysics_body_evas_object_update_select(EPhysics_Body *body)
237 _ephysics_body_outside_render_area_check(body); 243 _ephysics_body_outside_render_area_check(body);
238} 244}
239 245
246static void
247ephysics_body_collision_set(EPhysics_Body_Collision *collision, EPhysics_Body *contact_body, btVector3 position)
248{
249 double rate;
250 int height;
251 EPhysics_World *world = contact_body->world;
252
253 ephysics_world_render_geometry_get(world, NULL, NULL, NULL, &height);
254 rate = ephysics_world_rate_get(world);
255 collision->contact_body = contact_body;
256 collision->x = position.getX() * rate;
257 collision->y = height - (position.getY() * rate);
258}
259
260EAPI void
261ephysics_body_collision_position_get(const EPhysics_Body_Collision *collision, Evas_Coord *x, Evas_Coord *y)
262{
263 if (!collision)
264 {
265 ERR("Can't get body's collision data, collision is null.");
266 return;
267 }
268
269 if (x) *x = collision->x;
270 if (y) *y = collision->y;
271}
272
273EAPI EPhysics_Body *
274ephysics_body_collision_contact_body_get(const EPhysics_Body_Collision *collision)
275{
276 if (!collision)
277 {
278 ERR("Can't get body's collision contact body, collision is null.");
279 return NULL;
280 }
281
282 return collision->contact_body;
283}
284
240void 285void
241ephysics_body_contact_processed(EPhysics_Body *body, EPhysics_Body *contact_body) 286ephysics_body_contact_processed(EPhysics_Body *body, EPhysics_Body *contact_body, btVector3 position)
242{ 287{
243 EPhysics_Body_Callback *cb; 288 EPhysics_Body_Callback *cb;
289 EPhysics_Body_Collision *collision;
244 290
245 if ((!body) || (!contact_body)) 291 if ((!body) || (!contact_body))
246 return; 292 return;
@@ -248,7 +294,18 @@ ephysics_body_contact_processed(EPhysics_Body *body, EPhysics_Body *contact_body
248 EINA_INLIST_FOREACH(body->callbacks, cb) 294 EINA_INLIST_FOREACH(body->callbacks, cb)
249 { 295 {
250 if (cb->type == EPHYSICS_CALLBACK_BODY_COLLISION) 296 if (cb->type == EPHYSICS_CALLBACK_BODY_COLLISION)
251 cb->func(cb->data, body, (void *) contact_body); 297 {
298 collision = (EPhysics_Body_Collision*)malloc(
299 sizeof(EPhysics_Body_Collision));
300 if (!collision)
301 {
302 ERR("Can't allocate collision data structure.");
303 continue;
304 }
305 ephysics_body_collision_set(collision, contact_body, position);
306 cb->func(cb->data, body, collision);
307 free(collision);
308 }
252 } 309 }
253} 310}
254 311
diff --git a/legacy/ephysics/src/lib/ephysics_private.h b/legacy/ephysics/src/lib/ephysics_private.h
index 0970ba4c01..8ef80422ed 100644
--- a/legacy/ephysics/src/lib/ephysics_private.h
+++ b/legacy/ephysics/src/lib/ephysics_private.h
@@ -66,7 +66,7 @@ Eina_Bool ephysics_world_bodies_outside_autodel_get(const EPhysics_World *world)
66 66
67void ephysics_body_evas_object_update_select(EPhysics_Body *body); 67void ephysics_body_evas_object_update_select(EPhysics_Body *body);
68void ephysics_orphan_body_del(EPhysics_Body *body); 68void ephysics_orphan_body_del(EPhysics_Body *body);
69void ephysics_body_contact_processed(EPhysics_Body *body, EPhysics_Body *contact_body); 69void ephysics_body_contact_processed(EPhysics_Body *body, EPhysics_Body *contact_body, btVector3 position);
70btRigidBody *ephysics_body_rigid_body_get(const EPhysics_Body *body); 70btRigidBody *ephysics_body_rigid_body_get(const EPhysics_Body *body);
71void ephysics_body_active_set(EPhysics_Body *body, Eina_Bool active); 71void ephysics_body_active_set(EPhysics_Body *body, Eina_Bool active);
72 72
diff --git a/legacy/ephysics/src/lib/ephysics_world.cpp b/legacy/ephysics/src/lib/ephysics_world.cpp
index f29fdbe58d..d2c20a4b06 100644
--- a/legacy/ephysics/src/lib/ephysics_world.cpp
+++ b/legacy/ephysics/src/lib/ephysics_world.cpp
@@ -123,8 +123,8 @@ _ephysics_world_contact_processed_cb(btManifoldPoint &cp, void *b0, void *b1)
123 body_0 = (EPhysics_Body *) rigid_body_0->getUserPointer(); 123 body_0 = (EPhysics_Body *) rigid_body_0->getUserPointer();
124 body_1 = (EPhysics_Body *) rigid_body_1->getUserPointer(); 124 body_1 = (EPhysics_Body *) rigid_body_1->getUserPointer();
125 125
126 ephysics_body_contact_processed(body_0, body_1); 126 ephysics_body_contact_processed(body_0, body_1, cp.getPositionWorldOnA());
127 ephysics_body_contact_processed(body_1, body_0); 127 ephysics_body_contact_processed(body_1, body_0, cp.getPositionWorldOnB());
128 128
129 return EINA_TRUE; 129 return EINA_TRUE;
130} 130}