The purpose of this example is to show how to write an simple application -\/ as the name suggests -\/ with a small ball bouncing on the ground and responding to users events by making it jump -\/ applying a central impulse on it.

 
\begin{DoxyImageNoCaption}
  \mbox{\includegraphics[width=\textwidth,height=\textheight/2,keepaspectratio=true]{bouncing_ball}}
\end{DoxyImageNoCaption}


We\textquotesingle{}ll guide you on defining a E\+Physics world, defining its render geometry and the physics limiting boundaries, you\textquotesingle{}ll learn how to add E\+Physics bodies and how to associate it to evas objects. We also explain how to change restitution and friction properties. We see how to apply central impulse on a E\+Physics\+\_\+\+Body by implementing an elementary input event callback and calling the proper function.\hypertarget{tutorial_ephysics_bouncing_ball_test-structure}{}\doxysection{A test struct}\label{tutorial_ephysics_bouncing_ball_test-structure}


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}{typedef} \textcolor{keyword}{struct }\_Test\_Data Test\_Data;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{struct }\_Test\_Data \{}
\DoxyCodeLine{     \mbox{\hyperlink{group__EPhysics__World_ga83c0d22744748525aa2dac11726c58e6}{EPhysics\_World}} *world;}
\DoxyCodeLine{     \mbox{\hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object}} *layout;}
\DoxyCodeLine{     \mbox{\hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object}} *win;}
\DoxyCodeLine{     \mbox{\hyperlink{struct__Eina__List}{Eina\_List}} *constraints;}
\DoxyCodeLine{     \mbox{\hyperlink{struct__Eina__List}{Eina\_List}} *evas\_objs;}
\DoxyCodeLine{     \mbox{\hyperlink{struct__Eina__List}{Eina\_List}} *bodies;}
\DoxyCodeLine{     \textcolor{keywordtype}{void} *data;}
\DoxyCodeLine{\};}

\end{DoxyCodeInclude}
\hypertarget{tutorial_ephysics_bouncing_ball_world-new}{}\doxysection{World Initialization}\label{tutorial_ephysics_bouncing_ball_world-new}


Calling \mbox{\hyperlink{group__EPhysics__World_ga108e2d7df8c81321b8d5bde8497f0d45}{ephysics\+\_\+world\+\_\+new()}} will create a new physics world with its collision configuration, constraint solver, broadphase interface and dispatcher.

The default gravity is set to -\/9.\+81. It\textquotesingle{}s possible to stop a running world but its default status is running. Take a look at \mbox{\hyperlink{group__EPhysics__World_ga994f911d6249986a8d1c21fd53bec462}{ephysics\+\_\+world\+\_\+running\+\_\+set()}} for further informations about world running status.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{   world = \mbox{\hyperlink{group__EPhysics__World_ga108e2d7df8c81321b8d5bde8497f0d45}{ephysics\_world\_new}}();}

\end{DoxyCodeInclude}
\hypertarget{tutorial_ephysics_bouncing_ball_render-geometry}{}\doxysection{Render geometry}\label{tutorial_ephysics_bouncing_ball_render-geometry}
By setting the render geometry you tell ephysics the dimensions of rendered area to be take on account by default updates.

By default it starts with null x, y, z, width, height and depth. Initially there\textquotesingle{}s no physics limits but -\/ as we\textquotesingle{}ll see later in this example -\/ boundaries can be added by issuing either \mbox{\hyperlink{group__EPhysics__Body_ga2a3e40784ef30f15d5d7e9f1f2a814b3}{ephysics\+\_\+body\+\_\+top\+\_\+boundary\+\_\+add()}}, \mbox{\hyperlink{group__EPhysics__Body_gac5fc5a1817ac81e2f1fcdcd8908614f2}{ephysics\+\_\+body\+\_\+bottom\+\_\+boundary\+\_\+add()}}, \mbox{\hyperlink{group__EPhysics__Body_ga4ad52252b2537c6f41e14b9135847fe9}{ephysics\+\_\+body\+\_\+left\+\_\+boundary\+\_\+add()}} and \mbox{\hyperlink{group__EPhysics__Body_gab7e598db12a5270311c429f0ee12271c}{ephysics\+\_\+body\+\_\+right\+\_\+boundary\+\_\+add()}}.

While setting the worlds render geometry the first parameter is our just created world, the following parameters indicate the x, y, z, width, height and depth of our area of interest.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{   \mbox{\hyperlink{group__EPhysics__World_gaabf8abaf9c41b0c1e5daa4acf3df0356}{ephysics\_world\_render\_geometry\_set}}(world, 50, 40, -\/50,}
\DoxyCodeLine{                                      WIDTH -\/ 100, FLOOR\_Y -\/ 40, DEPTH);}

\end{DoxyCodeInclude}
\hypertarget{tutorial_ephysics_bouncing_ball_boundaries}{}\doxysection{Adding boundaries}\label{tutorial_ephysics_bouncing_ball_boundaries}
Boundaries are physics limits added by E\+Physics which you can use to limit the area where your objects can move around. Bear in mind that those boundaries are created by E\+Physics taking in account the render geometry you have previously defined by calling \mbox{\hyperlink{group__EPhysics__World_gaabf8abaf9c41b0c1e5daa4acf3df0356}{ephysics\+\_\+world\+\_\+render\+\_\+geometry\+\_\+set()}}.

In our example we start by adding a bottom boundary. This E\+Physics\+\_\+\+Body represents a physics limit under the world render geometry.

The second line states the restitution factor for that bottom boundary, and the third line its friction. These changes will make our ball to bounce whenever it hits the ground.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{   boundary = \mbox{\hyperlink{group__EPhysics__Body_gac5fc5a1817ac81e2f1fcdcd8908614f2}{ephysics\_body\_bottom\_boundary\_add}}(test\_data-\/>world);}
\DoxyCodeLine{   \mbox{\hyperlink{group__EPhysics__Body_ga731c9a6443f7c4294fce23fc1287b778}{ephysics\_body\_restitution\_set}}(boundary, 0.65);}
\DoxyCodeLine{   \mbox{\hyperlink{group__EPhysics__Body_ga986d45060bf6ba1689a56889a2bcabce}{ephysics\_body\_friction\_set}}(boundary, 4);}

\end{DoxyCodeInclude}


Then we add a right boundary limiting the physics world on the left side, we also change its restitution and friction factors but with a smaller value, we don\textquotesingle{}t want to make it bounce as much as it is when hits the ground.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{   boundary = \mbox{\hyperlink{group__EPhysics__Body_gab7e598db12a5270311c429f0ee12271c}{ephysics\_body\_right\_boundary\_add}}(test\_data-\/>world);}
\DoxyCodeLine{   \mbox{\hyperlink{group__EPhysics__Body_ga731c9a6443f7c4294fce23fc1287b778}{ephysics\_body\_restitution\_set}}(boundary, 0.4);}
\DoxyCodeLine{   \mbox{\hyperlink{group__EPhysics__Body_ga986d45060bf6ba1689a56889a2bcabce}{ephysics\_body\_friction\_set}}(boundary, 3);}

\end{DoxyCodeInclude}


We also add a left boundary taking the same considerations for right boundary.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{   boundary = \mbox{\hyperlink{group__EPhysics__Body_ga4ad52252b2537c6f41e14b9135847fe9}{ephysics\_body\_left\_boundary\_add}}(test\_data-\/>world);}
\DoxyCodeLine{   \mbox{\hyperlink{group__EPhysics__Body_ga731c9a6443f7c4294fce23fc1287b778}{ephysics\_body\_restitution\_set}}(boundary, 0.4);}
\DoxyCodeLine{   \mbox{\hyperlink{group__EPhysics__Body_ga986d45060bf6ba1689a56889a2bcabce}{ephysics\_body\_friction\_set}}(boundary, 3);}

\end{DoxyCodeInclude}


One of this examples requirements is to make the ball jump after a specific user event, so the ball can suffer an impulse for any direction.

With an upper impulse we don\textquotesingle{}t want our ball to fly all over there, we want to limit its upper movements, it\textquotesingle{}s intended to limit the ball movement within a box, it should not leave the render geometry area, for that purpose we must define a top boundary.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{   \mbox{\hyperlink{group__EPhysics__Body_ga2a3e40784ef30f15d5d7e9f1f2a814b3}{ephysics\_body\_top\_boundary\_add}}(test\_data-\/>world);}

\end{DoxyCodeInclude}
 \hypertarget{tutorial_ephysics_bouncing_ball_world-populate}{}\doxysection{Adding a ball}\label{tutorial_ephysics_bouncing_ball_world-populate}
Since we have defined the physics limits with our boundaries it\textquotesingle{}s time to add some fun. Here we add a ball as an elementary image widget and tell ephysics about it.

After setting the file that will be used as the image\textquotesingle{}s source of our elm image we move it to the center of render geometry and resize it to 70x70 pixels and show it.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{   sphere = \mbox{\hyperlink{group__Elm__Image_ga5d211c8af4f6e297a24fc4e8737c81a9}{elm\_image\_add}}(test\_data-\/>win);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Elm__Image_gad1a96ede5f513042cef3c1dac4572f73}{elm\_image\_file\_set}}(}
\DoxyCodeLine{      sphere, PACKAGE\_DATA\_DIR \textcolor{stringliteral}{"/"} EPHYSICS\_TEST\_THEME \textcolor{stringliteral}{".edj"}, \textcolor{stringliteral}{"big-\/blue-\/ball"});}
\DoxyCodeLine{   evas\_object\_move(sphere, WIDTH / 3, HEIGHT / 8);}
\DoxyCodeLine{   evas\_object\_resize(sphere, 70, 70);}
\DoxyCodeLine{   \mbox{\hyperlink{group__Evas__Object__Group__Basic_ga9cbc13661584e49fb9d9cdab514a1eeb}{evas\_object\_show}}(sphere);}

\end{DoxyCodeInclude}


The evas object is just set and we must tell E\+Physics about it, creating the E\+Physics\+\_\+\+Body representing our ball and associating it to the just created evas object.

Once the ball has been moved to the center of render geometry it should start falling after associating it to the E\+Physics\+\_\+\+Body. By default its mass is initially set to 1 kilo, but it can be changed by calling \mbox{\hyperlink{group__EPhysics__Body_gaa0915777f634079ca6c088fda5c20e8f}{ephysics\+\_\+body\+\_\+mass\+\_\+set()}}. Bear in mind that if you change its mass to 0 kilos it becomes a static body and will not move at all, the body will remain fixed in the initial position.

In the following code the first line adds a circle body, then we associate the evas object to E\+Physics\+\_\+\+Body, E\+Physics will map every changes on physics object simulation to its evas object. Some restitution and friction factors are added as well.


\begin{DoxyCodeInclude}{0}

\end{DoxyCodeInclude}
\hypertarget{tutorial_ephysics_bouncing_ball_jumping-ball}{}\doxysection{Making it jump}\label{tutorial_ephysics_bouncing_ball_jumping-ball}
The next step is to give us the ability to make our ball to jump -\/ actually apply some impulse whenever a key has been pressed. Then we add a elementary input callback to the window widget.


\begin{DoxyCodeInclude}{0}

\end{DoxyCodeInclude}




The jumping callback implementation consists on handling only key up events and discarding any other input event we get. We\textquotesingle{}re interested on keyboard events only. All the operations done in the following lines are done on sphere E\+Physics\+\_\+\+Body previously created.

We mainly use the \mbox{\hyperlink{group__EPhysics__Body_ga9109b39047a724640f6a18c683c9eb07}{ephysics\+\_\+body\+\_\+central\+\_\+impulse\+\_\+apply()}} function. This function applies an impulse on the center of a body.

Once pressed $<$Up$>$ key it applies a central impulse of 0 kilos on X axis, 10 kilos on Y and 0 kilos on Z -\/ so the ball is forced up.

If $<$Down$>$ key has been pressed we apply an impulse of 0 kilos on X axis, -\/10 kilos on Y and 0 kilos on Z -\/ here the ball is forced down.

In the case of $<$Right$>$ key pressing it\textquotesingle{}s applied an impulse of 10 kilos on X axis, 0 kilos on Y and 0 kilos on Z -\/ which applies a force to the right side. But if the key being pressed is $<$Left$>$ the opposite is done, and an impulse of -\/10 kilos is applied on X, 0 kilos on Y and 0 kilos on Z -\/ and the ball is forced to the left.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\_on\_keydown(\textcolor{keywordtype}{void} *data, \mbox{\hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object}} *obj \_\_UNUSED\_\_, \mbox{\hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object}} *src \_\_UNUSED\_\_, \mbox{\hyperlink{Evas__Common_8h_ad64cde0da38a60e7cb7905b3ab216968}{Evas\_Callback\_Type}} type, \textcolor{keywordtype}{void} *event\_info)}
\DoxyCodeLine{\{}
\DoxyCodeLine{   \mbox{\hyperlink{struct__Evas__Event__Key__Down}{Evas\_Event\_Key\_Down}} *ev = event\_info;}
\DoxyCodeLine{   \mbox{\hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body}} *body = data;}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (type != \mbox{\hyperlink{Evas__Common_8h_ad64cde0da38a60e7cb7905b3ab216968ac436b50ad665089f8a24b0fb827cec5f}{EVAS\_CALLBACK\_KEY\_UP}})}
\DoxyCodeLine{     \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{if} (strcmp(ev-\/>\mbox{\hyperlink{struct__Evas__Event__Key__Down_a70473bba88a1afaee180a54150513824}{key}}, \textcolor{stringliteral}{"Up"}) == 0)}
\DoxyCodeLine{     \mbox{\hyperlink{group__EPhysics__Body_ga9109b39047a724640f6a18c683c9eb07}{ephysics\_body\_central\_impulse\_apply}}(body, 0, -\/300, 0);}
\DoxyCodeLine{   \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (strcmp(ev-\/>\mbox{\hyperlink{struct__Evas__Event__Key__Down_a70473bba88a1afaee180a54150513824}{key}}, \textcolor{stringliteral}{"Down"}) == 0)}
\DoxyCodeLine{     \mbox{\hyperlink{group__EPhysics__Body_ga9109b39047a724640f6a18c683c9eb07}{ephysics\_body\_central\_impulse\_apply}}(body, 0, 300, 0);}
\DoxyCodeLine{   \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (strcmp(ev-\/>\mbox{\hyperlink{struct__Evas__Event__Key__Down_a70473bba88a1afaee180a54150513824}{key}}, \textcolor{stringliteral}{"Right"}) == 0)}
\DoxyCodeLine{     \mbox{\hyperlink{group__EPhysics__Body_ga9109b39047a724640f6a18c683c9eb07}{ephysics\_body\_central\_impulse\_apply}}(body, 300, 0, 0);}
\DoxyCodeLine{   \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (strcmp(ev-\/>\mbox{\hyperlink{struct__Evas__Event__Key__Down_a70473bba88a1afaee180a54150513824}{key}}, \textcolor{stringliteral}{"Left"}) == 0)}
\DoxyCodeLine{     \mbox{\hyperlink{group__EPhysics__Body_ga9109b39047a724640f6a18c683c9eb07}{ephysics\_body\_central\_impulse\_apply}}(body, -\/300, 0, 0);}
\DoxyCodeLine{}
\DoxyCodeLine{   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}


Here we finish the very simple bouncing ball example. The full source code can be found at \mbox{\hyperlink{test_bouncing_ball_c}{test\+\_\+bouncing\+\_\+ball.\+c}}. 