The purpose of this example is to demonstrate the E\+Physics Velocity usage -\/ The code adds a small bouncing ball on the ground and responding to users events by making it jump -\/ applying a central impulse on it and showing its velocity and acceleration.

We\textquotesingle{}ll see in this example how to get E\+Physics\+\_\+\+Body Linear and Angular velocity and acceleration.

For this example we\textquotesingle{}ll have an E\+Physics\+\_\+\+World and one basic E\+Physics\+\_\+\+Body, we\textquotesingle{}ll apply impulses that follows user events, it were already covered in \hyperlink{tutorial_ephysics_bouncing_ball}{E\+Physics -\/ Bouncing Ball}\hypertarget{tutorial_ephysics_velocity_add-velstruct}{}\section{Velocity Data Struct}\label{tutorial_ephysics_velocity_add-velstruct}


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}
\textcolor{keyword}{struct }\_Velocity\_Data \{
     Test\_Data base;
     \hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body} *body;
     \textcolor{keywordtype}{double} old\_vx;
     \textcolor{keywordtype}{double} old\_vy;
     \textcolor{keywordtype}{double} old\_vaz;
     \textcolor{keywordtype}{double} last\_time;
\};

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

\hyperlink{group__EPhysics__Body_gga904d0d1462e71c1f32cb27f540b5277cacb2363cd59bc8642e74ad916c6658e0a}{E\+P\+H\+Y\+S\+I\+C\+S\+\_\+\+C\+A\+L\+L\+B\+A\+C\+K\+\_\+\+B\+O\+D\+Y\+\_\+\+U\+P\+D\+A\+T\+E} \+: called after every physics iteration. In other words, will be called after each world tick.


\begin{DoxyCodeInclude}
   \hyperlink{group__EPhysics__Body_gada176f4748a4c2fc4ba2042021ea7067}{ephysics\_body\_event\_callback\_add}(sphere\_body, 
      \hyperlink{group__EPhysics__Body_gga904d0d1462e71c1f32cb27f540b5277cacb2363cd59bc8642e74ad916c6658e0a}{EPHYSICS\_CALLBACK\_BODY\_UPDATE},
                                    \_update\_vel\_cb, velocity\_data);

\end{DoxyCodeInclude}


\hyperlink{group__EPhysics__Body_gga904d0d1462e71c1f32cb27f540b5277cae88f6dfe5c24a67f5f074fb818edfa72}{E\+P\+H\+Y\+S\+I\+C\+S\+\_\+\+C\+A\+L\+L\+B\+A\+C\+K\+\_\+\+B\+O\+D\+Y\+\_\+\+S\+T\+O\+P\+P\+E\+D} \+: called when a body is found to be stopped. In other words, when the body is not moving anymore.


\begin{DoxyCodeInclude}
   \hyperlink{group__EPhysics__Body_gada176f4748a4c2fc4ba2042021ea7067}{ephysics\_body\_event\_callback\_add}(sphere\_body, 
      \hyperlink{group__EPhysics__Body_gga904d0d1462e71c1f32cb27f540b5277cae88f6dfe5c24a67f5f074fb818edfa72}{EPHYSICS\_CALLBACK\_BODY\_STOPPED},
                                    \_update\_vel\_cb, velocity\_data);

\end{DoxyCodeInclude}


See \hyperlink{group__EPhysics__Body_ga904d0d1462e71c1f32cb27f540b5277c}{\+\_\+\+E\+Physics\+\_\+\+Callback\+\_\+\+Body\+\_\+\+Type} for more event types.\hypertarget{tutorial_ephysics_velocity_add-velcb}{}\section{Velocity Function}\label{tutorial_ephysics_velocity_add-velcb}
The callback function will be called on every physics iteration to show the linear and angular velocity and acceleration.

Here we\textquotesingle{}re declaring the necessary variables to calculate acelerations and delta time. And checking if its the first time to return before shows informations about the velocity.




\begin{DoxyCodeInclude}
\_update\_vel\_cb(\textcolor{keywordtype}{void} *data, \hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body} *body, \textcolor{keywordtype}{void} *event\_info \_\_UNUSED\_\_)
\{
   Velocity\_Data *velocity\_data = data;
   \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool} first\_call = \hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE};
   \textcolor{keywordtype}{double} vx, vy, ax, ay, vaz, aaz;
   \textcolor{keywordtype}{double} time\_now, delta\_time;
   \textcolor{keywordtype}{char} buff[64];

   \textcolor{keywordflow}{if} (!velocity\_data->last\_time)
     first\_call = \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};

\end{DoxyCodeInclude}


Get the delta time to use it soon to calculate the acceleration on every physics iteration.


\begin{DoxyCodeInclude}
   time\_now = \hyperlink{group__Ecore__Time__Group_ga98f2439b5d3e7114a75bfe2f0c22c4e9}{ecore\_time\_get}();
   delta\_time = time\_now - velocity\_data->last\_time;
   velocity\_data->last\_time = time\_now;

\end{DoxyCodeInclude}


Note in this part we get the angular and linear velocities.


\begin{DoxyCodeInclude}
   \hyperlink{group__EPhysics__Body_ga4aa2211390aefa191a13639a672f21e8}{ephysics\_body\_angular\_velocity\_get}(body, NULL, NULL, &vaz);
   \hyperlink{group__EPhysics__Body_ga45dadf9123da25dd007b6c458f86d589}{ephysics\_body\_linear\_velocity\_get}(body, &vx, &vy, NULL);

\end{DoxyCodeInclude}


We need to handle the velocity using delta time to have the acceleration on every tick. Check if its the first time to return before shows informations about the velocity because we don\textquotesingle{}t have the old aceletations and then the calculation of this informations will be wrong.

Here we calculate the aceletarions using this formula\+:

(velocity -\/ old\+\_\+velocity) / delta\+\_\+time;


\begin{DoxyCodeInclude}
   aaz = (vaz - velocity\_data->old\_vaz) / delta\_time;
   velocity\_data->old\_vaz = vaz;
   ax = (vx - velocity\_data->old\_vx) / delta\_time;
   velocity\_data->old\_vx = vx;
   ay = (vy - velocity\_data->old\_vy) / delta\_time;
   velocity\_data->old\_vy = vy;

   \textcolor{keywordflow}{if} (first\_call) \textcolor{keywordflow}{return};

\end{DoxyCodeInclude}


Turning data into text, to pass it to edje shows on screen.


\begin{DoxyCodeInclude}
   snprintf(buff, \textcolor{keyword}{sizeof}(buff), \textcolor{stringliteral}{"Linear velocity: %.2f, %.2f"}, vx, vy);
   elm\_layout\_text\_set(velocity\_data->base.layout, \textcolor{stringliteral}{"linear\_vel"}, buff);
   snprintf(buff, \textcolor{keyword}{sizeof}(buff), \textcolor{stringliteral}{"Linear acceleration: %.2f, %.2f"}, ax, ay);
   elm\_layout\_text\_set(velocity\_data->base.layout, \textcolor{stringliteral}{"linear\_acc"}, buff);
   snprintf(buff, \textcolor{keyword}{sizeof}(buff), \textcolor{stringliteral}{"Angular velocity: %.2f"}, vaz);
   elm\_layout\_text\_set(velocity\_data->base.layout, \textcolor{stringliteral}{"angular\_vel"}, buff);
   snprintf(buff, \textcolor{keyword}{sizeof}(buff), \textcolor{stringliteral}{"Angular acceleration: %.2f"}, aaz);
   elm\_layout\_text\_set(velocity\_data->base.layout, \textcolor{stringliteral}{"angular\_acc"}, buff);
\}

\end{DoxyCodeInclude}


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