RunGame.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "RunGame.h"
  2. #include "pub.h"
  3. void RunGame::Process(int* right_pos, int* right_att, int right_zupt, int* left_pos, int* left_att, int left_zupt, int jump, int down, int girl_shoes)
  4. {
  5. /*
  6. 跑酷游戏处理接口,
  7. 暂时在同一个函数里分开处理左右脚的数据,往后再优化
  8. */
  9. if (left_zupt)
  10. {
  11. memcpy(left_last_pos, left_pos, 3 * sizeof(int));
  12. }
  13. if (right_zupt)
  14. {
  15. memcpy(right_last_pos, right_pos, 3 * sizeof(int));
  16. }
  17. int left_pos_tmp[3];
  18. int right_pos_tmp[3];
  19. for (int i = 0; i < 3; i++)
  20. {
  21. left_pos_tmp[i] = left_pos[i] - left_last_pos[i];
  22. right_pos_tmp[i] = right_pos[i] - right_last_pos[i];
  23. }
  24. /*
  25. if (left_zupt == 0)
  26. {
  27. std::cout << "rungame leftpos: " << left_pos_tmp[0] << " " << left_pos_tmp[1] << " " << left_pos_tmp[2] << std::endl;
  28. }
  29. if (right_zupt == 0)
  30. {
  31. std::cout << "rungame rightpos: " << right_pos_tmp[0] << " " << right_pos_tmp[1] << " " << right_pos_tmp[2] << std::endl;
  32. }
  33. */
  34. setResultConLeft(left_zupt);
  35. result[0] = getResultLeft(left_pos_tmp, girl_shoes);
  36. setResultConRight(right_zupt);
  37. result[1] = getResultRight(right_pos_tmp, girl_shoes);
  38. result[2] = getResultJump(jump);
  39. result[3] = getResultDown(down);
  40. }
  41. void RunGame::getResult(int* dec)
  42. {
  43. memcpy(dec, result, 4 * sizeof(int));
  44. }
  45. void RunGame::setResultConLeft(int zupt)
  46. {
  47. if (zupt == 1)
  48. {
  49. left_has_result = 1;
  50. left_pos_offset_min = 0;
  51. }
  52. }
  53. void RunGame::setResultConRight(int zupt)
  54. {
  55. if (zupt == 1)
  56. {
  57. right_has_result = 1;
  58. right_pos_offset_min = 0;
  59. }
  60. }
  61. int RunGame::getResultLeft(int *pos, int girl_shoes)
  62. {
  63. int isLeft = -1;
  64. int distance_threshold = 20;
  65. if (girl_shoes)
  66. distance_threshold = 12;
  67. if (pos[0] > left_pos_offset_min)
  68. {
  69. left_pos_offset_min = pos[0];
  70. }
  71. if (left_has_result == 1 && pos[0] - left_pos_offset_min < -distance_threshold)
  72. {
  73. printf("this motion is Left\n");
  74. isLeft = MOTION_LEFT;
  75. left_has_result = 0;
  76. }
  77. return isLeft;
  78. }
  79. int RunGame::getResultRight(int *pos, int girl_shoes)
  80. {
  81. int isRight = -1;
  82. int distance_threshold = 20;
  83. if (girl_shoes)
  84. distance_threshold = 12;
  85. if (pos[0] < right_pos_offset_min)
  86. {
  87. right_pos_offset_min = pos[0];
  88. }
  89. if (right_has_result == 1 && pos[0] - right_pos_offset_min > distance_threshold)
  90. {
  91. printf("this motion is Right\n");
  92. isRight = MOTION_RIGHT;
  93. right_has_result = 0;
  94. }
  95. return isRight;
  96. }
  97. int RunGame::getResultLeftRight(int* pos, int devNum)
  98. {
  99. if (devNum == LEFT_FOOT && pos[1] < 0)
  100. {
  101. return MOTION_LEFT;
  102. }
  103. if (devNum == RIGHT_FOOT && pos[1] > 0)
  104. {
  105. return MOTION_RIGHT;
  106. }
  107. return -1;
  108. }
  109. int RunGame::getResultJump(int jump)
  110. {
  111. int isJump = -1;
  112. if (last_jump == 0 && jump == 1)
  113. {
  114. printf("this motion is Jump\n");
  115. isJump = MOTION_JUMP;
  116. }
  117. last_jump = jump;
  118. return isJump;
  119. }
  120. int RunGame::getResultDown(int down)
  121. {
  122. int isDown = -1;
  123. if (last_down == 0 && down == 1)
  124. {
  125. printf("this motion is Down\n");
  126. isDown = MOTION_DOWN;
  127. }
  128. last_down = down;
  129. return isDown;
  130. }
  131. RunGame::RunGame()
  132. {
  133. last_down = 0;
  134. last_jump = 0;
  135. left_has_result = 0;
  136. right_has_result = 0;
  137. left_zupt = 0;
  138. right_zupt = 0;
  139. left_pos_offset_min = 0;
  140. right_pos_offset_min = 0;
  141. memset(result, 0, 4 * sizeof(int));
  142. memset(left_last_pos, 0, 3 * sizeof(int));
  143. memset(right_last_pos, 0, 3 * sizeof(int));
  144. }