The purpose of this example is to demonstrate the E\+Physics Collision Detection usage -\/ The code adds two balls, one with impulse and the second with a collision detection callback, to show an effect.

 
\begin{DoxyImageNoCaption}
  \mbox{\includegraphics{collision_detection}}
\end{DoxyImageNoCaption}


For this example we'll have an E\+Physics\+\_\+\+World, and two basic E\+Physics\+\_\+\+Bodys, we'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 Ephysics\+\_\+\+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_collision_detection_add-collstruct}{}\section{Collision Data Struct}\label{tutorial_ephysics_collision_detection_add-collstruct}


While in this example we'll be working with a struct to hold some objects in our code. For clarity sake we present you the struct declaration in the following block.


\begin{DoxyCodeInclude}
\textcolor{keyword}{struct }\_Collision\_Data \{
     Test\_Data base;
     \hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object} *impact;
     \hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body} *sphere;
     \hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body} *sphere2;
\};

\end{DoxyCodeInclude}
\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()} will register a callback to a type of physics body event.

\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\+O\+N} \+: 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}


See \hyperlink{group__EPhysics__Body_ga904d0d1462e71c1f32cb27f540b5277c}{\+\_\+\+E\+Physics\+\_\+\+Callback\+\_\+\+Body\+\_\+\+Type} for more event types.\hypertarget{tutorial_ephysics_collision_detection_add-collcb}{}\section{Collision Function}\label{tutorial_ephysics_collision_detection_add-collcb}
The callback function will filter the collision to be sure if that body is which we want and then show the effect.

First we need to create a specific variable type to get collision infos\+: \hyperlink{group__EPhysics__Body_ga454052288e957641ff40f79ca3c72792}{E\+Physics\+\_\+\+Body\+\_\+\+Collision}




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

\end{DoxyCodeInclude}


Now we want to know which body collides with and filter it.


\begin{DoxyCodeInclude}
   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};

\end{DoxyCodeInclude}


We just get the collision position, move the impact effect to this coordinate and send a signal to edje to show it.


\begin{DoxyCodeInclude}
   \hyperlink{group__EPhysics__Body_gabcbb3bd68d47bb0fdf071bb022763d58}{ephysics\_body\_collision\_position\_get}(collision, &x, &y, &z);

   evas\_object\_move(collision\_data->impact, x - 10, y - 40);
   elm\_object\_signal\_emit(collision\_data->impact, \textcolor{stringliteral}{"impact,show"},
                          \textcolor{stringliteral}{"ephysics\_test"});
\}

\end{DoxyCodeInclude}


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