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[width=\textwidth,height=\textheight/2,keepaspectratio=true]{collision_detection}}
\end{DoxyImageNoCaption}


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 Ephysics\+\_\+\+Body, associate it to evas objects, change restitution, friction and impulse properties, were already covered in \mbox{\hyperlink{tutorial_ephysics_bouncing_ball}{E\+Physics -\/ Bouncing Ball}}\hypertarget{tutorial_ephysics_collision_detection_add-collstruct}{}\doxysection{Collision Data Struct}\label{tutorial_ephysics_collision_detection_add-collstruct}


While in this example we\textquotesingle{}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}{0}
\DoxyCodeLine{\textcolor{keyword}{struct }\_Collision\_Data \{}
\DoxyCodeLine{     Test\_Data base;}
\DoxyCodeLine{     \mbox{\hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object}} *impact;}
\DoxyCodeLine{     \mbox{\hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body}} *sphere;}
\DoxyCodeLine{     \mbox{\hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body}} *sphere2;}
\DoxyCodeLine{\};}

\end{DoxyCodeInclude}
\hypertarget{tutorial_ephysics_velocity_add-callbacks}{}\doxysection{Adding the Callback}\label{tutorial_ephysics_velocity_add-callbacks}
Calling \mbox{\hyperlink{group__EPhysics__Body_gada176f4748a4c2fc4ba2042021ea7067}{ephysics\+\_\+body\+\_\+event\+\_\+callback\+\_\+add()}} will register a callback to a type of physics body event.

\mbox{\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}{0}
\DoxyCodeLine{   \mbox{\hyperlink{group__EPhysics__Body_gada176f4748a4c2fc4ba2042021ea7067}{ephysics\_body\_event\_callback\_add}}(collision\_data-\/>sphere,}
\DoxyCodeLine{                                    \mbox{\hyperlink{group__EPhysics__Body_gga904d0d1462e71c1f32cb27f540b5277caa002e2d26e86261221dc643f19071754}{EPHYSICS\_CALLBACK\_BODY\_COLLISION}},}
\DoxyCodeLine{                                    \_collision\_cb, collision\_data);}

\end{DoxyCodeInclude}


See \mbox{\hyperlink{group__EPhysics__Body_ga904d0d1462e71c1f32cb27f540b5277c}{\+\_\+\+E\+Physics\+\_\+\+Callback\+\_\+\+Body\+\_\+\+Type}} for more event types.\hypertarget{tutorial_ephysics_collision_detection_add-collcb}{}\doxysection{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\+: \mbox{\hyperlink{group__EPhysics__Body_ga454052288e957641ff40f79ca3c72792}{E\+Physics\+\_\+\+Body\+\_\+\+Collision}}




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

\end{DoxyCodeInclude}


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


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{   contact\_body = \mbox{\hyperlink{group__EPhysics__Body_gae94b1bd402657e11208cc90c5c3bbba4}{ephysics\_body\_collision\_contact\_body\_get}}(collision);}
\DoxyCodeLine{   \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}{0}
\DoxyCodeLine{   \mbox{\hyperlink{group__EPhysics__Body_gabcbb3bd68d47bb0fdf071bb022763d58}{ephysics\_body\_collision\_position\_get}}(collision, \&x, \&y, \&z);}
\DoxyCodeLine{}
\DoxyCodeLine{   evas\_object\_move(collision\_data-\/>impact, x -\/ 10, y -\/ 40);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Elm__General_ga1beada7edf5d714c0dba5459bcc993fc}{elm\_object\_signal\_emit}}(collision\_data-\/>impact, \textcolor{stringliteral}{"impact,show"},}
\DoxyCodeLine{                          \textcolor{stringliteral}{"ephysics\_test"});}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}


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