summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeandro Dorileo <dorileo@profusion.mobi>2012-11-23 21:52:08 +0000
committerBruno Dilly <bdilly@profusion.mobi>2012-11-23 21:52:08 +0000
commitb09ef08e17efaff51a6a2027f79d7dd3811bd944 (patch)
tree3ec1b8fc4cbe01d277b5bf914dcea5eb0da49198
parentc7f47bea087bfa987c0b6dbac5f296a4785fac8d (diff)
EPhysics: soft body operations on triangles list
This patch introduces API to get a list of triangles inside an area and another to apply impulse - also - in a list of triangles. Patch by: Leandro Dorileo <dorileo@profusion.mobi> SVN revision: 79607
-rw-r--r--legacy/src/lib/EPhysics.h50
-rw-r--r--legacy/src/lib/ephysics_body.cpp118
2 files changed, 158 insertions, 10 deletions
diff --git a/legacy/src/lib/EPhysics.h b/legacy/src/lib/EPhysics.h
index 9172ab0..9b23846 100644
--- a/legacy/src/lib/EPhysics.h
+++ b/legacy/src/lib/EPhysics.h
@@ -2428,6 +2428,35 @@ EAPI EPhysics_Body *ephysics_body_soft_ellipsoid_add(EPhysics_World *world, int
2428 2428
2429/** 2429/**
2430 * @brief 2430 * @brief
2431 * Get a list of triangles indexes inside an area.
2432 *
2433 * Get a list of triangles indexes given an area defined by @p x, @p y, @p z, @p
2434 * w, @p h and @p d, the z axis components are represented by @p x and @p d
2435 * where all triangles between @p z and @p d are considered, that's triangles
2436 * with their z component greater than @p z and smaller than @p d.
2437 *
2438 * @note EPhysics will not free the returned list, remember to do so.
2439 *
2440 * @param body The body to get triangles indexes from.
2441 * @param x The x component.
2442 * @param y The y component.
2443 * @param z The z component.
2444 * @param w The w component.
2445 * @param h The h component.
2446 * @param d The d component.
2447 *
2448 * @return NULL on errors or no triangles found, a list of triangles indexes
2449 * otherwhise.
2450 *
2451 * @see ephysics_body_soft_body_triangle_index_get().
2452 * @see ephysics_body_soft_body_slice_index_get().
2453 *
2454 * @ingroup EPhysics_Body
2455 */
2456EAPI Eina_List *ephysics_body_soft_body_triangles_inside_get(const EPhysics_Body *body, Evas_Coord x, Evas_Coord y, Evas_Coord z, Evas_Coord w, Evas_Coord h, Evas_Coord d);
2457
2458/**
2459 * @brief
2431 * Apply an impulse on a given soft body triangle. 2460 * Apply an impulse on a given soft body triangle.
2432 * 2461 *
2433 * The impulse is equal to the change of momentum of the body. 2462 * The impulse is equal to the change of momentum of the body.
@@ -2455,6 +2484,27 @@ EAPI void ephysics_body_soft_body_triangle_impulse_apply(EPhysics_Body * body, i
2455 2484
2456/** 2485/**
2457 * @brief 2486 * @brief
2487 * Apply impulse in a list of triangles.
2488 *
2489 * Apply impulse in a list of triangles all at once considering the same impulse
2490 * values on @p x, @p y and @p z.
2491 *
2492 * @param body The body to apply impulse.
2493 * @param triangles A list of triangles indexes.
2494 * @param x The axis @p x component of impulse.
2495 * @param y The axis @p y component of impulse.
2496 * @param z The axis @p z component of impulse.
2497 *
2498 * @see ephysics_body_soft_body_triangle_impulse_apply() to see about impulse
2499 * applying on soft bodies.
2500 * @see ephysics_body_soft_body_triangles_inside_get().
2501 *
2502 * @ingroup EPhysics_Body
2503 */
2504EAPI void ephysics_body_soft_body_triangle_list_impulse_apply(EPhysics_Body *body, Eina_List *triangles, double x, double y, double z);
2505
2506/**
2507 * @brief
2458 * Set the soft body number of position iterations. 2508 * Set the soft body number of position iterations.
2459 * 2509 *
2460 * Both soft body and cloth can have its number of position iterations changed. 2510 * Both soft body and cloth can have its number of position iterations changed.
diff --git a/legacy/src/lib/ephysics_body.cpp b/legacy/src/lib/ephysics_body.cpp
index 1869a3b..a636dc4 100644
--- a/legacy/src/lib/ephysics_body.cpp
+++ b/legacy/src/lib/ephysics_body.cpp
@@ -2290,14 +2290,90 @@ ephysics_body_soft_body_triangle_move(EPhysics_Body *body, int idx, Evas_Coord x
2290 ephysics_world_lock_release(body->world); 2290 ephysics_world_lock_release(body->world);
2291} 2291}
2292 2292
2293EAPI void 2293EAPI Eina_List *
2294ephysics_body_soft_body_triangle_impulse_apply(EPhysics_Body * body, int idx, double x, double y, double z) 2294ephysics_body_soft_body_triangles_inside_get(const EPhysics_Body *body, Evas_Coord x, Evas_Coord y, Evas_Coord z, Evas_Coord w, Evas_Coord h, Evas_Coord d)
2295{
2296 Eina_List *face_list = NULL;
2297 btSoftBody::Face *bt_face;
2298 btSoftBody::Node *node;
2299 int out, *idx;
2300 btScalar nx, ny, nz, xx, yy, zz, dd, ww, hh;
2301 Evas_Coord wy, wh;
2302 double rate;
2303
2304 if (body->type == EPHYSICS_BODY_TYPE_RIGID)
2305 {
2306 ERR("Can't get triangle indexes, operation not permited to rigid"
2307 " bodies.");
2308 return NULL;
2309 }
2310
2311 ephysics_world_lock_take(body->world);
2312 rate = ephysics_world_rate_get(body->world);
2313 ephysics_world_render_geometry_get(body->world, NULL, &wy, NULL, NULL, &wh,
2314 NULL);
2315 xx = x / rate;
2316 yy = ((wh + wy) - y) / rate;
2317 zz = z / rate;
2318 dd = d / rate;
2319 ww = w / rate;
2320 hh = h / rate;
2321
2322 for (int m = 0; m < body->soft_body->m_faces.size(); m++)
2323 {
2324 out = 0;
2325 bt_face = &body->soft_body->m_faces[m];
2326 for (int n = 0; n < 3; n++)
2327 {
2328 node = bt_face->m_n[n];
2329 nx = node->m_x.x();
2330 ny = node->m_x.y();
2331 nz = node->m_x.z();
2332
2333 if ((nz > zz || nz < dd) || (nx < xx || nx > xx + ww) ||
2334 (ny > yy || ny < ny - hh))
2335 out++;
2336 }
2337
2338 if (!out)
2339 {
2340 idx = (int *)malloc(sizeof(int));
2341 *idx = m;
2342 face_list = eina_list_append(face_list, idx);
2343 }
2344 }
2345
2346 ephysics_world_lock_release(body->world);
2347
2348 return face_list;
2349}
2350
2351static void
2352_ephysics_body_soft_body_triangle_impulse_apply(EPhysics_Body *body, int idx, double x, double y, double z)
2295{ 2353{
2296 btSoftBody::Face face; 2354 btSoftBody::Face face;
2297 btSoftBody::Node *node; 2355 btSoftBody::Node *node;
2298 double rate; 2356 double rate;
2299 btVector3 impulse; 2357 btVector3 impulse;
2300 2358
2359 rate = ephysics_world_rate_get(body->world);
2360 impulse = btVector3(x / rate, y / rate, z / rate);
2361
2362 face = body->soft_body->m_faces[idx];
2363 for (int i = 0; i < 3; i++)
2364 {
2365 node = face.m_n[i];
2366 node->m_v += impulse * node->m_im;
2367 }
2368
2369 DBG("Impulse applied to soft body node(%d): %lf, %lf, %lf", idx, impulse.x(),
2370 impulse.y(), impulse.z());
2371}
2372
2373EAPI void
2374ephysics_body_soft_body_triangle_impulse_apply(EPhysics_Body * body, int idx, double x, double y, double z)
2375{
2376
2301 if (body->type == EPHYSICS_BODY_TYPE_RIGID) 2377 if (body->type == EPHYSICS_BODY_TYPE_RIGID)
2302 { 2378 {
2303 ERR("Can't apply impulse, operation not permited to rigid bodies."); 2379 ERR("Can't apply impulse, operation not permited to rigid bodies.");
@@ -2311,18 +2387,40 @@ ephysics_body_soft_body_triangle_impulse_apply(EPhysics_Body * body, int idx, do
2311 return; 2387 return;
2312 } 2388 }
2313 2389
2314 rate = ephysics_world_rate_get(body->world);
2315 impulse = btVector3(x / rate, y / rate, z / rate);
2316
2317 ephysics_world_lock_take(body->world); 2390 ephysics_world_lock_take(body->world);
2391 _ephysics_body_soft_body_triangle_impulse_apply(body, idx, x, y, z);
2392 ephysics_world_lock_release(body->world);
2393}
2318 2394
2319 face = body->soft_body->m_faces[idx]; 2395EAPI void
2320 node = face.m_n[0]; 2396ephysics_body_soft_body_triangle_list_impulse_apply(EPhysics_Body *body, Eina_List *triangles, double x, double y, double z)
2321 node->m_f += impulse * node->m_im; 2397{
2398 Eina_List *l;
2399 void *ldata;
2400 int idx, faces_cnt;
2322 2401
2402 if (body->type == EPHYSICS_BODY_TYPE_RIGID)
2403 {
2404 ERR("Can't apply impulse, operation not permited to rigid bodies.");
2405 return;
2406 }
2407
2408 faces_cnt = body->soft_body->m_faces.size();
2409 ephysics_world_lock_take(body->world);
2410 EINA_LIST_FOREACH(triangles, l, ldata)
2411 {
2412 idx = *(int *)ldata;
2413 if (idx < 0 || idx >= faces_cnt)
2414 {
2415 INF("Could not apply impulse to triangle %d, provided body"
2416 " triangle index ranges from 0 to %d", idx, faces_cnt);
2417 continue;
2418 }
2419
2420 _ephysics_body_soft_body_triangle_impulse_apply(body, idx, x, y, z);
2421 DBG("Applied impulse on body %p, triangle: %d", body, idx);
2422 }
2323 ephysics_world_lock_release(body->world); 2423 ephysics_world_lock_release(body->world);
2324 DBG("Impulse applied to soft body node(%d): %lf, %lf, %lf", idx, impulse.x(),
2325 impulse.y(), impulse.z());
2326} 2424}
2327 2425
2328EAPI int 2426EAPI int