EPhysics: fix collision detection point
authorBruno Dilly <bdilly@profusion.mobi>
Fri, 27 Jul 2012 14:50:25 +0000 (14:50 +0000)
committerBruno Dilly <bdilly@profusion.mobi>
Fri, 27 Jul 2012 14:50:25 +0000 (14:50 +0000)
Also improve docs and test.

SVN revision: 74504

legacy/ephysics/src/bin/test_collision_detection.c
legacy/ephysics/src/lib/EPhysics.h
legacy/ephysics/src/lib/ephysics_body.cpp

index 7f3b2b5..7755f15 100644 (file)
@@ -25,11 +25,12 @@ _collision_cb(void *data, EPhysics_Body *body __UNUSED__, void *event_info)
    if (contact_body != collision_data->sphere2) return;
 
    ephysics_body_collision_position_get(collision, &x, &y);
-   INF("Collision Detected");
 
-   evas_object_move(collision_data->impact, x, y);
+   evas_object_move(collision_data->impact, x - 10, y - 40);
    elm_object_signal_emit(collision_data->impact, "impact,show",
                           "ephysics_test");
+
+   INF("Collision Detected at (%i, %i)", x, y);
 }
 
 static void
index 78d9908..ad5e358 100644 (file)
@@ -1467,9 +1467,11 @@ EAPI void ephysics_body_damping_get(const EPhysics_Body *body, double *linear_da
  * If not added to any group the body will collide against any other body.
  * Otherwise this body will collide only against those in the same groups.
  *
+ * If @p body was already part of @p group, nothing will happen.
+ *
  * @param body The body to be added to @p group.
  * @param group The group the @p body will belong to.
- * @return EINA_TRUE on success, EINA_FALSE otherwise.
+ * @return @c EINA_TRUE if body is added to group, or @c EINA_FALSE on error.
  *
  * @see ephysics_body_collision_group_del()
  * @see ephysics_body_collision_group_list_get()
@@ -1484,9 +1486,12 @@ EAPI Eina_Bool ephysics_body_collision_group_add(EPhysics_Body *body, const char
  * This @p body will not belong to @p group any more and the collisions filter
  * must take that on account.
  *
+ * If @p body wasn't part of @p group before, nothing will happen.
+ *
  * @param body The body to be removed from @p group.
  * @param group The group @p body must be removed from.
- * @return EINA_TRUE on success, EINA_FALSE otherwise.
+ * @return @c EINA_TRUE if body is removed from group, or @c EINA_FALSE on
+ * error.
  *
  * @see ephysics_body_collision_group_add()
  * @ingroup EPhysics_Body
index 063edc6..3c8c9c9 100644 (file)
@@ -75,8 +75,6 @@ ephysics_body_filter_collision(EPhysics_Body *body0, EPhysics_Body *body1)
 EAPI Eina_Bool
 ephysics_body_collision_group_add(EPhysics_Body *body, const char *group)
 {
-   Eina_List *l;
-   void *grp;
    Eina_Stringshare *group_str;
 
    if (!body)
@@ -86,13 +84,11 @@ ephysics_body_collision_group_add(EPhysics_Body *body, const char *group)
      }
 
    group_str = eina_stringshare_add(group);
-   EINA_LIST_FOREACH(body->collision_groups, l, grp)
+   if (eina_list_data_find(body->collision_groups, group_str))
      {
-        if (grp == group_str)
-          {
-             eina_stringshare_del(group_str);
-             return EINA_TRUE;
-          }
+        INF("Body already added to group: %s", group);
+        eina_stringshare_del(group_str);
+        return EINA_TRUE;
      }
 
    body->collision_groups = eina_list_append(body->collision_groups, group_str);
@@ -111,6 +107,13 @@ ephysics_body_collision_group_del(EPhysics_Body *body, const char *group)
      }
 
    group_str = eina_stringshare_add(group);
+   if (!eina_list_data_find(body->collision_groups, group_str))
+     {
+        INF("Body isn't part of group: %s", group);
+        eina_stringshare_del(group_str);
+        return EINA_TRUE;
+     }
+
    body->collision_groups = eina_list_remove(body->collision_groups, group_str);
    eina_stringshare_del(group_str);
    eina_stringshare_del(group_str);
@@ -331,17 +334,17 @@ ephysics_body_evas_object_update_select(EPhysics_Body *body)
 }
 
 static void
-ephysics_body_collision_set(EPhysics_Body_Collision *collision, EPhysics_Body *contact_body, btVector3 position)
+_ephysics_body_collision_set(EPhysics_Body_Collision *collision, EPhysics_Body *contact_body, btVector3 position)
 {
    double rate;
-   int height;
+   int wy, wh;
    EPhysics_World *world = contact_body->world;
 
-   ephysics_world_render_geometry_get(world, NULL, NULL, NULL, &height);
+   ephysics_world_render_geometry_get(world, NULL, &wy, NULL, &wh);
    rate = ephysics_world_rate_get(world);
    collision->contact_body = contact_body;
    collision->x = position.getX() * rate;
-   collision->y = height - (position.getY() * rate);
+   collision->y = wh + wy - (position.getY() * rate);
 }
 
 EAPI void
@@ -373,7 +376,6 @@ void
 ephysics_body_contact_processed(EPhysics_Body *body, EPhysics_Body *contact_body, btVector3 position)
 {
    EPhysics_Body_Callback *cb;
-   EPhysics_Body_Collision *collision;
 
    if ((!body) || (!contact_body))
      return;
@@ -382,14 +384,18 @@ ephysics_body_contact_processed(EPhysics_Body *body, EPhysics_Body *contact_body
      {
         if (cb->type == EPHYSICS_CALLBACK_BODY_COLLISION)
           {
-             collision = (EPhysics_Body_Collision*)malloc(
-                                              sizeof(EPhysics_Body_Collision));
+             EPhysics_Body_Collision *collision;
+
+             collision = (EPhysics_Body_Collision *)malloc(
+                sizeof(EPhysics_Body_Collision));
+
              if (!collision)
                {
                   ERR("Can't allocate collision data structure.");
                   continue;
                }
-             ephysics_body_collision_set(collision, contact_body, position);
+
+             _ephysics_body_collision_set(collision, contact_body, position);
              cb->func(cb->data, body, collision);
              free(collision);
           }