The purpose of this example is to demonstrate the EPhysics 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 EPhysics\+\_\+\+Body Linear and Angular velocity and acceleration.

For this example we\textquotesingle{}ll have an EPhysics\+\_\+\+World and one basic EPhysics\+\_\+\+Body, we\textquotesingle{}ll apply impulses that follows user events, it were already covered in \mbox{\hyperlink{tutorial_ephysics_bouncing_ball}{EPhysics -\/ Bouncing Ball}}\hypertarget{tutorial_ephysics_velocity_add-velstruct}{}\doxysection{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}{0}
\DoxyCodeLine{\textcolor{keyword}{struct\ }\_Velocity\_Data\ \{}
\DoxyCodeLine{\ \ \ \ \ Test\_Data\ base;}
\DoxyCodeLine{\ \ \ \ \ \mbox{\hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body}}\ *body;}
\DoxyCodeLine{\ \ \ \ \ \textcolor{keywordtype}{double}\ old\_vx;}
\DoxyCodeLine{\ \ \ \ \ \textcolor{keywordtype}{double}\ old\_vy;}
\DoxyCodeLine{\ \ \ \ \ \textcolor{keywordtype}{double}\ old\_vaz;}
\DoxyCodeLine{\ \ \ \ \ \textcolor{keywordtype}{double}\ last\_time;}
\DoxyCodeLine{\};}

\end{DoxyCodeInclude}
 \hypertarget{tutorial_ephysics_velocity_autotoc_md22}{}\doxysection{Adding the Callbacks}\label{tutorial_ephysics_velocity_autotoc_md22}
Calling \mbox{\hyperlink{group__EPhysics__Body_gada176f4748a4c2fc4ba2042021ea7067}{ephysics\+\_\+body\+\_\+event\+\_\+callback\+\_\+add()}} will register a callback to a type of physics body event.

\mbox{\hyperlink{group__EPhysics__Body_gga904d0d1462e71c1f32cb27f540b5277cacb2363cd59bc8642e74ad916c6658e0a}{EPHYSICS\+\_\+\+CALLBACK\+\_\+\+BODY\+\_\+\+UPDATE}} \+: called after every physics iteration. In other words, will be called after each world tick.


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

\end{DoxyCodeInclude}
 \mbox{\hyperlink{group__EPhysics__Body_gga904d0d1462e71c1f32cb27f540b5277cae88f6dfe5c24a67f5f074fb818edfa72}{EPHYSICS\+\_\+\+CALLBACK\+\_\+\+BODY\+\_\+\+STOPPED}} \+: called when a body is found to be stopped. In other words, when the body is not moving anymore.


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

\end{DoxyCodeInclude}
 See \mbox{\hyperlink{group__EPhysics__Body_ga904d0d1462e71c1f32cb27f540b5277c}{\+\_\+\+EPhysics\+\_\+\+Callback\+\_\+\+Body\+\_\+\+Type}} for more event types.\hypertarget{tutorial_ephysics_velocity_add-velcb}{}\doxysection{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}{0}
\DoxyCodeLine{\_update\_vel\_cb(\textcolor{keywordtype}{void}\ *data,\ \mbox{\hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body}}\ *body,\ \textcolor{keywordtype}{void}\ *event\_info\ \_\_UNUSED\_\_)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ Velocity\_Data\ *velocity\_data\ =\ data;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}\ first\_call\ =\ \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{double}\ vx,\ vy,\ ax,\ ay,\ vaz,\ aaz;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{double}\ time\_now,\ delta\_time;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ buff[64];}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (!velocity\_data-\/>last\_time)}
\DoxyCodeLine{\ \ \ \ \ first\_call\ =\ \mbox{\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}{0}
\DoxyCodeLine{\ \ \ time\_now\ =\ \mbox{\hyperlink{group__Ecore__Time__Group_ga98f2439b5d3e7114a75bfe2f0c22c4e9}{ecore\_time\_get}}();}
\DoxyCodeLine{\ \ \ delta\_time\ =\ time\_now\ -\/\ velocity\_data-\/>last\_time;}
\DoxyCodeLine{\ \ \ velocity\_data-\/>last\_time\ =\ time\_now;}

\end{DoxyCodeInclude}
 Note in this part we get the angular and linear velocities.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__EPhysics__Body_ga4aa2211390aefa191a13639a672f21e8}{ephysics\_body\_angular\_velocity\_get}}(body,\ NULL,\ NULL,\ \&vaz);}
\DoxyCodeLine{\ \ \ \mbox{\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}{0}
\DoxyCodeLine{\ \ \ aaz\ =\ (vaz\ -\/\ velocity\_data-\/>old\_vaz)\ /\ delta\_time;}
\DoxyCodeLine{\ \ \ velocity\_data-\/>old\_vaz\ =\ vaz;}
\DoxyCodeLine{\ \ \ ax\ =\ (vx\ -\/\ velocity\_data-\/>old\_vx)\ /\ delta\_time;}
\DoxyCodeLine{\ \ \ velocity\_data-\/>old\_vx\ =\ vx;}
\DoxyCodeLine{\ \ \ ay\ =\ (vy\ -\/\ velocity\_data-\/>old\_vy)\ /\ delta\_time;}
\DoxyCodeLine{\ \ \ velocity\_data-\/>old\_vy\ =\ vy;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (first\_call)\ \textcolor{keywordflow}{return};}

\end{DoxyCodeInclude}
 Turning data into text, to pass it to edje shows on screen.


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

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