DanceGame.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "DanceGame.h"
  2. /*
  3. * 初始化DanceGame对象,分左右脚来判断
  4. */
  5. DanceGame::DanceGame()
  6. {
  7. leftFoot = DanceFoot(LEFT_FOOT);
  8. rightFoot = DanceFoot(RIGHT_FOOT);
  9. memset(result, -1, 4 * sizeof(int));
  10. }
  11. /*
  12. * DanceGame主接口,承接Game类的主要调用
  13. */
  14. void DanceGame::Process(int* right_pos, int* right_att, int right_zupt, int* left_pos, int* left_att, int left_zupt, int jump, int down, int rssi)
  15. {
  16. static int left_zupt_count = 0;
  17. static int right_zupt_count = 0;
  18. static float leftRotate[4] = { 1.0, 0.0, 0.0, 1.0 };
  19. static float rightRotate[4] = { 1.0, 0.0, 0.0, 1.0 };
  20. if (left_zupt && rssi < DANCEGAME_MIN_RSSI)
  21. {
  22. left_zupt_count++;
  23. }
  24. else
  25. {
  26. left_zupt_count = 0;
  27. }
  28. if (right_zupt && rssi < DANCEGAME_MIN_RSSI)
  29. {
  30. //rotateTrajRight.ResetHeading(right_att[0]);
  31. right_zupt_count++;
  32. }
  33. else
  34. {
  35. right_zupt_count = 0;
  36. }
  37. if (rssi > DANCEGAME_MIN_RSSI)
  38. {
  39. left_zupt_count = 0;
  40. right_zupt_count = 0;
  41. }
  42. if (left_zupt_count > 50 && right_zupt_count > 50)
  43. {
  44. float leftTheta = float(left_att[0]) * 0.0001f;
  45. float rightTheta = float(right_att[0]) * 0.0001f;
  46. leftRotate[0] = cos(leftTheta); leftRotate[1] = sin(leftTheta);
  47. leftRotate[2] = -sin(leftTheta); leftRotate[3] = cos(leftTheta);
  48. rightRotate[0] = cos(rightTheta); rightRotate[1] = sin(rightTheta);
  49. rightRotate[2] = -sin(rightTheta); rightRotate[3] = cos(rightTheta);
  50. }
  51. float left_pos_g[3];
  52. float pos_tmp[2];
  53. float right_pos_g[3];
  54. pos_tmp[0] = leftRotate[0] * left_pos[0] * 0.01 + leftRotate[1] * left_pos[1] * 0.01;
  55. pos_tmp[1] = leftRotate[2] * left_pos[0] * 0.01 + leftRotate[3] * left_pos[1] * 0.01;
  56. left_pos_g[0] = 0.991444861 * pos_tmp[0] - 0.13052619 * pos_tmp[1];
  57. left_pos_g[1] = 0.13052619 * pos_tmp[0] + 0.991444861 * pos_tmp[1];
  58. left_pos_g[2] = left_pos[2] * 0.01;
  59. pos_tmp[0] = rightRotate[0] * right_pos[0] * 0.01 + rightRotate[1] * right_pos[1] * 0.01;
  60. pos_tmp[1] = rightRotate[2] * right_pos[0] * 0.01 + rightRotate[3] * right_pos[1] * 0.01;
  61. right_pos_g[0] = 0.991444861 * pos_tmp[0] + 0.13052619 * pos_tmp[1];
  62. right_pos_g[1] = -0.13052619 * pos_tmp[0] + 0.991444861 * pos_tmp[1];
  63. right_pos_g[2] = right_pos[2] * 0.01;
  64. result[0] = leftFoot.calGlobalPos(left_pos_g, rssi, left_zupt);
  65. result[1] = rightFoot.calGlobalPos(right_pos_g, rssi, right_zupt);
  66. }
  67. /*
  68. * 获取跳舞游戏结果
  69. */
  70. void DanceGame::getResult(int *matrix)
  71. {
  72. memcpy(matrix, result, 4 * sizeof(int));
  73. }
  74. float DanceGame::getGamePos(int left_or_right, int index)
  75. {
  76. if (index < 0 || index > 2)
  77. return -1;
  78. if (left_or_right == LEFT_FOOT)
  79. return leftFoot.getGamePos(index);
  80. else
  81. return rightFoot.getGamePos(index);
  82. }