summaryrefslogtreecommitdiff
path: root/legacy/ephysics
diff options
context:
space:
mode:
authorBruno Dilly <bdilly@profusion.mobi>2012-10-04 22:39:42 +0000
committerBruno Dilly <bdilly@profusion.mobi>2012-10-04 22:39:42 +0000
commit0bdec6725538665680ba77a84b21a745c7462380 (patch)
treec73adc53ab94baa2dc29a7de3b8a8763c60f1db9 /legacy/ephysics
parentf0b85f701d2c55ad70c37e85b57abb5819ceecf5 (diff)
ephysics: support body material
Using material_set it will be easier to set density, friction and restitution to a body, simulating properties of the selected material. SVN revision: 77462
Diffstat (limited to 'legacy/ephysics')
-rw-r--r--legacy/ephysics/src/lib/EPhysics.h62
-rw-r--r--legacy/ephysics/src/lib/Makefile.am2
-rw-r--r--legacy/ephysics/src/lib/ephysics_body.cpp69
-rw-r--r--legacy/ephysics/src/lib/ephysics_body_materials.h37
-rw-r--r--legacy/ephysics/src/lib/ephysics_private.h1
5 files changed, 166 insertions, 5 deletions
diff --git a/legacy/ephysics/src/lib/EPhysics.h b/legacy/ephysics/src/lib/EPhysics.h
index afd6dc9c56..d102e2b3b4 100644
--- a/legacy/ephysics/src/lib/EPhysics.h
+++ b/legacy/ephysics/src/lib/EPhysics.h
@@ -1465,6 +1465,28 @@ typedef enum _EPhysics_Callback_Body_Type
1465 EPHYSICS_CALLBACK_BODY_LAST, /**< kept as sentinel, not really an event */ 1465 EPHYSICS_CALLBACK_BODY_LAST, /**< kept as sentinel, not really an event */
1466} EPhysics_Callback_Body_Type; /**< The types of events triggering a callback */ 1466} EPhysics_Callback_Body_Type; /**< The types of events triggering a callback */
1467 1467
1468/**
1469 * @enum _EPhysics_Body_Material
1470 * @typedef EPhysics_Body_Material
1471 *
1472 * EPhysics bodies materials. Each material has specific properties to be
1473 * applied on the body, as density, friction and restitution.
1474 *
1475 * @see ephysics_body_material_set() for details.
1476 *
1477 * @ingroup EPhysics_Body
1478 */
1479typedef enum _EPhysics_Body_Material
1480{
1481 EPHYSICS_BODY_MATERIAL_CUSTOM, /**< Custom properties set by the user */
1482 EPHYSICS_BODY_MATERIAL_CONCRETE, /**< Density:2300,Fric:0.65,Rest:0.75 */
1483 EPHYSICS_BODY_MATERIAL_IRON, /**< Density:7400,Fric:0.8,Rest:0.85 */
1484 EPHYSICS_BODY_MATERIAL_PLASTIC, /**< Density:1300,Fric:0.35,Rest:0.6 */
1485 EPHYSICS_BODY_MATERIAL_POLYSTYRENE, /**< Density:80,Fric:0.5,Rest:0.5*/
1486 EPHYSICS_BODY_MATERIAL_RUBBER, /**< Density:920,Fric:0.75,Rest:0.3*/
1487 EPHYSICS_BODY_MATERIAL_WOOD, /**< Density:680,Fric:0.4,Rest:0.7*/
1488 EPHYSICS_BODY_MATERIAL_LAST, /**< kept as sentinel, not really a material */
1489} EPhysics_Body_Material; /**< The types of materials to be set on a body */
1468 1490
1469/** 1491/**
1470 * @typedef EPhysics_Body_Event_Cb 1492 * @typedef EPhysics_Body_Event_Cb
@@ -2893,6 +2915,46 @@ EAPI void ephysics_body_density_set(EPhysics_Body *body, double density);
2893EAPI double ephysics_body_density_get(const EPhysics_Body *body); 2915EAPI double ephysics_body_density_get(const EPhysics_Body *body);
2894 2916
2895/** 2917/**
2918 * @brief
2919 * Set body's material.
2920 *
2921 * This function makes properties setting easy. When a material is set to
2922 * a body, it will update its friction, restitution and density,
2923 * recalculating its mass.
2924 *
2925 * EPhysics support some materials defined by @ref EPhysics_Body_Material.
2926 *
2927 * So, for example, if @ref EPHYSICS_BODY_MATERIAL_WOOD is set, it will
2928 * set body's density to @ref EPHYSICS_BODY_DENSITY_WOOD, restitution
2929 * to @ref EPHSYICS_BODY_RESTITUTION_WOOD and friction to
2930 * @ref EPHYSICS_BODY_FRICTION_WOOD.
2931 *
2932 * If any of these values are later explicitely set, the material will
2933 * be set back to @ref EPHYSICS_BODY_MATERIAL_CUSTOM, the default.
2934 *
2935 * @param body The body to has its material set.
2936 * @param material The @p material to be used by the body.
2937 *
2938 * @see ephysics_body_material_get().
2939 *
2940 * @ingroup EPhysics_Body
2941 */
2942EAPI void ephysics_body_material_set(EPhysics_Body *body, EPhysics_Body_Material material);
2943
2944/**
2945 * @brief
2946 * Get body's material.
2947 *
2948 * @param body The physics body.
2949 * @return the @p material used by the body.
2950 *
2951 * @see ephysics_body_body_set() for more details.
2952 *
2953 * @ingroup EPhysics_Body
2954 */
2955EAPI EPhysics_Body_Material ephysics_body_material_get(const EPhysics_Body *body);
2956
2957/**
2896 * @} 2958 * @}
2897 */ 2959 */
2898 2960
diff --git a/legacy/ephysics/src/lib/Makefile.am b/legacy/ephysics/src/lib/Makefile.am
index 5e040b3951..c91e5fb786 100644
--- a/legacy/ephysics/src/lib/Makefile.am
+++ b/legacy/ephysics/src/lib/Makefile.am
@@ -26,4 +26,4 @@ libephysics_la_SOURCES = $(base_sources)
26libephysics_la_LIBADD = @EPHYSICS_LIBS@ @EVIL_LIBS@ -lm 26libephysics_la_LIBADD = @EPHYSICS_LIBS@ @EVIL_LIBS@ -lm
27libephysics_la_LDFLAGS = -no-undefined @lt_enable_auto_import@ -version-info @version_info@ @release_info@ 27libephysics_la_LDFLAGS = -no-undefined @lt_enable_auto_import@ -version-info @version_info@ @release_info@
28 28
29EXTRA_DIST = ephysics_private.h ephysics_trimesh.h 29EXTRA_DIST = ephysics_private.h ephysics_trimesh.h ephysics_body_materials.h
diff --git a/legacy/ephysics/src/lib/ephysics_body.cpp b/legacy/ephysics/src/lib/ephysics_body.cpp
index c03a8db705..30df114c34 100644
--- a/legacy/ephysics/src/lib/ephysics_body.cpp
+++ b/legacy/ephysics/src/lib/ephysics_body.cpp
@@ -10,6 +10,7 @@
10 10
11#include "ephysics_private.h" 11#include "ephysics_private.h"
12#include "ephysics_trimesh.h" 12#include "ephysics_trimesh.h"
13#include "ephysics_body_materials.h"
13 14
14#ifdef __cplusplus 15#ifdef __cplusplus
15extern "C" { 16extern "C" {
@@ -1418,6 +1419,7 @@ ephysics_body_mass_set(EPhysics_Body *body, double mass)
1418 } 1419 }
1419 1420
1420 ephysics_world_lock_take(body->world); 1421 ephysics_world_lock_take(body->world);
1422 body->material = EPHYSICS_BODY_MATERIAL_CUSTOM;
1421 body->density = 0; 1423 body->density = 0;
1422 _ephysics_body_mass_set(body, mass); 1424 _ephysics_body_mass_set(body, mass);
1423 ephysics_world_lock_release(body->world); 1425 ephysics_world_lock_release(body->world);
@@ -1673,6 +1675,13 @@ ephysics_body_event_callback_del_full(EPhysics_Body *body, EPhysics_Callback_Bod
1673 return cb_data; 1675 return cb_data;
1674} 1676}
1675 1677
1678static void
1679_ephysics_body_restitution_set(EPhysics_Body *body, double restitution)
1680{
1681 body->rigid_body->setRestitution(btScalar(restitution));
1682 DBG("Body %p restitution set to %lf", body, restitution);
1683}
1684
1676EAPI void 1685EAPI void
1677ephysics_body_restitution_set(EPhysics_Body *body, double restitution) 1686ephysics_body_restitution_set(EPhysics_Body *body, double restitution)
1678{ 1687{
@@ -1683,8 +1692,8 @@ ephysics_body_restitution_set(EPhysics_Body *body, double restitution)
1683 } 1692 }
1684 1693
1685 ephysics_world_lock_take(body->world); 1694 ephysics_world_lock_take(body->world);
1686 body->rigid_body->setRestitution(btScalar(restitution)); 1695 body->material = EPHYSICS_BODY_MATERIAL_CUSTOM;
1687 DBG("Body %p restitution set to %lf", body, restitution); 1696 _ephysics_body_restitution_set(body, restitution);
1688 ephysics_world_lock_release(body->world); 1697 ephysics_world_lock_release(body->world);
1689} 1698}
1690 1699
@@ -1700,6 +1709,13 @@ ephysics_body_restitution_get(const EPhysics_Body *body)
1700 return body->rigid_body->getRestitution(); 1709 return body->rigid_body->getRestitution();
1701} 1710}
1702 1711
1712static void
1713_ephysics_body_friction_set(EPhysics_Body *body, double friction)
1714{
1715 body->rigid_body->setFriction(btScalar(friction));
1716 DBG("Body %p friction set to %lf", body, friction);
1717}
1718
1703EAPI void 1719EAPI void
1704ephysics_body_friction_set(EPhysics_Body *body, double friction) 1720ephysics_body_friction_set(EPhysics_Body *body, double friction)
1705{ 1721{
@@ -1710,8 +1726,8 @@ ephysics_body_friction_set(EPhysics_Body *body, double friction)
1710 } 1726 }
1711 1727
1712 ephysics_world_lock_take(body->world); 1728 ephysics_world_lock_take(body->world);
1713 body->rigid_body->setFriction(btScalar(friction)); 1729 body->material = EPHYSICS_BODY_MATERIAL_CUSTOM;
1714 DBG("Body %p friction set to %lf", body, friction); 1730 _ephysics_body_friction_set(body, friction);
1715 ephysics_world_lock_release(body->world); 1731 ephysics_world_lock_release(body->world);
1716} 1732}
1717 1733
@@ -2032,6 +2048,7 @@ ephysics_body_density_set(EPhysics_Body *body, double density)
2032 return; 2048 return;
2033 } 2049 }
2034 2050
2051 body->material = EPHYSICS_BODY_MATERIAL_CUSTOM;
2035 body->density = density; 2052 body->density = density;
2036 ephysics_world_lock_take(body->world); 2053 ephysics_world_lock_take(body->world);
2037 _ephysics_body_mass_set(body, 0); 2054 _ephysics_body_mass_set(body, 0);
@@ -2050,6 +2067,50 @@ ephysics_body_density_get(const EPhysics_Body *body)
2050 return body->density; 2067 return body->density;
2051} 2068}
2052 2069
2070EAPI void
2071ephysics_body_material_set(EPhysics_Body *body, EPhysics_Body_Material material)
2072{
2073 if (!body)
2074 {
2075 ERR("Can't set body's material.");
2076 return;
2077 }
2078
2079 if (material >= EPHYSICS_BODY_MATERIAL_LAST)
2080 {
2081 ERR("Invalid material.");
2082 return;
2083 }
2084
2085 if (material != ephysics_material_props[material].material)
2086 {
2087 ERR("Material properties weren't found.");
2088 return;
2089 }
2090
2091 ephysics_world_lock_take(body->world);
2092 body->density = ephysics_material_props[material].density;
2093 _ephysics_body_mass_set(body, 0);
2094 _ephysics_body_friction_set(body,
2095 ephysics_material_props[material].friction);
2096 _ephysics_body_restitution_set(
2097 body, ephysics_material_props[material].restitution);
2098 body->material = material;
2099 ephysics_world_lock_release(body->world);
2100}
2101
2102EAPI EPhysics_Body_Material
2103ephysics_body_material_get(const EPhysics_Body *body)
2104{
2105 if (!body)
2106 {
2107 ERR("Can't get body's material.");
2108 return EPHYSICS_BODY_MATERIAL_LAST;
2109 }
2110
2111 return body->material;
2112}
2113
2053#ifdef __cplusplus 2114#ifdef __cplusplus
2054} 2115}
2055#endif 2116#endif
diff --git a/legacy/ephysics/src/lib/ephysics_body_materials.h b/legacy/ephysics/src/lib/ephysics_body_materials.h
new file mode 100644
index 0000000000..993a0b116b
--- /dev/null
+++ b/legacy/ephysics/src/lib/ephysics_body_materials.h
@@ -0,0 +1,37 @@
1#ifndef _EPHYSICS_BODY_MATERIALS_H
2#define _EPHYSICS_BODY_MATERIALS_H
3
4#ifdef __cplusplus
5extern "C" {
6#endif
7
8typedef struct _EPhysics_Body_Material_Props EPhysics_Body_Material_Props;
9struct _EPhysics_Body_Material_Props {
10 EPhysics_Body_Material material;
11 int density;
12 double friction;
13 double restitution;
14};
15
16const EPhysics_Body_Material_Props ephysics_material_props [] = {
17 {EPHYSICS_BODY_MATERIAL_CUSTOM, 1.0, 0.0, 0.0},
18 {EPHYSICS_BODY_MATERIAL_CONCRETE, EPHYSICS_BODY_DENSITY_CONCRETE,
19 EPHYSICS_BODY_FRICTION_CONCRETE, EPHYSICS_BODY_RESTITUTION_CONCRETE},
20 {EPHYSICS_BODY_MATERIAL_IRON, EPHYSICS_BODY_DENSITY_IRON,
21 EPHYSICS_BODY_FRICTION_IRON, EPHYSICS_BODY_RESTITUTION_IRON},
22 {EPHYSICS_BODY_MATERIAL_PLASTIC, EPHYSICS_BODY_DENSITY_PLASTIC,
23 EPHYSICS_BODY_FRICTION_PLASTIC, EPHYSICS_BODY_RESTITUTION_PLASTIC},
24 {EPHYSICS_BODY_MATERIAL_POLYSTYRENE, EPHYSICS_BODY_DENSITY_POLYSTYRENE,
25 EPHYSICS_BODY_FRICTION_POLYSTYRENE,
26 EPHYSICS_BODY_RESTITUTION_POLYSTYRENE},
27 {EPHYSICS_BODY_MATERIAL_RUBBER, EPHYSICS_BODY_DENSITY_RUBBER,
28 EPHYSICS_BODY_FRICTION_RUBBER, EPHYSICS_BODY_RESTITUTION_RUBBER},
29 {EPHYSICS_BODY_MATERIAL_WOOD, EPHYSICS_BODY_DENSITY_WOOD,
30 EPHYSICS_BODY_FRICTION_WOOD, EPHYSICS_BODY_RESTITUTION_WOOD}
31};
32
33#ifdef __cplusplus
34}
35#endif
36
37#endif
diff --git a/legacy/ephysics/src/lib/ephysics_private.h b/legacy/ephysics/src/lib/ephysics_private.h
index 78eff5bec4..358c720a93 100644
--- a/legacy/ephysics/src/lib/ephysics_private.h
+++ b/legacy/ephysics/src/lib/ephysics_private.h
@@ -79,6 +79,7 @@ struct _EPhysics_Body {
79 Eina_Inlist *callbacks; 79 Eina_Inlist *callbacks;
80 Eina_List *collision_groups; 80 Eina_List *collision_groups;
81 Eina_List *to_delete; 81 Eina_List *to_delete;
82 EPhysics_Body_Material material;
82 double mass; 83 double mass;
83 double density; 84 double density;
84 struct { 85 struct {