\chapter{EPhysics -\/ Collision Detection}
\hypertarget{tutorial_ephysics_collision_detection}{}\label{tutorial_ephysics_collision_detection}\index{EPhysics -\/ Collision Detection@{EPhysics -\/ Collision Detection}}
The purpose of this example is to demonstrate the EPhysics 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 EPhysics\+\_\+\+World, and two basic EPhysics\+\_\+\+Bodys, we\textquotesingle{}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 \doxysectlink{tutorial_ephysics_bouncing_ball}{EPhysics -\/ Bouncing Ball}{0}\hypertarget{tutorial_ephysics_collision_detection_add-collstruct}{}\doxysection{\texorpdfstring{Collision Data Struct}{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{\ \ \ \ \ Evas\_Object\ *impact;}
\DoxyCodeLine{\ \ \ \ \ EPhysics\_Body\ *sphere;}
\DoxyCodeLine{\ \ \ \ \ EPhysics\_Body\ *sphere2;}
\DoxyCodeLine{\};}

\end{DoxyCodeInclude}
 \hypertarget{tutorial_ephysics_collision_detection_autotoc_md11}{}\doxysection{\texorpdfstring{Adding the Callback}{Adding the Callback}}\label{tutorial_ephysics_collision_detection_autotoc_md11}
Calling \doxylink{group__EPhysics__Body_gada176f4748a4c2fc4ba2042021ea7067}{ephysics\+\_\+body\+\_\+event\+\_\+callback\+\_\+add()} will register a callback to a type of physics body event.

\doxylink{group__EPhysics__Body_gga904d0d1462e71c1f32cb27f540b5277caa002e2d26e86261221dc643f19071754}{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.


\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 \doxylink{group__EPhysics__Body_ga904d0d1462e71c1f32cb27f540b5277c}{\+\_\+\+EPhysics\+\_\+\+Callback\+\_\+\+Body\+\_\+\+Type} for more event types.\hypertarget{tutorial_ephysics_collision_detection_add-collcb}{}\doxysection{\texorpdfstring{Collision Function}{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\+: \doxylink{group__EPhysics__Body_ga454052288e957641ff40f79ca3c72792}{EPhysics\+\_\+\+Body\+\_\+\+Collision}




\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\_collision\_cb(\textcolor{keywordtype}{void}\ *data,\ EPhysics\_Body\ *body\ \_\_UNUSED\_\_,\ \textcolor{keywordtype}{void}\ *event\_info)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ 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_gacb7ca0a444bf31b04b84f97d7198d392}{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{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group_ga335c8ee6eaeaac9710656af4f8b0db91}{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 \doxysectlink{test_collision_detection_c}{test\+\_\+collision\+\_\+detection.\+c}{0}. 