The purpose of this example is to demonstrate the E\+Physics\+\_\+\+Camera Track usage.

The E\+Physics\+\_\+\+Camera facilitates the usage of scenarios bigger than the viewport, thats because the E\+Physics handles the position of objects which has control.

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


For this example we'll have an E\+Physics\+\_\+\+World, one main E\+Physics\+\_\+\+Body that will be tracked by an E\+Physics\+\_\+\+Camera on three ways, horizontal, vertical and full tracking. Also nine E\+Physics\+\_\+\+Bodys with mass 0, that will be used as scenario in order to our main body change its position on x and y axes when passes through this scenario.

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_camera_track_add-trkstruct}{}\section{Track Data Struct}\label{tutorial_ephysics_camera_track_add-trkstruct}


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 }\_Track\_Data \{
     Test\_Data base;
     \hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body} *body;
     \hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object} *sp;
     \textcolor{keywordtype}{int} old\_cx;
\};

\end{DoxyCodeInclude}
\hypertarget{tutorial_ephysics_camera_track_add-camera}{}\section{Adding a Camera}\label{tutorial_ephysics_camera_track_add-camera}
In this example we'll use 3 kinds of tracking, to change this values we'll have an Elementary spinner widget and handle it on this function.

Every world has a camera, so here we get this camera used by our E\+Physics\+\_\+\+World.


\begin{DoxyCodeInclude}
\_track\_apply(Track\_Data *track\_data)
\{
   \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool} hor = \hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE};
   \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool} ver = \hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE};
   \hyperlink{group__EPhysics__Camera_ga945948dcbf4d98beb457c77b6537116f}{EPhysics\_Camera} *camera;
   \hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body} *body;
   \textcolor{keywordtype}{int} mode;

   body = track\_data->body;
   camera = \hyperlink{group__EPhysics__World_ga97fe0c2a98a7998f29f7390cea868ff0}{ephysics\_world\_camera\_get}(track\_data->base.world);

\end{DoxyCodeInclude}


Here we'll get the elm\+\_\+spinner value to the tracking base on this value


\begin{DoxyCodeInclude}
   mode = (int) elm\_spinner\_value\_get(track\_data->sp);

   \textcolor{keywordflow}{switch} (mode)
     \{
      \textcolor{keywordflow}{case} 1:
         hor = \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
         \textcolor{keywordflow}{break};
      \textcolor{keywordflow}{case} 3:
         hor = \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
      \textcolor{keywordflow}{case} 2:
         ver = \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
     \}

\end{DoxyCodeInclude}


Here we'll set the camera to track the body, when a body is tracked, the camera will move automatically, following this body. It will keeps the body centralized on rendered area. If it will be centralized horizontally and / or vertically depends if parameters horizontal and vertical are set to E\+I\+N\+A\+\_\+\+T\+R\+U\+E, in this case we based these values on elm\+\_\+spinner.


\begin{DoxyCodeInclude}
   \hyperlink{group__EPhysics__Camera_gabb7602830e3a9daa67d7bbe1a71a776e}{ephysics\_camera\_body\_track}(camera, body, hor, ver);
\}

\end{DoxyCodeInclude}
\hypertarget{tutorial_ephysics_camera_track_add-uptfloor}{}\section{Updating the floor}\label{tutorial_ephysics_camera_track_add-uptfloor}
Here we'll use 2 floor images to give the impression of an infinite ground.

Calling \hyperlink{group__EPhysics__World_ga2b621ffb39920e4619fe2ec2e5508073}{ephysics\+\_\+world\+\_\+event\+\_\+callback\+\_\+add()} will register a callback to a type of physics world event.

\hyperlink{group__EPhysics__World_gga0b0aff31f415be24679d4ab703674619aa5f6adbd98bc6599221d12ecfeb1cbc6}{E\+P\+H\+Y\+S\+I\+C\+S\+\_\+\+C\+A\+L\+L\+B\+A\+C\+K\+\_\+\+W\+O\+R\+L\+D\+\_\+\+C\+A\+M\+E\+R\+A\+\_\+\+M\+O\+V\+E\+D} \+: called if the camera position changed on physics simulation tick.


\begin{DoxyCodeInclude}
   \hyperlink{group__EPhysics__World_ga2b621ffb39920e4619fe2ec2e5508073}{ephysics\_world\_event\_callback\_add}(world,
                                     \hyperlink{group__EPhysics__World_gga0b0aff31f415be24679d4ab703674619aa5f6adbd98bc6599221d12ecfeb1cbc6}{EPHYSICS\_CALLBACK\_WORLD\_CAMERA\_MOVED}
      ,
                                     \_camera\_moved\_cb, track\_data);

\end{DoxyCodeInclude}


In the function, we'll get the cameras position to know how much the camera moved and move the same value to the floor passing it as delta\+\_\+x to the function, note that we use an old\+\_\+x variable to do this calculation.

We'll get also if the body is being tracked on x and y axes. If the body isn't being tracked on x axis the floors x position won't change, delta\+\_\+x will be zero.




\begin{DoxyCodeInclude}
\_camera\_moved\_cb(\textcolor{keywordtype}{void} *data, \hyperlink{group__EPhysics__World_ga83c0d22744748525aa2dac11726c58e6}{EPhysics\_World} *world \_\_UNUSED\_\_, \textcolor{keywordtype}{void} *event\_info)
\{
   \hyperlink{group__EPhysics__Camera_ga945948dcbf4d98beb457c77b6537116f}{EPhysics\_Camera} *camera = event\_info;
   Track\_Data *track\_data = data;
   \textcolor{keywordtype}{int} cx, cy, delta\_x = 0;
   \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool} hor, ver;

   \hyperlink{group__Eina__File__Group_gae8924623ca601364965e3d88fb7f40fa}{DBG}(\textcolor{stringliteral}{"Camera moved"});

   \hyperlink{group__EPhysics__Camera_ga3f8d5e51ae54ac3c2a5bdc0a6fb162c6}{ephysics\_camera\_tracked\_body\_get}(camera, NULL, &hor, &ver);
   \hyperlink{group__EPhysics__Camera_ga5e549e85d0d98a878dc636b6463645d6}{ephysics\_camera\_position\_get}(camera, &cx, &cy);

   \textcolor{keywordflow}{if} (hor)
     delta\_x = track\_data->old\_cx - cx;

   \_update\_floor(track\_data->base.layout, \textcolor{stringliteral}{"floor"}, delta\_x, cy, ver);
   \_update\_floor(track\_data->base.layout, \textcolor{stringliteral}{"floor2"}, delta\_x, cy, ver);

   track\_data->old\_cx = cx;
\}

\end{DoxyCodeInclude}


Here we get the floors position and plus the delta\+\_\+x value to move the floor in the same \char`\"{}velocity\char`\"{}.




\begin{DoxyCodeInclude}
\_update\_floor(\hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object} *layout, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *name, \textcolor{keywordtype}{int} delta\_x, \textcolor{keywordtype}{int} cy, 
      \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool} ver)
\{
   \hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object} *floor\_obj;
   \textcolor{keywordtype}{int} x, y, fx, fy;

   floor\_obj = \hyperlink{group__Evas__Object__Group__Extras_gafbad43015793eda4ae50202c0ce0e652}{evas\_object\_data\_get}(layout, name);
   \hyperlink{group__Evas__Object__Group__Basic_ga6f52fab1a96dc58c85ee481d84bac871}{evas\_object\_geometry\_get}(floor\_obj, &x, &y, NULL, NULL);
   fx = x + delta\_x;

\end{DoxyCodeInclude}


We use 2 floor images because whenever one exits the screen by the left side, another is being shown, when it happens the one which exit the screen is sent to the right side, entering into an infinite loop, giving the impression of an infinite ground image. Its important to note that we need to use the fx to don't gap the images.

Note that the fy is being defined considering its offsets, -\/20 is to the floor image be above the floor, thus having an border above the collision point, +40 is the render area height, to offset the cameras y, basically to draw in the correct position in the canvas.


\begin{DoxyCodeInclude}
   \textcolor{keywordflow}{if} (fx < -FLOOR\_WIDTH)
     fx += 2 * FLOOR\_WIDTH;

   fy = (ver) ? FLOOR\_Y - 20 - cy + 40 : y;
   evas\_object\_move(floor\_obj, fx, fy);
\}

\end{DoxyCodeInclude}


Here we finish the example. The full source code can be found at \hyperlink{test_camera_track_c}{test\+\_\+camera\+\_\+track.\+c}. 