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{bouncing_ball}}
\end{DoxyImageNoCaption}


We'll guide you on defining a E\+Physics world, defining its render geometry and the physics limiting boundaries, you'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}{}\section{A test struct}\label{tutorial_ephysics_bouncing_ball_test-structure}


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}{typedef} \textcolor{keyword}{struct }\_Test\_Data Test\_Data;

\textcolor{keyword}{struct }\_Test\_Data \{
     \hyperlink{group__EPhysics__World_ga83c0d22744748525aa2dac11726c58e6}{EPhysics\_World} *world;
     \hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object} *layout;
     \hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object} *win;
     \hyperlink{struct__Eina__List}{Eina\_List} *constraints;
     \hyperlink{struct__Eina__List}{Eina\_List} *evas\_objs;
     \hyperlink{struct__Eina__List}{Eina\_List} *bodies;
     \textcolor{keywordtype}{void} *data;
\};

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


Calling \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's possible to stop a running world but its default status is running. Take a look at \hyperlink{group__EPhysics__World_ga994f911d6249986a8d1c21fd53bec462}{ephysics\+\_\+world\+\_\+running\+\_\+set()} for further informations about world running status.


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

\end{DoxyCodeInclude}
\hypertarget{tutorial_ephysics_bouncing_ball_render-geometry}{}\section{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's no physics limits but -\/ as we'll see later in this example -\/ boundaries can be added by issuing either \hyperlink{group__EPhysics__Body_ga2a3e40784ef30f15d5d7e9f1f2a814b3}{ephysics\+\_\+body\+\_\+top\+\_\+boundary\+\_\+add()}, \hyperlink{group__EPhysics__Body_gac5fc5a1817ac81e2f1fcdcd8908614f2}{ephysics\+\_\+body\+\_\+bottom\+\_\+boundary\+\_\+add()}, \hyperlink{group__EPhysics__Body_ga4ad52252b2537c6f41e14b9135847fe9}{ephysics\+\_\+body\+\_\+left\+\_\+boundary\+\_\+add()} and \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}
   \hyperlink{group__EPhysics__World_gaabf8abaf9c41b0c1e5daa4acf3df0356}{ephysics\_world\_render\_geometry\_set}(world, 50, 40, -50,
                                      WIDTH - 100, FLOOR\_Y - 40, DEPTH);

\end{DoxyCodeInclude}
\hypertarget{tutorial_ephysics_bouncing_ball_boundaries}{}\section{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 \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}
   boundary = \hyperlink{group__EPhysics__Body_gac5fc5a1817ac81e2f1fcdcd8908614f2}{ephysics\_body\_bottom\_boundary\_add}(test\_data->world);
   \hyperlink{group__EPhysics__Body_ga731c9a6443f7c4294fce23fc1287b778}{ephysics\_body\_restitution\_set}(boundary, 0.65);
   \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't want to make it bounce as much as it is when hits the ground.


\begin{DoxyCodeInclude}
   boundary = \hyperlink{group__EPhysics__Body_gab7e598db12a5270311c429f0ee12271c}{ephysics\_body\_right\_boundary\_add}(test\_data->world);
   \hyperlink{group__EPhysics__Body_ga731c9a6443f7c4294fce23fc1287b778}{ephysics\_body\_restitution\_set}(boundary, 0.4);
   \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}
   boundary = \hyperlink{group__EPhysics__Body_ga4ad52252b2537c6f41e14b9135847fe9}{ephysics\_body\_left\_boundary\_add}(test\_data->world);
   \hyperlink{group__EPhysics__Body_ga731c9a6443f7c4294fce23fc1287b778}{ephysics\_body\_restitution\_set}(boundary, 0.4);
   \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't want our ball to fly all over there, we want to limit its upper movements, it'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}
   \hyperlink{group__EPhysics__Body_ga2a3e40784ef30f15d5d7e9f1f2a814b3}{ephysics\_body\_top\_boundary\_add}(test\_data->world);

\end{DoxyCodeInclude}
 \hypertarget{tutorial_ephysics_bouncing_ball_world-populate}{}\section{Adding a ball}\label{tutorial_ephysics_bouncing_ball_world-populate}
Since we have defined the physics limits with our boundaries it'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'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}
   sphere = elm\_image\_add(test\_data->win);
   elm\_image\_file\_set(
      sphere, PACKAGE\_DATA\_DIR \textcolor{stringliteral}{"/"} EPHYSICS\_TEST\_THEME \textcolor{stringliteral}{".edj"}, \textcolor{stringliteral}{"big-blue-ball"});
   evas\_object\_move(sphere, WIDTH / 3, HEIGHT / 8);
   evas\_object\_resize(sphere, 70, 70);
   \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 \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}

\end{DoxyCodeInclude}
\hypertarget{tutorial_ephysics_bouncing_ball_jumping-ball}{}\section{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}

\end{DoxyCodeInclude}




The jumping callback implementation consists on handling only key up events and discarding any other input event we get. We'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 \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'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}
\_on\_keydown(\textcolor{keywordtype}{void} *data, \hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object} *obj \_\_UNUSED\_\_, \hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object} *src \_\_UNUSED\_\_, 
      \hyperlink{Evas__Common_8h_a076b2a9b2de2b2144a4193b1d12ed448}{Evas\_Callback\_Type} type, \textcolor{keywordtype}{void} *event\_info)
\{
   \hyperlink{struct__Evas__Event__Key__Down}{Evas\_Event\_Key\_Down} *ev = event\_info;
   \hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body} *body = data;

   \textcolor{keywordflow}{if} (type != \hyperlink{Evas__Common_8h_a99e208e463a20499aabe6faac8a8cb8aac436b50ad665089f8a24b0fb827cec5f}{EVAS\_CALLBACK\_KEY\_UP})
     \textcolor{keywordflow}{return} \hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE};

   \textcolor{keywordflow}{if} (strcmp(ev->\hyperlink{struct__Evas__Event__Key__Down_a70473bba88a1afaee180a54150513824}{key}, \textcolor{stringliteral}{"Up"}) == 0)
     \hyperlink{group__EPhysics__Body_ga9109b39047a724640f6a18c683c9eb07}{ephysics\_body\_central\_impulse\_apply}(body, 0, -300, 0);
   \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (strcmp(ev->\hyperlink{struct__Evas__Event__Key__Down_a70473bba88a1afaee180a54150513824}{key}, \textcolor{stringliteral}{"Down"}) == 0)
     \hyperlink{group__EPhysics__Body_ga9109b39047a724640f6a18c683c9eb07}{ephysics\_body\_central\_impulse\_apply}(body, 0, 300, 0);
   \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (strcmp(ev->\hyperlink{struct__Evas__Event__Key__Down_a70473bba88a1afaee180a54150513824}{key}, \textcolor{stringliteral}{"Right"}) == 0)
     \hyperlink{group__EPhysics__Body_ga9109b39047a724640f6a18c683c9eb07}{ephysics\_body\_central\_impulse\_apply}(body, 300, 0, 0);
   \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (strcmp(ev->\hyperlink{struct__Evas__Event__Key__Down_a70473bba88a1afaee180a54150513824}{key}, \textcolor{stringliteral}{"Left"}) == 0)
     \hyperlink{group__EPhysics__Body_ga9109b39047a724640f6a18c683c9eb07}{ephysics\_body\_central\_impulse\_apply}(body, -300, 0, 0);

   \textcolor{keywordflow}{return} \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
\}

\end{DoxyCodeInclude}


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