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 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_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.
EPHYSICS_CALLBACK_BODY_DEL,
_del_cb, collision_data);
EPHYSICS_CALLBACK_BODY_DEL
@ EPHYSICS_CALLBACK_BODY_DEL
Body being deleted (called before free).
Definition EPhysics.h:2265

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_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.
EPhysics_Body
struct _EPhysics_Body EPhysics_Body
Body handle, represents an object on EPhysics world.
Definition EPhysics.h:655
eina_list_remove
EINA_API Eina_List * eina_list_remove(Eina_List *list, const void *data)
Removes the first instance of the specified data from the given list.
Definition eina_list.c:773
evas_object_del
EVAS_API void evas_object_del(Evas_Object *obj)
Marks the given Evas object for deletion (when Evas will free its memory).
Definition evas_object_main.c:928
Evas_Object
Efl_Canvas_Object Evas_Object
An Evas Object handle.
Definition Evas_Common.h:185

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);
EPHYSICS_CALLBACK_BODY_COLLISION
@ EPHYSICS_CALLBACK_BODY_COLLISION
Body collided with other body.
Definition EPhysics.h:2264

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);
}
EPhysics_Body_Collision
struct _EPhysics_Body_Collision EPhysics_Body_Collision
Body collision wraps collision informations.
Definition EPhysics.h:2247
ephysics_body_del
EAPI void ephysics_body_del(EPhysics_Body *body)
Delete a physics body.
ephysics_body_collision_contact_body_get
EAPI EPhysics_Body * ephysics_body_collision_contact_body_get(const EPhysics_Body_Collision *collision)
Get the body's collision contact body.
INF
#define INF(...)
Macro for logging Eina info messages.
Definition eina_file_common.h:173

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.