diff --git a/legacy/ephysics/src/bin/test_rotating_forever.c b/legacy/ephysics/src/bin/test_rotating_forever.c index cf3e37bcc5..df2ef46cfa 100644 --- a/legacy/ephysics/src/bin/test_rotating_forever.c +++ b/legacy/ephysics/src/bin/test_rotating_forever.c @@ -14,7 +14,7 @@ _update_object_cb(void *data __UNUSED__, EPhysics_Body *body, void *event_info _ vrot = ephysics_body_angular_velocity_get(body); ephysics_body_evas_object_update(body); - DBG("rot: %lf, vrot :%lf", rot, vrot); + DBG("body: %p, rot: %lf, vrot :%lf", body, rot, vrot); } static void @@ -39,6 +39,23 @@ _world_populate(Test_Data *test_data) _update_object_cb, NULL); ephysics_body_torque_impulse_apply(body, 1); + + cube = elm_image_add(test_data->win); + elm_image_file_set( + cube, PACKAGE_DATA_DIR "/" EPHYSICS_TEST_THEME ".edj", "purple-cube"); + evas_object_move(cube, WIDTH / 3, FLOOR_Y - 70); + evas_object_resize(cube, 70, 70); + evas_object_show(cube); + test_data->evas_objs = eina_list_append(test_data->evas_objs, cube); + + body = ephysics_body_box_add(test_data->world); + ephysics_body_evas_object_set(body, cube, EINA_TRUE); + test_data->bodies = eina_list_append(test_data->bodies, body); + ephysics_body_event_callback_add(body, + EPHYSICS_CALLBACK_BODY_UPDATE, + _update_object_cb, NULL); + + ephysics_body_impulse_apply(body, 30, 0, 0, -10); } static void diff --git a/legacy/ephysics/src/lib/EPhysics.h b/legacy/ephysics/src/lib/EPhysics.h index 65b1ae2f46..8200a6d020 100644 --- a/legacy/ephysics/src/lib/EPhysics.h +++ b/legacy/ephysics/src/lib/EPhysics.h @@ -2143,6 +2143,9 @@ EAPI double ephysics_body_friction_get(const EPhysics_Body *body); * @param x The axis x component of impulse. * @param y The axis y component of impulse. * + * @see ephysics_body_torque_impulse_apply(). + * @see ephysics_body_impulse_apply(). + * * @ingroup EPhysics_Body */ EAPI void ephysics_body_central_impulse_apply(EPhysics_Body *body, double x, double y); @@ -2162,11 +2165,46 @@ EAPI void ephysics_body_central_impulse_apply(EPhysics_Body *body, double x, dou * Negative values will impulse body on anti clock rotation. * * @see ephysics_body_central_impulse_apply(). + * @see ephysics_body_impulse_apply(). * * @ingroup EPhysics_Body */ EAPI void ephysics_body_torque_impulse_apply(EPhysics_Body *body, double roll); +/** + * @brief + * Apply an impulse over a body. + * + * An impulse will be applied over the body to make it move and rotate around + * Z axis. + * + * Impulse is the product of the force over the time this force is applied. + * It can be applied in the center of the body, avoiding rotating it, + * with @ref ephysics_body_central_impulse_apply(), it can be applied only + * to make a body rotate, with @ref ephysics_body_torque_impulse_apply(), + * or can be used to lead to both behaviors with + * @ref ephysics_body_impulse_apply(). + * + * It will resulte in a central impulse with impulse (@p x, @p y) and a + * torque impulse that will be calculated as a cross product on impulse + * and relative position. + * + * @param body The physics body that will receive the impulse. + * @param x The axis x component of impulse. + * @param y The axis y component of impulse. + * @param pos_x The axis x component of the relative position to apply impulse. + * @param pos_y The axis y component of the relative position to apply impulse. + * + * @note Impulse is measured in kg * p / s and position in pixels + * (Evas coordinates). + * + * @see ephysics_body_central_impulse_apply(). + * @see ephysics_body_torque_impulse_apply(). + * + * @ingroup EPhysics_Body + */ +EAPI void ephysics_body_impulse_apply(EPhysics_Body *body, double x, double y, Evas_Coord pos_x, Evas_Coord pos_y); + /** * @brief * Enable or disable body's rotation on z axis. diff --git a/legacy/ephysics/src/lib/ephysics_body.cpp b/legacy/ephysics/src/lib/ephysics_body.cpp index 0059e8ff37..26a34911ae 100644 --- a/legacy/ephysics/src/lib/ephysics_body.cpp +++ b/legacy/ephysics/src/lib/ephysics_body.cpp @@ -1237,6 +1237,25 @@ ephysics_body_central_impulse_apply(EPhysics_Body *body, double x, double y) body->rigid_body->applyCentralImpulse(btVector3(x / rate, y / rate, 0)); } +EAPI void +ephysics_body_impulse_apply(EPhysics_Body *body, double x, double y, Evas_Coord pos_x, Evas_Coord pos_y) +{ + double rate; + + if (!body) + { + ERR("Can't apply impulse to a null body."); + return; + } + + rate = ephysics_world_rate_get(body->world); + + body->rigid_body->activate(1); + body->rigid_body->applyImpulse(btVector3(x / rate, y / rate, 0), + btVector3((double) pos_x / rate, + (double) pos_y / rate, 0)); +} + EAPI void ephysics_body_linear_movement_enable_set(EPhysics_Body *body, Eina_Bool enable_x, Eina_Bool enable_y) {