summaryrefslogtreecommitdiff
path: root/legacy/ephysics
diff options
context:
space:
mode:
authorBruno Dilly <bdilly@profusion.mobi>2012-10-10 19:17:54 +0000
committerBruno Dilly <bdilly@profusion.mobi>2012-10-10 19:17:54 +0000
commitf75457d4b95e26fa485193535ffac43f3fb59a21 (patch)
tree5e8d0702178c08de97fdea9669596de27b22da62 /legacy/ephysics
parent59cc6c6f2fd9f9c991a9d310c2dfc8576304d55c (diff)
ephysics: add new 3d shapes
Instead of creating it from 2d shapes. Bad news is that for some cases basically points will need to be kind of duplicated. SVN revision: 77783
Diffstat (limited to 'legacy/ephysics')
-rw-r--r--legacy/ephysics/src/bin/test_shapes.c33
-rw-r--r--legacy/ephysics/src/lib/EPhysics.h33
-rw-r--r--legacy/ephysics/src/lib/ephysics_body.cpp41
-rw-r--r--legacy/ephysics/src/lib/ephysics_private.h2
-rw-r--r--legacy/ephysics/src/lib/ephysics_shape.cpp3
5 files changed, 71 insertions, 41 deletions
diff --git a/legacy/ephysics/src/bin/test_shapes.c b/legacy/ephysics/src/bin/test_shapes.c
index 1a55840b04..5af0fa549c 100644
--- a/legacy/ephysics/src/bin/test_shapes.c
+++ b/legacy/ephysics/src/bin/test_shapes.c
@@ -20,11 +20,16 @@ _world_populate(Test_Data *test_data)
20 test_data->evas_objs = eina_list_append(test_data->evas_objs, pentagon); 20 test_data->evas_objs = eina_list_append(test_data->evas_objs, pentagon);
21 21
22 pentagon_shape = ephysics_shape_new(); 22 pentagon_shape = ephysics_shape_new();
23 ephysics_shape_point_add(pentagon_shape, -1, -9/33.); 23 ephysics_shape_point_add(pentagon_shape, -1, -9/33., -1);
24 ephysics_shape_point_add(pentagon_shape, 0, -1); 24 ephysics_shape_point_add(pentagon_shape, -1, -9/33., 1);
25 ephysics_shape_point_add(pentagon_shape, 1, -9/33.); 25 ephysics_shape_point_add(pentagon_shape, 0, -1, -1);
26 ephysics_shape_point_add(pentagon_shape, -21/35., 1); 26 ephysics_shape_point_add(pentagon_shape, 0, -1, 1);
27 ephysics_shape_point_add(pentagon_shape, 21/35., 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);
28 33
29 pentagon_body = ephysics_body_shape_add(test_data->world, pentagon_shape); 34 pentagon_body = ephysics_body_shape_add(test_data->world, pentagon_shape);
30 ephysics_body_evas_object_set(pentagon_body, pentagon, EINA_TRUE); 35 ephysics_body_evas_object_set(pentagon_body, pentagon, EINA_TRUE);
@@ -40,12 +45,18 @@ _world_populate(Test_Data *test_data)
40 test_data->evas_objs = eina_list_append(test_data->evas_objs, hexagon); 45 test_data->evas_objs = eina_list_append(test_data->evas_objs, hexagon);
41 46
42 hexagon_shape = ephysics_shape_new(); 47 hexagon_shape = ephysics_shape_new();
43 ephysics_shape_point_add(hexagon_shape, 0, 30); 48 ephysics_shape_point_add(hexagon_shape, 0, 30, -10);
44 ephysics_shape_point_add(hexagon_shape, 18, 0); 49 ephysics_shape_point_add(hexagon_shape, 0, 30, 10);
45 ephysics_shape_point_add(hexagon_shape, 52, 0); 50 ephysics_shape_point_add(hexagon_shape, 18, 0, -10);
46 ephysics_shape_point_add(hexagon_shape, 70, 30); 51 ephysics_shape_point_add(hexagon_shape, 18, 0, 10);
47 ephysics_shape_point_add(hexagon_shape, 52, 60); 52 ephysics_shape_point_add(hexagon_shape, 52, 0, -10);
48 ephysics_shape_point_add(hexagon_shape, 18, 60); 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);
49 60
50 hexagon_body = ephysics_body_shape_add(test_data->world, hexagon_shape); 61 hexagon_body = ephysics_body_shape_add(test_data->world, hexagon_shape);
51 ephysics_body_evas_object_set(hexagon_body, hexagon, EINA_TRUE); 62 ephysics_body_evas_object_set(hexagon_body, hexagon, EINA_TRUE);
diff --git a/legacy/ephysics/src/lib/EPhysics.h b/legacy/ephysics/src/lib/EPhysics.h
index d49492d61d..2d294b6850 100644
--- a/legacy/ephysics/src/lib/EPhysics.h
+++ b/legacy/ephysics/src/lib/EPhysics.h
@@ -214,11 +214,16 @@ EAPI void ephysics_shape_del(EPhysics_Shape *shape);
214 * @code 214 * @code
215 * EPhysics_Shape *shape = ephysics_shape_new(); 215 * EPhysics_Shape *shape = ephysics_shape_new();
216 * 216 *
217 * ephysics_shape_point_add(shape, 0, 24); 217 * ephysics_shape_point_add(shape, 0, 24, -10);
218 * ephysics_shape_point_add(shape, 35, 0); 218 * ephysics_shape_point_add(shape, 0, 24, 10);
219 * ephysics_shape_point_add(shape, 70, 24); 219 * ephysics_shape_point_add(shape, 35, 0, -10);
220 * ephysics_shape_point_add(shape, 56, 66); 220 * ephysics_shape_point_add(shape, 35, 0, 10);
221 * ephysics_shape_point_add(shape, 14, 66); 221 * ephysics_shape_point_add(shape, 70, 24, -10);
222 * ephysics_shape_point_add(shape, 70, 24, 10);
223 * ephysics_shape_point_add(shape, 56, 66, -10);
224 * ephysics_shape_point_add(shape, 56, 66, 10);
225 * ephysics_shape_point_add(shape, 14, 66, -10);
226 * ephysics_shape_point_add(shape, 14, 66, 10);
222 * 227 *
223 * ephysics_body_shape_add(world, shape); 228 * ephysics_body_shape_add(world, shape);
224 * 229 *
@@ -228,13 +233,14 @@ EAPI void ephysics_shape_del(EPhysics_Shape *shape);
228 * @param shape The shape to be modified. 233 * @param shape The shape to be modified.
229 * @param x Point position at x axis. 234 * @param x Point position at x axis.
230 * @param y Point position at y axis. 235 * @param y Point position at y axis.
236 * @param z Point position at z axis.
231 * @return @c EINA_TRUE on success or EINA_FALSE on error. 237 * @return @c EINA_TRUE on success or EINA_FALSE on error.
232 * 238 *
233 * @see ephysics_shape_new(). 239 * @see ephysics_shape_new().
234 * 240 *
235 * @ingroup EPhysics_Shape 241 * @ingroup EPhysics_Shape
236 */ 242 */
237EAPI Eina_Bool ephysics_shape_point_add(EPhysics_Shape *shape, double x, double y); 243EAPI Eina_Bool ephysics_shape_point_add(EPhysics_Shape *shape, double x, double y, double z);
238 244
239/** 245/**
240 * @brief 246 * @brief
@@ -1765,7 +1771,7 @@ EAPI void ephysics_body_cloth_anchor_del(EPhysics_Body *body);
1765 * @ref ephysics_shape_new(). 1771 * @ref ephysics_shape_new().
1766 * 1772 *
1767 * To change it's size @ref ephysics_body_geometry_set() should be used, 1773 * To change it's size @ref ephysics_body_geometry_set() should be used,
1768 * so it can be deformed on x and y axes. 1774 * so it can be deformed on x, y and z axes.
1769 * 1775 *
1770 * The center of mass of this body can be get with 1776 * The center of mass of this body can be get with
1771 * @ref ephysics_body_center_mass_get(). 1777 * @ref ephysics_body_center_mass_get().
@@ -3043,14 +3049,14 @@ EAPI void ephysics_body_forces_clear(EPhysics_Body *body);
3043 * Get the center of mass of physics body. 3049 * Get the center of mass of physics body.
3044 * 3050 *
3045 * It returns a value from 0 to 1 representing where is the center of the mass 3051 * It returns a value from 0 to 1 representing where is the center of the mass
3046 * considering the height and width of the body. 3052 * considering the height, width and depth of the body.
3047 * 3053 *
3048 * If a body of width = 30 and height = 20 has the center of mass at 3054 * If a body of width = 30, height = 20 and depth = 20, and has the center of
3049 * center of mass x component = 20 and y component = 10, it will return 3055 * mass at x component = 20, y component = 10 and z = 10, it will return
3050 * @p x = 0.666 and @p y = 0.5. 3056 * @p x = 0.666, @p y = 0.5 and @z = 0.5.
3051 * 3057 *
3052 * For primitive shapes, like box and circle, the center of mass 3058 * For primitive shapes, like box and circle, the center of mass
3053 * is (0.5, 0.5). 3059 * is (0.5, 0.5, 0.5).
3054 * 3060 *
3055 * This function can be useful when updating evas objects for bodies 3061 * This function can be useful when updating evas objects for bodies
3056 * with custom shapes. 3062 * with custom shapes.
@@ -3058,12 +3064,13 @@ EAPI void ephysics_body_forces_clear(EPhysics_Body *body);
3058 * @param body The physics body. 3064 * @param body The physics body.
3059 * @param x The axis x component of center of mass. 3065 * @param x The axis x component of center of mass.
3060 * @param y The axis y component of center of mass. 3066 * @param y The axis y component of center of mass.
3067 * @param z The axis z component of center of mass.
3061 * 3068 *
3062 * @see ephysics_body_shape_add(). 3069 * @see ephysics_body_shape_add().
3063 * 3070 *
3064 * @ingroup EPhysics_Body 3071 * @ingroup EPhysics_Body
3065 */ 3072 */
3066EAPI void ephysics_body_center_mass_get(const EPhysics_Body *body, double *x, double *y); 3073EAPI void ephysics_body_center_mass_get(const EPhysics_Body *body, double *x, double *y, double *z);
3067 3074
3068/** 3075/**
3069 * @brief 3076 * @brief
diff --git a/legacy/ephysics/src/lib/ephysics_body.cpp b/legacy/ephysics/src/lib/ephysics_body.cpp
index 87a5200a1e..319bacbc97 100644
--- a/legacy/ephysics/src/lib/ephysics_body.cpp
+++ b/legacy/ephysics/src/lib/ephysics_body.cpp
@@ -633,7 +633,7 @@ ephysics_body_collision_group_list_get(const EPhysics_Body *body)
633} 633}
634 634
635static EPhysics_Body * 635static EPhysics_Body *
636_ephysics_body_new(EPhysics_World *world, btScalar mass, double cm_x, double cm_y) 636_ephysics_body_new(EPhysics_World *world, btScalar mass, double cm_x, double cm_y, double cm_z)
637{ 637{
638 EPhysics_Body *body; 638 EPhysics_Body *body;
639 639
@@ -648,12 +648,13 @@ _ephysics_body_new(EPhysics_World *world, btScalar mass, double cm_x, double cm_
648 body->world = world; 648 body->world = world;
649 body->cm.x = cm_x; 649 body->cm.x = cm_x;
650 body->cm.y = cm_y; 650 body->cm.y = cm_y;
651 body->cm.z = cm_z;
651 652
652 return body; 653 return body;
653} 654}
654 655
655static EPhysics_Body * 656static EPhysics_Body *
656_ephysics_body_rigid_body_add(EPhysics_World *world, btCollisionShape *collision_shape, const char *type, double cm_x, double cm_y) 657_ephysics_body_rigid_body_add(EPhysics_World *world, btCollisionShape *collision_shape, const char *type, double cm_x, double cm_y, double cm_z)
657{ 658{
658 btRigidBody::btRigidBodyConstructionInfo *rigid_body_ci; 659 btRigidBody::btRigidBodyConstructionInfo *rigid_body_ci;
659 btDefaultMotionState *motion_state; 660 btDefaultMotionState *motion_state;
@@ -668,7 +669,7 @@ _ephysics_body_rigid_body_add(EPhysics_World *world, btCollisionShape *collision
668 return NULL; 669 return NULL;
669 } 670 }
670 671
671 body = _ephysics_body_new(world, mass, cm_x, cm_y); 672 body = _ephysics_body_new(world, mass, cm_x, cm_y, cm_z);
672 if (!body) 673 if (!body)
673 { 674 {
674 ERR("Couldn't create a new body instance."); 675 ERR("Couldn't create a new body instance.");
@@ -1273,7 +1274,7 @@ _ephysics_body_soft_body_add(EPhysics_World *world, btCollisionShape *collision_
1273 EPhysics_Body *body; 1274 EPhysics_Body *body;
1274 1275
1275 body = _ephysics_body_rigid_body_add(world, collision_shape, "soft box", 0.5, 1276 body = _ephysics_body_rigid_body_add(world, collision_shape, "soft box", 0.5,
1276 0.5); 1277 0.5, 0.5);
1277 if (!body) 1278 if (!body)
1278 { 1279 {
1279 if (body->deleted) return NULL; 1280 if (body->deleted) return NULL;
@@ -1411,7 +1412,7 @@ ephysics_body_cloth_add(EPhysics_World *world, unsigned short granularity)
1411 return NULL; 1412 return NULL;
1412 } 1413 }
1413 1414
1414 body = _ephysics_body_new(world, 1, 0.5, 0.5); 1415 body = _ephysics_body_new(world, 1, 0.5, 0.5, 0.5);
1415 if (!body) 1416 if (!body)
1416 goto no_body; 1417 goto no_body;
1417 1418
@@ -1530,7 +1531,7 @@ ephysics_body_circle_add(EPhysics_World *world)
1530 1531
1531 ephysics_world_lock_take(world); 1532 ephysics_world_lock_take(world);
1532 body = _ephysics_body_rigid_body_add(world, collision_shape, "circle", 0.5, 1533 body = _ephysics_body_rigid_body_add(world, collision_shape, "circle", 0.5,
1533 0.5); 1534 0.5, 0.5);
1534 ephysics_world_lock_release(world); 1535 ephysics_world_lock_release(world);
1535 return body; 1536 return body;
1536} 1537}
@@ -1614,7 +1615,8 @@ ephysics_body_box_add(EPhysics_World *world)
1614 collision_shape = new btBoxShape(btVector3(0.5, 0.5, 0.5)); 1615 collision_shape = new btBoxShape(btVector3(0.5, 0.5, 0.5));
1615 1616
1616 ephysics_world_lock_take(world); 1617 ephysics_world_lock_take(world);
1617 body = _ephysics_body_rigid_body_add(world, collision_shape, "box", 0.5, 0.5); 1618 body = _ephysics_body_rigid_body_add(world, collision_shape, "box", 0.5,
1619 0.5, 0.5);
1618 ephysics_world_lock_release(world); 1620 ephysics_world_lock_release(world);
1619 return body; 1621 return body;
1620} 1622}
@@ -1622,7 +1624,8 @@ ephysics_body_box_add(EPhysics_World *world)
1622EAPI EPhysics_Body * 1624EAPI EPhysics_Body *
1623ephysics_body_shape_add(EPhysics_World *world, EPhysics_Shape *shape) 1625ephysics_body_shape_add(EPhysics_World *world, EPhysics_Shape *shape)
1624{ 1626{
1625 double max_x, max_y, min_x, min_y, cm_x, cm_y, range_x, range_y; 1627 double max_x, max_y, max_z, min_x, min_y, min_z, cm_x, cm_y, cm_z,
1628 range_x, range_y, range_z;
1626 btConvexHullShape *full_shape, *simplified_shape; 1629 btConvexHullShape *full_shape, *simplified_shape;
1627 btAlignedObjectArray<btVector3> vertexes, planes; 1630 btAlignedObjectArray<btVector3> vertexes, planes;
1628 const Eina_Inlist *points; 1631 const Eina_Inlist *points;
@@ -1662,7 +1665,8 @@ ephysics_body_shape_add(EPhysics_World *world, EPhysics_Shape *shape)
1662 point = EINA_INLIST_CONTAINER_GET(points, EPhysics_Point); 1665 point = EINA_INLIST_CONTAINER_GET(points, EPhysics_Point);
1663 max_x = min_x = point->x; 1666 max_x = min_x = point->x;
1664 max_y = min_y = point->y; 1667 max_y = min_y = point->y;
1665 cm_x = cm_y = 0; 1668 max_z = min_z = point->z;
1669 cm_x = cm_y = cm_z = 0;
1666 1670
1667 /* FIXME : only vertices should be used to calculate the center of mass */ 1671 /* FIXME : only vertices should be used to calculate the center of mass */
1668 EINA_INLIST_FOREACH(points, point) 1672 EINA_INLIST_FOREACH(points, point)
@@ -1671,27 +1675,30 @@ ephysics_body_shape_add(EPhysics_World *world, EPhysics_Shape *shape)
1671 if (point->x < min_x) min_x = point->x; 1675 if (point->x < min_x) min_x = point->x;
1672 if (point->y > max_y) max_y = point->y; 1676 if (point->y > max_y) max_y = point->y;
1673 if (point->y < min_y) min_y = point->y; 1677 if (point->y < min_y) min_y = point->y;
1678 if (point->z > max_z) max_z = point->z;
1679 if (point->z < min_z) min_z = point->z;
1674 1680
1675 cm_x += point->x; 1681 cm_x += point->x;
1676 cm_y += point->y; 1682 cm_y += point->y;
1683 cm_z += point->z;
1677 } 1684 }
1678 1685
1679 cm_x /= eina_inlist_count(points); 1686 cm_x /= eina_inlist_count(points);
1680 cm_y /= eina_inlist_count(points); 1687 cm_y /= eina_inlist_count(points);
1688 cm_z /= eina_inlist_count(points);
1681 range_x = max_x - min_x; 1689 range_x = max_x - min_x;
1682 range_y = max_y - min_y; 1690 range_y = max_y - min_y;
1691 range_z = max_z - min_z;
1683 1692
1684 EINA_INLIST_FOREACH(points, point) 1693 EINA_INLIST_FOREACH(points, point)
1685 { 1694 {
1686 double x, y; 1695 double x, y, z;
1687 1696
1688 x = (point->x - cm_x) / range_x; 1697 x = (point->x - cm_x) / range_x;
1689 y = - (point->y - cm_y) / range_y; 1698 y = - (point->y - cm_y) / range_y;
1699 z = (point->z - cm_z) / range_z;
1690 1700
1691 point3d = btVector3(x, y, -0.5); 1701 point3d = btVector3(x, y, z);
1692 vertexes.push_back(point3d);
1693
1694 point3d = btVector3(x, y, 0.5);
1695 vertexes.push_back(point3d); 1702 vertexes.push_back(point3d);
1696 } 1703 }
1697 1704
@@ -1732,7 +1739,8 @@ ephysics_body_shape_add(EPhysics_World *world, EPhysics_Shape *shape)
1732 body = _ephysics_body_rigid_body_add(world, 1739 body = _ephysics_body_rigid_body_add(world,
1733 (btCollisionShape *)simplified_shape, 1740 (btCollisionShape *)simplified_shape,
1734 "generic", (cm_x - min_x) / range_x, 1741 "generic", (cm_x - min_x) / range_x,
1735 1 - (cm_y - min_y) / range_y); 1742 1 - (cm_y - min_y) / range_y,
1743 (cm_z - min_z) / range_z);
1736 ephysics_world_lock_release(world); 1744 ephysics_world_lock_release(world);
1737 return body; 1745 return body;
1738} 1746}
@@ -2723,7 +2731,7 @@ ephysics_body_forces_clear(EPhysics_Body *body)
2723} 2731}
2724 2732
2725EAPI void 2733EAPI void
2726ephysics_body_center_mass_get(const EPhysics_Body *body, double *x, double *y) 2734ephysics_body_center_mass_get(const EPhysics_Body *body, double *x, double *y, double *z)
2727{ 2735{
2728 if (!body) 2736 if (!body)
2729 { 2737 {
@@ -2733,6 +2741,7 @@ ephysics_body_center_mass_get(const EPhysics_Body *body, double *x, double *y)
2733 2741
2734 if (x) *x = body->cm.x; 2742 if (x) *x = body->cm.x;
2735 if (y) *y = body->cm.y; 2743 if (y) *y = body->cm.y;
2744 if (z) *z = body->cm.z;
2736} 2745}
2737 2746
2738EAPI void 2747EAPI void
diff --git a/legacy/ephysics/src/lib/ephysics_private.h b/legacy/ephysics/src/lib/ephysics_private.h
index 5d624fb16b..56dcb6e845 100644
--- a/legacy/ephysics/src/lib/ephysics_private.h
+++ b/legacy/ephysics/src/lib/ephysics_private.h
@@ -63,6 +63,7 @@ struct _EPhysics_Point {
63 EINA_INLIST; 63 EINA_INLIST;
64 double x; 64 double x;
65 double y; 65 double y;
66 double z;
66}; 67};
67 68
68typedef enum _EPhysics_Body_Type 69typedef enum _EPhysics_Body_Type
@@ -101,6 +102,7 @@ struct _EPhysics_Body {
101 struct { 102 struct {
102 double x; 103 double x;
103 double y; 104 double y;
105 double z;
104 } cm; 106 } cm;
105 Eina_Bool active:1; 107 Eina_Bool active:1;
106 Eina_Bool deleted:1; 108 Eina_Bool deleted:1;
diff --git a/legacy/ephysics/src/lib/ephysics_shape.cpp b/legacy/ephysics/src/lib/ephysics_shape.cpp
index c2e1e18f7d..6aba343ed6 100644
--- a/legacy/ephysics/src/lib/ephysics_shape.cpp
+++ b/legacy/ephysics/src/lib/ephysics_shape.cpp
@@ -70,7 +70,7 @@ ephysics_shape_del(EPhysics_Shape *shape)
70} 70}
71 71
72EAPI Eina_Bool 72EAPI Eina_Bool
73ephysics_shape_point_add(EPhysics_Shape *shape, double x, double y) 73ephysics_shape_point_add(EPhysics_Shape *shape, double x, double y, double z)
74{ 74{
75 EPhysics_Point *point; 75 EPhysics_Point *point;
76 76
@@ -86,6 +86,7 @@ ephysics_shape_point_add(EPhysics_Shape *shape, double x, double y)
86 86
87 point->x = x; 87 point->x = x;
88 point->y = y; 88 point->y = y;
89 point->z = z;
89 90
90 shape->points = eina_inlist_append(shape->points, EINA_INLIST_GET(point)); 91 shape->points = eina_inlist_append(shape->points, EINA_INLIST_GET(point));
91 92