From 3f27034dc7578100af371575322e6f2d0d351f49 Mon Sep 17 00:00:00 2001 From: Bruno Dilly Date: Wed, 12 Sep 2012 21:50:01 +0000 Subject: [PATCH] ephysics: shrink convex shapes to avoid margin gap SVN revision: 76563 --- legacy/ephysics/src/bin/test_shapes.c | 5 +++-- legacy/ephysics/src/lib/ephysics_body.cpp | 22 +++++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/legacy/ephysics/src/bin/test_shapes.c b/legacy/ephysics/src/bin/test_shapes.c index 3547dbbd6b..8780ee463f 100644 --- a/legacy/ephysics/src/bin/test_shapes.c +++ b/legacy/ephysics/src/bin/test_shapes.c @@ -14,7 +14,7 @@ _world_populate(Test_Data *test_data) pentagon = elm_image_add(test_data->win); elm_image_file_set( pentagon, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "pentagon"); - evas_object_move(pentagon, WIDTH / 3, HEIGHT / 2 - 30); + evas_object_move(pentagon, WIDTH / 3, HEIGHT / 2 - 80); evas_object_resize(pentagon, 70, 68); evas_object_show(pentagon); test_data->evas_objs = eina_list_append(test_data->evas_objs, pentagon); @@ -34,7 +34,7 @@ _world_populate(Test_Data *test_data) hexagon = elm_image_add(test_data->win); elm_image_file_set( hexagon, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "hexagon"); - evas_object_move(hexagon, WIDTH / 3 + 80, HEIGHT / 2 - 30); + evas_object_move(hexagon, WIDTH / 3 + 100, HEIGHT / 2 - 100); evas_object_resize(hexagon, 70, 60); evas_object_show(hexagon); test_data->evas_objs = eina_list_append(test_data->evas_objs, hexagon); @@ -51,6 +51,7 @@ _world_populate(Test_Data *test_data) ephysics_body_evas_object_set(hexagon_body, hexagon, EINA_TRUE); ephysics_body_restitution_set(hexagon_body, 1); test_data->bodies = eina_list_append(test_data->bodies, hexagon_body); + ephysics_body_torque_apply(hexagon_body, -3); ephysics_shape_del(pentagon_shape); ephysics_shape_del(hexagon_shape); diff --git a/legacy/ephysics/src/lib/ephysics_body.cpp b/legacy/ephysics/src/lib/ephysics_body.cpp index ddf6a16da4..98f3d13b7f 100644 --- a/legacy/ephysics/src/lib/ephysics_body.cpp +++ b/legacy/ephysics/src/lib/ephysics_body.cpp @@ -4,6 +4,7 @@ #include #include +#include #include @@ -946,10 +947,12 @@ ephysics_body_box_add(EPhysics_World *world) EAPI EPhysics_Body * ephysics_body_shape_add(EPhysics_World *world, EPhysics_Shape *shape) { - btConvexHullShape *full_shape, *simplified_shape; double max_x, max_y, min_x, min_y, cm_x, cm_y, range_x, range_y; + btConvexHullShape *full_shape, *simplified_shape; + btAlignedObjectArray vertexes, planes; const Eina_Inlist *points; EPhysics_Point *point; + int array_size, i; btShapeHull *hull; btVector3 point3d; btScalar margin; @@ -1010,12 +1013,25 @@ ephysics_body_shape_add(EPhysics_World *world, EPhysics_Shape *shape) y = - (point->y - cm_y) / range_y; point3d = btVector3(x, y, -0.5); - full_shape->addPoint(point3d); + vertexes.push_back(point3d); point3d = btVector3(x, y, 0.5); - full_shape->addPoint(point3d); + vertexes.push_back(point3d); } + /* Shrink convex shape to consider margin. Otherwise it would have a gap */ + btGeometryUtil::getPlaneEquationsFromVertices(vertexes, planes); + array_size = planes.size(); + for (i = 0; i < array_size; ++i) + planes[i][3] += full_shape->getMargin(); + + vertexes.clear(); + btGeometryUtil::getVerticesFromPlaneEquations(planes, vertexes); + + array_size = vertexes.size(); + for (i = 0; i < array_size; ++i) + full_shape->addPoint(vertexes[i]); + hull = new btShapeHull(full_shape); if (!hull) {