\chapter{EPhysics -\/ Rotating Forever}
\hypertarget{tutorial_ephysics_rotating_forever}{}\label{tutorial_ephysics_rotating_forever}\index{EPhysics -\/ Rotating Forever@{EPhysics -\/ Rotating Forever}}
The purpose of this example is to demonstrate the EPhysics Rotate usage -\/ The code applies different ways to rotate an EPhysics\+\_\+\+Body, such as torque, torque impulse and rotation set.

For this example we\textquotesingle{}ll have an EPhysics\+\_\+\+World with gravity setted to zero, and four basic EPhysics\+\_\+\+Bodys.

The basic concepts like -\/ defining an EPhysics\+\_\+\+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 \doxysectlink{tutorial_ephysics_bouncing_ball}{EPhysics -\/ Bouncing Ball}{0}\hypertarget{tutorial_ephysics_rotating_forever_add-rotate}{}\doxysection{\texorpdfstring{Rotating}{Rotating}}\label{tutorial_ephysics_rotating_forever_add-rotate}


For the first body we\textquotesingle{}ll apply a torque impulse to make it rotate around Z axis (rotate on x-\/y plane). Will make the body rolls on clockwise rotation, if the value is negative, the impulse will be on counter clockwise.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__EPhysics__Body_ga6c6d3c8bfc6bb5600947b66b519d2d98}{ephysics\_body\_torque\_impulse\_apply}}(body,\ 0,\ 0,\ 1);}

\end{DoxyCodeInclude}
 For the second body we\textquotesingle{}ll use an offset to apply the force, the three last parameters are responsible to set a relative position to apply the force.\+In other words, the force applied with an offset will make the body rotates and move around the other cubes.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__EPhysics__Body_gadc5405c4d07742e9aff7a0a564aa63e2}{ephysics\_body\_impulse\_apply}}(body,\ 30,\ 0,\ 0,\ 0,\ -\/10,\ 0);}

\end{DoxyCodeInclude}
 For the third body we\textquotesingle{}ll use a timer to rotate the body and a callback to delete it.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ timer\ =\ \mbox{\hyperlink{group__Ecore__Timer__Group_ga926423e464d7ef71105dd822d90e1187}{ecore\_timer\_add}}(1,\ \_rotate\_cb,\ body);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__EPhysics__Body_gada176f4748a4c2fc4ba2042021ea7067}{ephysics\_body\_event\_callback\_add}}(body,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__EPhysics__Body_gga904d0d1462e71c1f32cb27f540b5277cab8f46f167ee3872ea35fe645bf14a5d2}{EPHYSICS\_CALLBACK\_BODY\_DEL}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \_del\_cb,\ timer);}

\end{DoxyCodeInclude}
  
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\_del\_cb(\textcolor{keywordtype}{void}\ *data,\ EPhysics\_Body\ *body\ \_\_UNUSED\_\_,\ \textcolor{keywordtype}{void}\ *event\_info\ \_\_UNUSED\_\_)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Timer__Group_ga7c118b6a6d232b63a6500e599d2a1b47}{ecore\_timer\_del}}(data);}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 In the function we\textquotesingle{}ll get the body rotation on z axis in degrees and handle it increasing 5 degrees on its position on z axis on each tick of the timer.

 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\_rotate\_cb(\textcolor{keywordtype}{void}\ *data)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__EPhysics__Quaternion}{EPhysics\_Quaternion}}\ *quat\_prev,\ quat\_delta,\ quat;}
\DoxyCodeLine{\ \ \ EPhysics\_Body\ *body\ =\ data;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ quat\_prev\ =\ \mbox{\hyperlink{group__EPhysics__Body_ga39ae7b220642d8c30cab9c8b313cdde3}{ephysics\_body\_rotation\_get}}(body,\ NULL);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__EPhysics__Quaternion_gae05800bc35b8f0d161673f167e632524}{ephysics\_quaternion\_set}}(\&quat\_delta,\ 0,\ 0,\ -\/0.15,\ 0.98);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__EPhysics__Quaternion_gae72bd8e462318feae3636dd5f405065f}{ephysics\_quaternion\_normalize}}(\&quat\_delta);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__EPhysics__Body_ga11a611cba12f9066a272e5cb6d1b371e}{ephysics\_body\_rotation\_set}}(}
\DoxyCodeLine{\ \ \ \ \ \ body,\ \mbox{\hyperlink{group__EPhysics__Quaternion_ga959d29aa69ad3e9d99fd49784d88e887}{ephysics\_quaternion\_multiply}}(\&quat\_delta,\ quat\_prev,\ \&quat));}
\DoxyCodeLine{\ \ \ free(quat\_prev);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 For the forth body we\textquotesingle{}ll use 2 timers, but before that, we\textquotesingle{}ll apply an initial torque, changing the body angular acceleration and a callback to delete the timers we\textquotesingle{}ll add.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__EPhysics__Body_ga336a585598ed882a9575ed72a9f748e2}{ephysics\_body\_torque\_apply}}(body,\ 0,\ 0,\ 2);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__EPhysics__Body_gada176f4748a4c2fc4ba2042021ea7067}{ephysics\_body\_event\_callback\_add}}(body,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__EPhysics__Body_gga904d0d1462e71c1f32cb27f540b5277cab8f46f167ee3872ea35fe645bf14a5d2}{EPHYSICS\_CALLBACK\_BODY\_DEL}},}

\end{DoxyCodeInclude}
 Just the callback function to delete the timers.

 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\_del\_torque\_cb(\textcolor{keywordtype}{void}\ *data\ \_\_UNUSED\_\_,\ Evas\ *e\ \_\_UNUSED\_\_,\ Evas\_Object\ *obj,\ \textcolor{keywordtype}{void}\ *event\_info\ \_\_UNUSED\_\_)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Ecore__Timer__Group_gaf96ffb609956658ec038e54bd39f46f0}{Ecore\_Timer}}\ *timer;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ timer\ =\ evas\_object\_data\_get(obj,\ \textcolor{stringliteral}{"{}increase\_timer"{}});}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (timer)}
\DoxyCodeLine{\ \ \ \ \ \mbox{\hyperlink{group__Ecore__Timer__Group_ga7c118b6a6d232b63a6500e599d2a1b47}{ecore\_timer\_del}}(timer);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ timer\ =\ evas\_object\_data\_get(obj,\ \textcolor{stringliteral}{"{}stop\_timer"{}});}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (timer)}
\DoxyCodeLine{\ \ \ \ \ \mbox{\hyperlink{group__Ecore__Timer__Group_ga7c118b6a6d232b63a6500e599d2a1b47}{ecore\_timer\_del}}(timer);}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 As we commented we\textquotesingle{}ll use 2 timers, one to increase the torque and another to stop the torque, cleaning the forces related to the body.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ timer\ =\ \mbox{\hyperlink{group__Ecore__Timer__Group_ga926423e464d7ef71105dd822d90e1187}{ecore\_timer\_add}}(3,\ \_increase\_torque\_cb,\ body);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group__Extras_ga3061f8abe8f1a1f657d622ae0409d8e3}{evas\_object\_data\_set}}(cube,\ \textcolor{stringliteral}{"{}increase\_timer"{}},\ timer);}
\DoxyCodeLine{\ \ \ timer\ =\ \mbox{\hyperlink{group__Ecore__Timer__Group_ga926423e464d7ef71105dd822d90e1187}{ecore\_timer\_add}}(5,\ \_stop\_torque\_cb,\ body);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group__Extras_ga3061f8abe8f1a1f657d622ae0409d8e3}{evas\_object\_data\_set}}(cube,\ \textcolor{stringliteral}{"{}stop\_timer"{}},\ timer);}

\end{DoxyCodeInclude}
 In the increase function we\textquotesingle{}ll apply a torque over the body, changing its angular acceleration, it will leads to a change on angular velocity over time. We\textquotesingle{}re using a timer to increase the angular acceleration on each tick of the timer.

 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\_increase\_torque\_cb(\textcolor{keywordtype}{void}\ *data)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ EPhysics\_Body\ *body\ =\ data;}
\DoxyCodeLine{\ \ \ Evas\_Object\ *obj;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__EPhysics__Body_ga336a585598ed882a9575ed72a9f748e2}{ephysics\_body\_torque\_apply}}(body,\ 0,\ 0,\ 2);}
\DoxyCodeLine{\ \ \ obj\ =\ \mbox{\hyperlink{group__EPhysics__Body_gade11f9cb1bdd26f393adbbe0b4a594be}{ephysics\_body\_evas\_object\_get}}(body);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group__Extras_ga3061f8abe8f1a1f657d622ae0409d8e3}{evas\_object\_data\_set}}(obj,\ \textcolor{stringliteral}{"{}increase\_timer"{}},\ NULL);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 In the stop function we\textquotesingle{}ll clear all the forces applied to the body, setting its linear and angular acceleration to zero. We\textquotesingle{}re using this timer to "{}control"{} the body velocity, since we are increasing it by another timer. Note that we set the acceleration to zero not the velocity.


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\_stop\_torque\_cb(\textcolor{keywordtype}{void}\ *data)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ EPhysics\_Body\ *body\ =\ data;}
\DoxyCodeLine{\ \ \ Evas\_Object\ *obj;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__EPhysics__Body_ga8a84144392d9f3c7dbecdec6a1199a49}{ephysics\_body\_forces\_clear}}(body);}
\DoxyCodeLine{\ \ \ obj\ =\ \mbox{\hyperlink{group__EPhysics__Body_gade11f9cb1bdd26f393adbbe0b4a594be}{ephysics\_body\_evas\_object\_get}}(body);}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Evas__Object__Group__Extras_ga3061f8abe8f1a1f657d622ae0409d8e3}{evas\_object\_data\_set}}(obj,\ \textcolor{stringliteral}{"{}stop\_timer"{}},\ NULL);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 Here we finish the example. The full source code can be found at \doxysectlink{test_rotating_forever_c}{test\+\_\+rotating\+\_\+forever.\+c}{0}. 