From 4bccde902aa21076915c49cdf372e1c4b8c2ccfe Mon Sep 17 00:00:00 2001 From: Bruno Dilly Date: Mon, 20 Aug 2012 22:14:43 +0000 Subject: [PATCH] EPhysics: rotation set implemented SVN revision: 75475 --- .../ephysics/src/bin/test_rotating_forever.c | 39 +++++++++++++++++++ legacy/ephysics/src/lib/EPhysics.h | 20 ++++++++++ legacy/ephysics/src/lib/ephysics_body.cpp | 22 +++++++++++ 3 files changed, 81 insertions(+) diff --git a/legacy/ephysics/src/bin/test_rotating_forever.c b/legacy/ephysics/src/bin/test_rotating_forever.c index df2ef46cfa..105a6dc373 100644 --- a/legacy/ephysics/src/bin/test_rotating_forever.c +++ b/legacy/ephysics/src/bin/test_rotating_forever.c @@ -4,6 +4,23 @@ #include "ephysics_test.h" +static Eina_Bool +_rotate_cb(void *data) +{ + EPhysics_Body *body = data; + double rotation; + + rotation = ephysics_body_rotation_get(body); + ephysics_body_rotation_set(body, ((int) round(rotation) + 5) % 360); + + return EINA_TRUE; +} + +static void +_del_cb(void *data, EPhysics_Body *body __UNUSED__, void *event_info __UNUSED__) +{ + ecore_timer_del(data); +} static void _update_object_cb(void *data __UNUSED__, EPhysics_Body *body, void *event_info __UNUSED__) @@ -21,6 +38,7 @@ static void _world_populate(Test_Data *test_data) { EPhysics_Body *body; + Ecore_Timer *timer; Evas_Object *cube; cube = elm_image_add(test_data->win); @@ -56,6 +74,27 @@ _world_populate(Test_Data *test_data) _update_object_cb, NULL); ephysics_body_impulse_apply(body, 30, 0, 0, -10); + + 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, 60); + 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); + + timer = ecore_timer_add(1, _rotate_cb, body); + + ephysics_body_event_callback_add(body, + EPHYSICS_CALLBACK_BODY_DEL, + _del_cb, timer); } static void diff --git a/legacy/ephysics/src/lib/EPhysics.h b/legacy/ephysics/src/lib/EPhysics.h index 8200a6d020..dec2024635 100644 --- a/legacy/ephysics/src/lib/EPhysics.h +++ b/legacy/ephysics/src/lib/EPhysics.h @@ -2284,10 +2284,30 @@ EAPI void ephysics_body_linear_movement_enable_get(const EPhysics_Body *body, Ei * @param body The physics body. * @return The amount of degrees @p body is rotated, from 0.0 to 360.0. * + * @see ephysics_body_rotation_set() + * * @ingroup EPhysics_Body */ EAPI double ephysics_body_rotation_get(const EPhysics_Body *body); +/** + * @brief + * Set body's rotation on z axis. + * + * By default rotation is 0 degrees. + * Negative values indicates rotation on counterclockwise direction. + * + * @note The unit used for rotation is degrees. + * + * @param body The physics body. + * @param The amount of degrees @p body should be rotated. + * + * @see ephysics_body_rotation_get() + * + * @ingroup EPhysics_Body + */ +EAPI void ephysics_body_rotation_set(EPhysics_Body *body, double rotation); + /** * @brief * Set data to @p body. diff --git a/legacy/ephysics/src/lib/ephysics_body.cpp b/legacy/ephysics/src/lib/ephysics_body.cpp index 26a34911ae..a907008300 100644 --- a/legacy/ephysics/src/lib/ephysics_body.cpp +++ b/legacy/ephysics/src/lib/ephysics_body.cpp @@ -1340,6 +1340,28 @@ ephysics_body_rotation_get(const EPhysics_Body *body) return rot; } +EAPI void +ephysics_body_rotation_set(EPhysics_Body *body, double rotation) +{ + btTransform trans; + btQuaternion quat; + + if (!body) + { + ERR("Can't set rotation, body is null."); + return; + } + + body->rigid_body->activate(1); + body->rigid_body->getMotionState()->getWorldTransform(trans); + quat.setEuler(0, 0, -rotation / RAD_TO_DEG); + trans.setRotation(quat); + body->rigid_body->proceedToTransform(trans); + body->rigid_body->getMotionState()->setWorldTransform(trans); + + DBG("Body %p rotation set to %lf", body, rotation); +} + EAPI void ephysics_body_data_set(EPhysics_Body *body, void *data) {