\hypertarget{test_velocity_8c-example}{}\section{test\+\_\+velocity.\+c}

\begin{DoxyCodeInclude}
\textcolor{preprocessor}{#ifdef HAVE\_CONFIG\_H}
\textcolor{preprocessor}{# include <config.h>}
\textcolor{preprocessor}{#endif}

\textcolor{preprocessor}{#include "ephysics\_test.h"}

\textcolor{keyword}{typedef} \textcolor{keyword}{struct }\_Velocity\_Data Velocity\_Data;

\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;
\};

\textcolor{keyword}{static} \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
\_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{group__Evas_gad64cde0da38a60e7cb7905b3ab216968}{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{group__Evas_ggad64cde0da38a60e7cb7905b3ab216968ac436b50ad665089f8a24b0fb827cec5f}{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};
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_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};

   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;

   \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);
   vx = (vx > 0 || vx <= -0.01) ? vx : 0;
   vy = (vy > 0 || vy <= -0.01) ? vy : 0;

   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};

   \hyperlink{group__Eina__File__Group_gae8924623ca601364965e3d88fb7f40fa}{DBG}(\textcolor{stringliteral}{"Delta Time: %0.3lf"}, delta\_time);

   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);
   \hyperlink{group__Eina__File__Group_gae8924623ca601364965e3d88fb7f40fa}{DBG}(\textcolor{stringliteral}{"Vx: %0.3lf p/s Ax: %0.3lf p/s/s"}, vx, ax);
   \hyperlink{group__Eina__File__Group_gae8924623ca601364965e3d88fb7f40fa}{DBG}(\textcolor{stringliteral}{"Vy: %0.3lf p/s Ay: %0.3lf p/s/s"}, vy, ay);

   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);
   \hyperlink{group__Eina__File__Group_gae8924623ca601364965e3d88fb7f40fa}{DBG}(\textcolor{stringliteral}{"Va: %0.3lf d/s Aa: %0.3lf d/s/s"}, vaz, aaz);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_world\_populate(Velocity\_Data *velocity\_data)
\{
   \hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object} *sphere, *shadow;
   \hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body} *sphere\_body;

   shadow = elm\_layout\_add(velocity\_data->base.win);
   elm\_layout\_file\_set(
      shadow, PACKAGE\_DATA\_DIR \textcolor{stringliteral}{"/"} EPHYSICS\_TEST\_THEME \textcolor{stringliteral}{".edj"}, \textcolor{stringliteral}{"shadow-ball"});
   \hyperlink{group__Evas__Font__Group_ga02da8091bbac768b8e86c7b74b2a94f9}{evas\_object\_move}(shadow, WIDTH / 3, FLOOR\_Y);
   \hyperlink{group__Evas__Font__Group_ga21e8604c0e4a93e469bff4bd069e82b5}{evas\_object\_resize}(shadow, 54, 3);
   \hyperlink{group__Evas__Object__Group__Basic_ga9cbc13661584e49fb9d9cdab514a1eeb}{evas\_object\_show}(shadow);
   velocity\_data->base.evas\_objs = \hyperlink{group__Eina__List__Group_ga92ec14d1a1040a361c4d8cd0ab7b0ecc}{eina\_list\_append}(
      velocity\_data->base.evas\_objs, shadow);

   sphere = elm\_image\_add(velocity\_data->base.win);
   elm\_image\_file\_set(
      sphere, PACKAGE\_DATA\_DIR \textcolor{stringliteral}{"/"} EPHYSICS\_TEST\_THEME \textcolor{stringliteral}{".edj"}, \textcolor{stringliteral}{"red-ball"});
   \hyperlink{group__Evas__Font__Group_ga02da8091bbac768b8e86c7b74b2a94f9}{evas\_object\_move}(sphere, WIDTH / 3, FLOOR\_Y - 54);
   \hyperlink{group__Evas__Font__Group_ga21e8604c0e4a93e469bff4bd069e82b5}{evas\_object\_resize}(sphere, 54, 54);
   \hyperlink{group__Evas__Object__Group__Basic_ga9cbc13661584e49fb9d9cdab514a1eeb}{evas\_object\_show}(sphere);
   velocity\_data->base.evas\_objs = \hyperlink{group__Eina__List__Group_ga92ec14d1a1040a361c4d8cd0ab7b0ecc}{eina\_list\_append}(
      velocity\_data->base.evas\_objs, sphere);

   sphere\_body = \hyperlink{group__EPhysics__Body_gab26c09b5a98dabfb0323dbdb7f2a64af}{ephysics\_body\_sphere\_add}(velocity\_data->base.world);
   \hyperlink{group__EPhysics__Body_ga39cb9e95bbe1d097926a09365c501db3}{ephysics\_body\_evas\_object\_set}(sphere\_body, sphere, 
      \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE});
   \hyperlink{group__EPhysics__Body_ga731c9a6443f7c4294fce23fc1287b778}{ephysics\_body\_restitution\_set}(sphere\_body, 0.8);
   \hyperlink{group__EPhysics__Body_ga986d45060bf6ba1689a56889a2bcabce}{ephysics\_body\_friction\_set}(sphere\_body, 1);
   \hyperlink{group__EPhysics__Body_gaa65f3bf819cf09ac5da543ee6460a9f3}{ephysics\_body\_sleeping\_threshold\_set}(sphere\_body, 0, 0);
   \hyperlink{group__EPhysics__Body_ga0ecea96f6a5a62f29e2bdeecb86db400}{ephysics\_body\_damping\_set}(sphere\_body, 0.2, 0.2);
   \hyperlink{group__EPhysics__Body_gada176f4748a4c2fc4ba2042021ea7067}{ephysics\_body\_event\_callback\_add}(sphere\_body, 
      \hyperlink{group__EPhysics__Body_gga904d0d1462e71c1f32cb27f540b5277cacb2363cd59bc8642e74ad916c6658e0a}{EPHYSICS\_CALLBACK\_BODY\_UPDATE},
                                    update\_object\_cb, shadow);
   \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);
   \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);
   velocity\_data->base.bodies = \hyperlink{group__Eina__List__Group_ga92ec14d1a1040a361c4d8cd0ab7b0ecc}{eina\_list\_append}(velocity\_data->base.bodies,
                                                 sphere\_body);
   velocity\_data->body = sphere\_body;
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_restart(\textcolor{keywordtype}{void} *data, \hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object} *obj \_\_UNUSED\_\_, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *emission \_\_UNUSED\_\_, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *
      source \_\_UNUSED\_\_)
\{
   Velocity\_Data *velocity\_data = data;

   elm\_object\_event\_callback\_del(velocity\_data->base.win, \_on\_keydown,
                                 velocity\_data->body);

   \hyperlink{group__Eina__File__Group_gae8924623ca601364965e3d88fb7f40fa}{DBG}(\textcolor{stringliteral}{"Restart pressed"});
   test\_clean((Test\_Data *)velocity\_data);
   \_world\_populate(velocity\_data);
   velocity\_data->old\_vx = 0;
   velocity\_data->old\_vy = 0;
   velocity\_data->old\_vaz = 0;
   velocity\_data->last\_time = 0;

   elm\_object\_event\_callback\_add(velocity\_data->base.win, \_on\_keydown,
                                 velocity\_data->body);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_win\_del(\textcolor{keywordtype}{void} *data, \hyperlink{group__Evas__Canvas_ga5ff87cc4ce6bc43e3b640a6d37f73043}{Evas} *e \_\_UNUSED\_\_, \hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object} *obj \_\_UNUSED\_\_, \textcolor{keywordtype}{void} *event\_info 
      \_\_UNUSED\_\_)
\{
   Velocity\_Data *velocity\_data = data;

   test\_clean((Test\_Data *)velocity\_data);
   \hyperlink{group__Evas__Object__Group__Basic_ga7e7a625dab7a3890532aa13b43117076}{evas\_object\_del}(velocity\_data->base.layout);
   \hyperlink{group__EPhysics__World_ga99ebd1425f258f895c0089d6eb13def9}{ephysics\_world\_del}(velocity\_data->base.world);
   free(velocity\_data);
   \hyperlink{group__EPhysics_ga07e695074fb4d5d125b36c3a1d963f49}{ephysics\_shutdown}();
\}

\textcolor{keywordtype}{void}
test\_velocity(\textcolor{keywordtype}{void} *data \_\_UNUSED\_\_, \hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object} *obj \_\_UNUSED\_\_, \textcolor{keywordtype}{void} *event\_info \_\_UNUSED\_\_)
\{
   Velocity\_Data *velocity\_data;
   \hyperlink{group__EPhysics__Body_gaf39ad724f8a2f81f9139c63263900bf1}{EPhysics\_Body} *boundary;
   \hyperlink{group__EPhysics__World_ga83c0d22744748525aa2dac11726c58e6}{EPhysics\_World} *world;

   \textcolor{keywordflow}{if} (!\hyperlink{group__EPhysics_gaca8fc170d73b3f688349cc4dbaf219fd}{ephysics\_init}())
     \textcolor{keywordflow}{return};

   velocity\_data = calloc(1, \textcolor{keyword}{sizeof}(Velocity\_Data));
   \textcolor{keywordflow}{if} (!velocity\_data)
     \{
        \hyperlink{group__Eina__File__Group_ga24e93be9c5baa3aeab7021b64e290672}{ERR}(\textcolor{stringliteral}{"Failed to create test data"});
        \hyperlink{group__EPhysics_ga07e695074fb4d5d125b36c3a1d963f49}{ephysics\_shutdown}();
        \textcolor{keywordflow}{return};
     \}

   test\_win\_add((Test\_Data *)velocity\_data, \textcolor{stringliteral}{"Velocity Getters"}, \hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE});
   \hyperlink{group__Evas__Object__Group__Events_ga23be30ac5cdfb780f1ff82e9108ec26b}{evas\_object\_event\_callback\_add}(velocity\_data->base.win, 
      \hyperlink{group__Evas_ggad64cde0da38a60e7cb7905b3ab216968a4471afbcbb4a754b7b0bbde00fb9a277}{EVAS\_CALLBACK\_DEL},
                                  \_win\_del, velocity\_data);
   elm\_layout\_signal\_callback\_add(velocity\_data->base.layout, \textcolor{stringliteral}{"restart"},
                                  \textcolor{stringliteral}{"test-theme"}, \_restart, velocity\_data);

   elm\_object\_signal\_emit(velocity\_data->base.layout, \textcolor{stringliteral}{"borders,show"},
                          \textcolor{stringliteral}{"ephysics\_test"});
   elm\_object\_signal\_emit(velocity\_data->base.layout, \textcolor{stringliteral}{"arrows,show"},
                          \textcolor{stringliteral}{"ephysics\_test"});
   elm\_object\_signal\_emit(velocity\_data->base.layout, \textcolor{stringliteral}{"velocity,show"},
                          \textcolor{stringliteral}{"ephysics\_test"});

   world = \hyperlink{group__EPhysics__World_ga108e2d7df8c81321b8d5bde8497f0d45}{ephysics\_world\_new}();
   \hyperlink{group__EPhysics__World_gaabf8abaf9c41b0c1e5daa4acf3df0356}{ephysics\_world\_render\_geometry\_set}(world, 50, 40, -50,
                                      WIDTH - 100, FLOOR\_Y - 40, DEPTH);
   velocity\_data->base.world = world;

   boundary = \hyperlink{group__EPhysics__Body_gac5fc5a1817ac81e2f1fcdcd8908614f2}{ephysics\_body\_bottom\_boundary\_add}(velocity\_data->base.world)
      ;
   \hyperlink{group__EPhysics__Body_ga731c9a6443f7c4294fce23fc1287b778}{ephysics\_body\_restitution\_set}(boundary, 0.65);
   \hyperlink{group__EPhysics__Body_ga986d45060bf6ba1689a56889a2bcabce}{ephysics\_body\_friction\_set}(boundary, 4);

   boundary = \hyperlink{group__EPhysics__Body_gab7e598db12a5270311c429f0ee12271c}{ephysics\_body\_right\_boundary\_add}(velocity\_data->base.world);
   \hyperlink{group__EPhysics__Body_ga731c9a6443f7c4294fce23fc1287b778}{ephysics\_body\_restitution\_set}(boundary, 0.4);
   \hyperlink{group__EPhysics__Body_ga986d45060bf6ba1689a56889a2bcabce}{ephysics\_body\_friction\_set}(boundary, 3);

   boundary = \hyperlink{group__EPhysics__Body_ga4ad52252b2537c6f41e14b9135847fe9}{ephysics\_body\_left\_boundary\_add}(velocity\_data->base.world);
   \hyperlink{group__EPhysics__Body_ga731c9a6443f7c4294fce23fc1287b778}{ephysics\_body\_restitution\_set}(boundary, 0.4);
   \hyperlink{group__EPhysics__Body_ga986d45060bf6ba1689a56889a2bcabce}{ephysics\_body\_friction\_set}(boundary, 3);

   \hyperlink{group__EPhysics__Body_ga2a3e40784ef30f15d5d7e9f1f2a814b3}{ephysics\_body\_top\_boundary\_add}(velocity\_data->base.world);

   \_world\_populate(velocity\_data);
   elm\_object\_event\_callback\_add(velocity\_data->base.win, \_on\_keydown,
                                 velocity\_data->body);
\}
\end{DoxyCodeInclude}
 