EPhysics: add soft body triangle impulse API
authorLeandro Dorileo <dorileo@profusion.mobi>
Fri, 23 Nov 2012 21:43:49 +0000 (21:43 +0000)
committerBruno Dilly <bdilly@profusion.mobi>
Fri, 23 Nov 2012 21:43:49 +0000 (21:43 +0000)
Patch by: Leandro Dorileo <dorileo@profusion.mobi>

SVN revision: 79594

legacy/ephysics/src/lib/EPhysics.h
legacy/ephysics/src/lib/ephysics_body.cpp

index a9ac03d..49e3f80 100644 (file)
@@ -2370,6 +2370,33 @@ EAPI int ephysics_body_soft_body_triangle_index_get(EPhysics_Body *body, Evas_Co
 
 /**
  * @brief
+ * Apply an impulse on a given soft body triangle.
+ *
+ * The impulse is equal to the change of momentum of the body.
+ * Impulse is the product of the force over the time this force is applied. In
+ * ephysics case, it would be the time of a tick, so it behaves just summing
+ * current linear velocity to impulse per mass(per triangle mass).
+ *
+ * When a impulse is applied over a body, it will have its velocity changed. This
+ * impulse will be applied on body's specified triangle @p idx, so it won't
+ * imply in rotating the body.
+ *
+ * @note Impulse is measured in kg * p / s.
+ *
+ * @param body The body to apply impulse to.
+ * @param idx The soft body triangle index.
+ * @param x The axis @p x component of impulse.
+ * @param y The axis @p y component of impulse
+ * @param z The axis @p z component of impulse
+ *
+ * @see ephysics_body_soft_body_triangle_index_get().
+ *
+ * @ingroup EPhysics_Body
+ */
+EAPI void ephysics_body_soft_body_triangle_impulse_apply(EPhysics_Body * body, int idx, double x, double y, double z);
+
+/**
+ * @brief
  * Set the soft body number of position iterations.
  *
  * Both soft body and cloth can have its number of position iterations changed.
index 5cc5f6f..4dd6cd8 100644 (file)
@@ -2078,6 +2078,41 @@ ephysics_body_soft_body_triangle_move(EPhysics_Body *body, int idx, Evas_Coord x
    ephysics_world_lock_release(body->world);
 }
 
+EAPI void
+ephysics_body_soft_body_triangle_impulse_apply(EPhysics_Body * body, int idx, double x, double y, double z)
+{
+   btSoftBody::Face face;
+   btSoftBody::Node *node;
+   double rate;
+   btVector3 impulse;
+
+   if (body->type == EPHYSICS_BODY_TYPE_RIGID)
+     {
+        ERR("Can't apply impulse, operation not permited to rigid bodies.");
+        return;
+     }
+
+   if (idx < 0 || idx >= body->soft_body->m_faces.size())
+     {
+        ERR("Could not apply impulse, provided body triangle index ranges from"
+            " 0 to %d", body->soft_body->m_faces.size());
+        return;
+     }
+
+   rate = ephysics_world_rate_get(body->world);
+   impulse = btVector3(x / rate, y / rate, z / rate);
+
+   ephysics_world_lock_take(body->world);
+
+   face = body->soft_body->m_faces[idx];
+   node = face.m_n[0];
+   node->m_f += impulse * node->m_im;
+
+   ephysics_world_lock_release(body->world);
+   DBG("Impulse applied to soft body node(%d): %lf, %lf, %lf", idx, impulse.x(),
+       impulse.y(), impulse.z());
+}
+
 EAPI int
 ephysics_body_soft_body_triangle_index_get(EPhysics_Body *body, Evas_Coord x, Evas_Coord y)
 {