summaryrefslogtreecommitdiff
path: root/legacy
diff options
context:
space:
mode:
authorBruno Dilly <bdilly@profusion.mobi>2012-10-04 22:39:29 +0000
committerBruno Dilly <bdilly@profusion.mobi>2012-10-04 22:39:29 +0000
commit748cfbe2f447a3005c3951b6c0a5635a87e55d3b (patch)
tree69dcf2184994f6f0e890e08109d97c9d530d76e6 /legacy
parentb52d2ed85e8fb878588279faf0723fb1fd7b99a2 (diff)
ephysics: support density
SVN revision: 77460
Diffstat (limited to 'legacy')
-rw-r--r--legacy/ephysics/src/lib/EPhysics.h41
-rw-r--r--legacy/ephysics/src/lib/ephysics_body.cpp38
-rw-r--r--legacy/ephysics/src/lib/ephysics_private.h1
3 files changed, 80 insertions, 0 deletions
diff --git a/legacy/ephysics/src/lib/EPhysics.h b/legacy/ephysics/src/lib/EPhysics.h
index 52d6ec8..0f6234b 100644
--- a/legacy/ephysics/src/lib/EPhysics.h
+++ b/legacy/ephysics/src/lib/EPhysics.h
@@ -2699,6 +2699,47 @@ EAPI void ephysics_body_forces_clear(EPhysics_Body *body);
2699EAPI void ephysics_body_center_mass_get(const EPhysics_Body *body, double *x, double *y); 2699EAPI void ephysics_body_center_mass_get(const EPhysics_Body *body, double *x, double *y);
2700 2700
2701/** 2701/**
2702 * @brief
2703 * Set body's material density.
2704 *
2705 * The density of a material is its mass per unit volume. It will set the
2706 * body mass considering its volume. While a density is set, resizing
2707 * a body will always recalculate its mass.
2708 *
2709 * When a mass is explicitely set with @ref ephysics_body_mass_set(),
2710 * the density will be unset.
2711 *
2712 * It's useful in cases where a specific material needs to be simulated,
2713 * so its density can be set and ephysics will calcute the body's mass.
2714 *
2715 * By default, no density is set.
2716 *
2717 * @note The unit used for density is kilograms / meters ^ 3.
2718 *
2719 * @param body The body to has its material density set.
2720 * @param density The @p body's material density, in kilograms / meters ^ 3.
2721 *
2722 * @see ephysics_body_density_get().
2723 *
2724 * @ingroup EPhysics_Body
2725 */
2726EAPI void ephysics_body_density_set(EPhysics_Body *body, double density);
2727
2728/**
2729 * @brief
2730 * Get body's material density.
2731 *
2732 * @param body The physics body.
2733 * @return the @p body material's density, in kilograms / meters ^ 3 or 0
2734 * if no density is set.
2735 *
2736 * @see ephysics_body_density_set() for details.
2737 *
2738 * @ingroup EPhysics_Body
2739 */
2740EAPI double ephysics_body_density_get(const EPhysics_Body *body);
2741
2742/**
2702 * @} 2743 * @}
2703 */ 2744 */
2704 2745
diff --git a/legacy/ephysics/src/lib/ephysics_body.cpp b/legacy/ephysics/src/lib/ephysics_body.cpp
index 2d1a163..c03a8db 100644
--- a/legacy/ephysics/src/lib/ephysics_body.cpp
+++ b/legacy/ephysics/src/lib/ephysics_body.cpp
@@ -347,11 +347,21 @@ _ephysics_body_soft_body_constraints_rebuild(EPhysics_Body *body)
347 _ephysics_body_soft_body_points_distance_get(body, body->distances); 347 _ephysics_body_soft_body_points_distance_get(body, body->distances);
348} 348}
349 349
350inline static double
351_ephysics_body_volume_get(const EPhysics_Body *body)
352{
353 btVector3 vector = body->collision_shape->getLocalScaling();
354 return vector.x() * vector.y() * vector.z();
355}
356
350static void 357static void
351_ephysics_body_mass_set(EPhysics_Body *body, double mass) 358_ephysics_body_mass_set(EPhysics_Body *body, double mass)
352{ 359{
353 btVector3 inertia(0, 0, 0); 360 btVector3 inertia(0, 0, 0);
354 361
362 if (body->density)
363 mass = body->density * _ephysics_body_volume_get(body);
364
355 if (body->soft_body) 365 if (body->soft_body)
356 body->soft_body->setTotalMass(mass); 366 body->soft_body->setTotalMass(mass);
357 else 367 else
@@ -1408,6 +1418,7 @@ ephysics_body_mass_set(EPhysics_Body *body, double mass)
1408 } 1418 }
1409 1419
1410 ephysics_world_lock_take(body->world); 1420 ephysics_world_lock_take(body->world);
1421 body->density = 0;
1411 _ephysics_body_mass_set(body, mass); 1422 _ephysics_body_mass_set(body, mass);
1412 ephysics_world_lock_release(body->world); 1423 ephysics_world_lock_release(body->world);
1413} 1424}
@@ -2012,6 +2023,33 @@ ephysics_body_center_mass_get(const EPhysics_Body *body, double *x, double *y)
2012 if (y) *y = body->cm.y; 2023 if (y) *y = body->cm.y;
2013} 2024}
2014 2025
2026EAPI void
2027ephysics_body_density_set(EPhysics_Body *body, double density)
2028{
2029 if (!body)
2030 {
2031 ERR("Can't set the density of a null body's material.");
2032 return;
2033 }
2034
2035 body->density = density;
2036 ephysics_world_lock_take(body->world);
2037 _ephysics_body_mass_set(body, 0);
2038 ephysics_world_lock_release(body->world);
2039}
2040
2041EAPI double
2042ephysics_body_density_get(const EPhysics_Body *body)
2043{
2044 if (!body)
2045 {
2046 ERR("Can't get the density of a null body's material.");
2047 return 0;
2048 }
2049
2050 return body->density;
2051}
2052
2015#ifdef __cplusplus 2053#ifdef __cplusplus
2016} 2054}
2017#endif 2055#endif
diff --git a/legacy/ephysics/src/lib/ephysics_private.h b/legacy/ephysics/src/lib/ephysics_private.h
index 08d9af6..78eff5b 100644
--- a/legacy/ephysics/src/lib/ephysics_private.h
+++ b/legacy/ephysics/src/lib/ephysics_private.h
@@ -80,6 +80,7 @@ struct _EPhysics_Body {
80 Eina_List *collision_groups; 80 Eina_List *collision_groups;
81 Eina_List *to_delete; 81 Eina_List *to_delete;
82 double mass; 82 double mass;
83 double density;
83 struct { 84 struct {
84 double x; 85 double x;
85 double y; 86 double y;