The purpose of this example is to demonstrate the E\+Physics 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\textquotesingle{}ll have an E\+Physics\+\_\+\+World and two basic E\+Physics\+\_\+\+Bodys, we\textquotesingle{}ll apply an impulse in one of then and the other will be stopped \char`\"{}waiting\char`\"{} for a collision.

The basic concepts like -\/ initializing an E\+Physics\+\_\+\+World, render geometry, physics limiting boundaries, add an E\+Physics\+\_\+\+Body, associate it to evas objects, change restitution, friction and impulse properties, were already covered in \hyperlink{tutorial_ephysics_bouncing_ball}{E\+Physics -\/ Bouncing Ball}\hypertarget{tutorial_ephysics_velocity_add-callbacks}{}\section{Adding the Callback}\label{tutorial_ephysics_velocity_add-callbacks}


Calling \hyperlink{group__EPhysics__Body_gada176f4748a4c2fc4ba2042021ea7067}{ephysics\+\_\+body\+\_\+event\+\_\+callback\+\_\+add()} registers a callback to a given E\+Physics\+\_\+\+Body event type.

We\textquotesingle{}ll use two types\+:

\hyperlink{group__EPhysics__Body_gga904d0d1462e71c1f32cb27f540b5277cab8f46f167ee3872ea35fe645bf14a5d2}{E\+P\+H\+Y\+S\+I\+C\+S\+\_\+\+C\+A\+L\+L\+B\+A\+C\+K\+\_\+\+B\+O\+D\+Y\+\_\+\+D\+EL} \+: 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.


\begin{DoxyCodeInclude}
   \hyperlink{group__EPhysics__Body_gada176f4748a4c2fc4ba2042021ea7067}{ephysics\_body\_event\_callback\_add}(sphere\_body1,
                                    \hyperlink{group__EPhysics__Body_gga904d0d1462e71c1f32cb27f540b5277cab8f46f167ee3872ea35fe645bf14a5d2}{EPHYSICS\_CALLBACK\_BODY\_DEL},
                                    \_del\_cb, collision\_data);

\end{DoxyCodeInclude}


The callback function will receive the collision\+\_\+data and free some data associated with the body.




\begin{DoxyCodeInclude}
\_del\_cb(\textcolor{keywordtype}{void} *data, \hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body} *body, \textcolor{keywordtype}{void} *event\_info \_\_UNUSED\_\_)
\{
   Collision\_Data *collision\_data = data;
   \hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object} *obj, *shadow;

   obj = \hyperlink{group__EPhysics__Body_ga47ea07534cc809cd5d132495ebf2793a}{ephysics\_body\_evas\_object\_get}(body);
   shadow = \hyperlink{group__Evas__Object__Group__Extras_gafbad43015793eda4ae50202c0ce0e652}{evas\_object\_data\_get}(obj, \textcolor{stringliteral}{"shadow"});

   collision\_data->base.evas\_objs = \hyperlink{group__Eina__List__Group_ga7c0c6e07aa592a1cb2e010049a718035}{eina\_list\_remove}(
      collision\_data->base.evas\_objs, shadow);
   collision\_data->base.evas\_objs = \hyperlink{group__Eina__List__Group_ga7c0c6e07aa592a1cb2e010049a718035}{eina\_list\_remove}(
      collision\_data->base.evas\_objs, obj);

   \hyperlink{group__Evas__Object__Group__Basic_ga7e7a625dab7a3890532aa13b43117076}{evas\_object\_del}(shadow);
   \hyperlink{group__Evas__Object__Group__Basic_ga7e7a625dab7a3890532aa13b43117076}{evas\_object\_del}(obj);

   collision\_data->sphere = NULL;
\}

\end{DoxyCodeInclude}


\hyperlink{group__EPhysics__Body_gga904d0d1462e71c1f32cb27f540b5277caa002e2d26e86261221dc643f19071754}{E\+P\+H\+Y\+S\+I\+C\+S\+\_\+\+C\+A\+L\+L\+B\+A\+C\+K\+\_\+\+B\+O\+D\+Y\+\_\+\+C\+O\+L\+L\+I\+S\+I\+ON} \+: 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.


\begin{DoxyCodeInclude}
   \hyperlink{group__EPhysics__Body_gada176f4748a4c2fc4ba2042021ea7067}{ephysics\_body\_event\_callback\_add}(collision\_data->sphere,
                                    \hyperlink{group__EPhysics__Body_gga904d0d1462e71c1f32cb27f540b5277caa002e2d26e86261221dc643f19071754}{EPHYSICS\_CALLBACK\_BODY\_COLLISION},
                                    \_collision\_cb, collision\_data);

\end{DoxyCodeInclude}


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




\begin{DoxyCodeInclude}
\_collision\_cb(\textcolor{keywordtype}{void} *data, \hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body} *body, \textcolor{keywordtype}{void} *event\_info)
\{
   \hyperlink{group__EPhysics__Body_ga454052288e957641ff40f79ca3c72792}{EPhysics\_Body\_Collision} *collision = event\_info;
   Collision\_Data *collision\_data = data;
   \hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body} *contact\_body;

   contact\_body = \hyperlink{group__EPhysics__Body_gae94b1bd402657e11208cc90c5c3bbba4}{ephysics\_body\_collision\_contact\_body\_get}(
      collision);
   \textcolor{keywordflow}{if} (contact\_body != collision\_data->sphere2) \textcolor{keywordflow}{return};

   collision\_data->base.bodies = \hyperlink{group__Eina__List__Group_ga7c0c6e07aa592a1cb2e010049a718035}{eina\_list\_remove}(collision\_data->base.bodies,
                                                  body);

   \hyperlink{group__Eina__File__Group_gaa0bb6435252613b1a74175af3d60045f}{INF}(\textcolor{stringliteral}{"Collision Detected"});
   \hyperlink{group__EPhysics__Body_ga9f47ada6fe8ee0abb234bc4aaab7dd1c}{ephysics\_body\_del}(body);
\}

\end{DoxyCodeInclude}


See \hyperlink{group__EPhysics__Body_ga904d0d1462e71c1f32cb27f540b5277c}{\+\_\+\+E\+Physics\+\_\+\+Callback\+\_\+\+Body\+\_\+\+Type} for more event types.

Here we finish the example. The full source code can be found at \hyperlink{test_delete_c}{test\+\_\+delete.\+c}. 