RunGame.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. setResultConLeft(left_zupt);
  34. result[0] = getResultLeft(left_pos_tmp, girl_shoes);
  35. setResultConRight(right_zupt);
  36. result[1] = getResultRight(right_pos_tmp, girl_shoes);
  37. result[2] = getResultJump(jump);
  38. result[3] = getResultDown(down);
  39. }
  40. void RunGame::getResult(int* dec)
  41. {
  42. memcpy(dec, result, 4 * sizeof(int));
  43. }
  44. void RunGame::setResultConLeft(int zupt)
  45. {
  46. if (zupt == 1)
  47. {
  48. left_has_result = 1;
  49. left_pos_offset_min = 0;
  50. }
  51. }
  52. void RunGame::setResultConRight(int zupt)
  53. {
  54. if (zupt == 1)
  55. {
  56. right_has_result = 1;
  57. right_pos_offset_min = 0;
  58. }
  59. }
  60. int RunGame::getResultLeft(int *pos, int girl_shoes)
  61. {
  62. int isLeft = -1;
  63. int distance_threshold = 20;
  64. if (girl_shoes)
  65. distance_threshold = 12;
  66. if (pos[0] > left_pos_offset_min)
  67. {
  68. left_pos_offset_min = pos[0];
  69. }
  70. if (left_has_result == 1 && pos[0] - left_pos_offset_min < -distance_threshold)
  71. {
  72. printf("this motion is Left\n");
  73. isLeft = MOTION_LEFT;
  74. left_has_result = 0;
  75. }
  76. return isLeft;
  77. }
  78. int RunGame::getResultRight(int *pos, int girl_shoes)
  79. {
  80. int isRight = -1;
  81. int distance_threshold = 20;
  82. if (girl_shoes)
  83. distance_threshold = 12;
  84. if (pos[0] < right_pos_offset_min)
  85. {
  86. right_pos_offset_min = pos[0];
  87. }
  88. if (right_has_result == 1 && pos[0] - right_pos_offset_min > distance_threshold)
  89. {
  90. printf("this motion is Right\n");
  91. isRight = MOTION_RIGHT;
  92. right_has_result = 0;
  93. }
  94. return isRight;
  95. }
  96. int RunGame::getResultLeftRight(int* pos, int devNum)
  97. {
  98. if (devNum == LEFT_FOOT && pos[1] < 0)
  99. {
  100. return MOTION_LEFT;
  101. }
  102. if (devNum == RIGHT_FOOT && pos[1] > 0)
  103. {
  104. return MOTION_RIGHT;
  105. }
  106. return -1;
  107. }
  108. int RunGame::getResultJump(int jump)
  109. {
  110. int isJump = -1;
  111. last_jump_time++;
  112. if (last_jump == 0 && jump == 1)
  113. {
  114. printf("this motion is Jump\n");
  115. isJump = MOTION_JUMP;
  116. last_jump_time = 0;
  117. }
  118. last_jump = jump;
  119. return isJump;
  120. }
  121. int RunGame::getResultDown(int down)
  122. {
  123. int isDown = -1;
  124. if (last_down == 0 && down == 1 && last_jump_time > 10)
  125. {
  126. printf("this motion is Down\n");
  127. isDown = MOTION_DOWN;
  128. }
  129. last_down = down;
  130. return isDown;
  131. }
  132. RunGame::RunGame()
  133. {
  134. last_down = 0;
  135. last_jump = 0;
  136. last_jump_time = 0;
  137. left_has_result = 0;
  138. right_has_result = 0;
  139. left_zupt = 0;
  140. right_zupt = 0;
  141. left_pos_offset_min = 0;
  142. right_pos_offset_min = 0;
  143. memset(result, 0, 4 * sizeof(int));
  144. memset(left_last_pos, 0, 3 * sizeof(int));
  145. memset(right_last_pos, 0, 3 * sizeof(int));
  146. }