EPhysics - Rotating Forever

The purpose of this example is to demonstrate the EPhysics Rotate usage - The code applies different ways to rotate an EPhysics_Body, such as torque, torque impulse and rotation set.

For this example we'll have an EPhysics_World with gravity setted to zero, and four basic EPhysics_Bodys.

The basic concepts like - defining an EPhysics_World, render geometry, physics limiting boundaries, add an EPhysics_Body, associate it to evas objects, change restitution, friction and impulse properties, were already covered in EPhysics - Bouncing Ball

Rotating

For the first body we'll apply a torque impulse to make it rotate around Z axis (rotate on x-y plane). Will make the body rolls on clockwise rotation, if the value is negative, the impulse will be on counter clockwise.

For the second body we'll use an offset to apply the force, the three last parameters are responsible to set a relative position to apply the force.In other words, the force applied with an offset will make the body rotates and move around the other cubes.

ephysics_body_impulse_apply(body, 30, 0, 0, 0, -10, 0);

For the third body we'll use a timer to rotate the body and a callback to delete it.

timer = ecore_timer_add(1, _rotate_cb, body);
_del_cb, timer);
_del_cb(void *data, EPhysics_Body *body __UNUSED__, void *event_info __UNUSED__)
{
}

In the function we'll get the body rotation on z axis in degrees and handle it increasing 5 degrees on its position on z axis on each tick of the timer.

_rotate_cb(void *data)
{
EPhysics_Quaternion *quat_prev, quat_delta, quat;
EPhysics_Body *body = data;
quat_prev = ephysics_body_rotation_get(body, NULL);
ephysics_quaternion_set(&quat_delta, 0, 0, -0.15, 0.98);
body, ephysics_quaternion_multiply(&quat_delta, quat_prev, &quat));
free(quat_prev);
return EINA_TRUE;
}

For the forth body we'll use 2 timers, but before that, we'll apply an initial torque, changing the body angular acceleration and a callback to delete the timers we'll add.

Just the callback function to delete the timers.

_del_torque_cb(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
{
Ecore_Timer *timer;
timer = evas_object_data_get(obj, "increase_timer");
if (timer)
timer = evas_object_data_get(obj, "stop_timer");
if (timer)
}

As we commented we'll use 2 timers, one to increase the torque and another to stop the torque, cleaning the forces related to the body.

timer = ecore_timer_add(3, _increase_torque_cb, body);
evas_object_data_set(cube, "increase_timer", timer);
timer = ecore_timer_add(5, _stop_torque_cb, body);
evas_object_data_set(cube, "stop_timer", timer);

In the increase function we'll apply a torque over the body, changing its angular acceleration, it will leads to a change on angular velocity over time. We're using a timer to increase the angular acceleration on each tick of the timer.

_increase_torque_cb(void *data)
{
EPhysics_Body *body = data;
evas_object_data_set(obj, "increase_timer", NULL);
return EINA_FALSE;
}

In the stop function we'll clear all the forces applied to the body, setting its linear and angular acceleration to zero. We're using this timer to "control" the body velocity, since we are increasing it by another timer. Note that we set the acceleration to zero not the velocity.

_stop_torque_cb(void *data)
{
EPhysics_Body *body = data;
evas_object_data_set(obj, "stop_timer", NULL);
return EINA_FALSE;
}

Here we finish the example. The full source code can be found at test_rotating_forever.c.

ephysics_quaternion_normalize
EAPI void ephysics_quaternion_normalize(EPhysics_Quaternion *quat)
Normalize the quaternion.
evas_object_data_get
void * evas_object_data_get(const Evas_Object *obj, const char *key)
Return an attached data pointer on an Evas object by its given string key.
Definition: evas_data.c:12
EPhysics_Body
struct _EPhysics_Body EPhysics_Body
Definition: EPhysics.h:655
ephysics_body_event_callback_add
EAPI void ephysics_body_event_callback_add(EPhysics_Body *body, EPhysics_Callback_Body_Type type, EPhysics_Body_Event_Cb func, const void *data)
Register a callback to a type of physics body event.
EINA_FALSE
#define EINA_FALSE
Definition: eina_types.h:502
ecore_timer_add
Ecore_Timer * ecore_timer_add(double in, Ecore_Task_Cb func, const void *data)
Creates a timer to call the given function in the given period of time.
Definition: ecore_timer.c:171
ephysics_body_torque_impulse_apply
EAPI void ephysics_body_torque_impulse_apply(EPhysics_Body *body, double pitch, double yaw, double roll)
Apply a torque impulse over a body.
EPHYSICS_CALLBACK_BODY_DEL
Body being deleted (called before free)
Definition: EPhysics.h:2265
Evas_Object
Efl_Canvas_Object Evas_Object
Definition: Evas_Common.h:180
ephysics_body_forces_clear
EAPI void ephysics_body_forces_clear(EPhysics_Body *body)
Clear all the forces applied to a body.
Evas
Eo Evas
Definition: Evas_Common.h:158
EINA_TRUE
#define EINA_TRUE
Definition: eina_types.h:508
evas_object_data_set
void evas_object_data_set(Evas_Object *eo_obj, const char *key, const void *data)
Set an attached data pointer to an object with a given string key.
Definition: evas_data.c:5
ephysics_quaternion_set
EAPI void ephysics_quaternion_set(EPhysics_Quaternion *quat, double x, double y, double z, double w)
Set quaternion values.
ephysics_body_impulse_apply
EAPI void ephysics_body_impulse_apply(EPhysics_Body *body, double x, double y, double z, Evas_Coord pos_x, Evas_Coord pos_y, Evas_Coord pos_z)
Apply an impulse over a body.
_EPhysics_Quaternion
Definition: EPhysics.h:189
ephysics_body_evas_object_get
EAPI Evas_Object * ephysics_body_evas_object_get(const EPhysics_Body *body)
Get the evas object associated to a physics body.
Ecore_Timer
Eo Ecore_Timer
A handle for timers.
Definition: Ecore_Common.h:3069
ephysics_body_torque_apply
EAPI void ephysics_body_torque_apply(EPhysics_Body *body, double torque_x, double torque_y, double torque_z)
Apply a torque over a body.
ecore_timer_del
void * ecore_timer_del(Ecore_Timer *timer)
Deletes the specified timer from the timer list.
Definition: ecore_timer.c:218
ephysics_quaternion_multiply
EAPI EPhysics_Quaternion * ephysics_quaternion_multiply(const EPhysics_Quaternion *quat1, const EPhysics_Quaternion *quat2, EPhysics_Quaternion *result)
Multiply two quaternions.
ephysics_body_rotation_set
EAPI void ephysics_body_rotation_set(EPhysics_Body *body, EPhysics_Quaternion *quat)
Set body's rotation.
ephysics_body_rotation_get
EAPI EPhysics_Quaternion * ephysics_body_rotation_get(const EPhysics_Body *body, EPhysics_Quaternion *rotation)
Get body's rotation quaternion.