- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Examples

EPhysics - Delete Body

The purpose of this example is to demonstrate the EPhysics Callbacks usage - The code adds two balls, one with impulse and the second with a collision detection callback, to delete the body.

For this example we'll have an EPhysics_World and two basic EPhysics_Bodys, we'll apply an impulse in one of then and the other will be stopped "waiting" for a collision.

The basic concepts like - initializing 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

# Adding the Callbacks

Calling ephysics_body_event_callback_add() registers a callback to a given EPhysics_Body event type.

We'll use two types:

EPHYSICS_CALLBACK_BODY_DEL : called when a body deletion has been issued and just before the deletion actually happens. In other words, to know that body has been marked for deletion. Typically to free some data associated with the body.

ephysics_body_event_callback_add(sphere_body1,
EPHYSICS_CALLBACK_BODY_DEL,
_del_cb, collision_data);

The callback function will receive the collision_data and free some data associated with the body.

_del_cb(void *data, EPhysics_Body *body, void *event_info __UNUSED__)
{
Collision_Data *collision_data = data;
Evas_Object *obj, *shadow;
obj = ephysics_body_evas_object_get(body);
shadow = evas_object_data_get(obj, "shadow");
collision_data->base.evas_objs = eina_list_remove(
collision_data->base.evas_objs, shadow);
collision_data->base.evas_objs = eina_list_remove(
collision_data->base.evas_objs, obj);
evas_object_del(shadow);
evas_object_del(obj);
collision_data->sphere = NULL;
}

EPHYSICS_CALLBACK_BODY_COLLISION : called just after the collision has been actually processed by the physics engine. In other words, to be notified about a collision between two physical bodies.

ephysics_body_event_callback_add(collision_data->sphere,
EPHYSICS_CALLBACK_BODY_COLLISION,
_collision_cb, collision_data);

The callback function will get the collision body and check if its body is equal to which we want to delete.

_collision_cb(void *data, EPhysics_Body *body, void *event_info)
{
EPhysics_Body_Collision *collision = event_info;
Collision_Data *collision_data = data;
EPhysics_Body *contact_body;
contact_body = ephysics_body_collision_contact_body_get(collision);
if (contact_body != collision_data->sphere2) return;
collision_data->base.bodies = eina_list_remove(collision_data->base.bodies,
body);
INF("Collision Detected");
ephysics_body_del(body);
}

See _EPhysics_Callback_Body_Type for more event types.

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