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.

EAPI void ephysics_body_torque_impulse_apply(EPhysics_Body *body, double pitch, double yaw, double roll)
Apply a torque impulse over a body.

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);
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.

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);
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.
@ EPHYSICS_CALLBACK_BODY_DEL
Body being deleted (called before free)
Definition: EPhysics.h:2265
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:189
_del_cb(void *data, EPhysics_Body *body __UNUSED__, void *event_info __UNUSED__)
{
}
struct _EPhysics_Body EPhysics_Body
Body handle, represents an object on EPhysics world.
Definition: EPhysics.h:655
void * ecore_timer_del(Ecore_Timer *timer)
Deletes the specified timer from the timer list.
Definition: ecore_timer.c:238

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;
}
EAPI void ephysics_body_rotation_set(EPhysics_Body *body, EPhysics_Quaternion *quat)
Set body's rotation.
EAPI EPhysics_Quaternion * ephysics_body_rotation_get(const EPhysics_Body *body, EPhysics_Quaternion *rotation)
Get body's rotation quaternion.
EAPI EPhysics_Quaternion * ephysics_quaternion_multiply(const EPhysics_Quaternion *quat1, const EPhysics_Quaternion *quat2, EPhysics_Quaternion *result)
Multiply two quaternions.
EAPI void ephysics_quaternion_set(EPhysics_Quaternion *quat, double x, double y, double z, double w)
Set quaternion values.
EAPI void ephysics_quaternion_normalize(EPhysics_Quaternion *quat)
Normalize the quaternion.
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
Quaternion coordinates and rotation (w, x, y, z)
Definition: EPhysics.h:190

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.

EAPI void ephysics_body_torque_apply(EPhysics_Body *body, double torque_x, double torque_y, double torque_z)
Apply a torque over a body.

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)
}
Eo Ecore_Timer
A handle for timers.
Definition: Ecore_Common.h:3079
Eo Evas
An opaque handle to an Evas canvas.
Definition: Evas_Common.h:163
EVAS_API 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
Efl_Canvas_Object Evas_Object
An Evas Object handle.
Definition: Evas_Common.h:185

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);
EVAS_API 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

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;
}
EAPI Evas_Object * ephysics_body_evas_object_get(const EPhysics_Body *body)
Get the evas object associated to a physics body.
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition: eina_types.h:533

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;
}
EAPI void ephysics_body_forces_clear(EPhysics_Body *body)
Clear all the forces applied to a body.

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