o0ProjectRelease0.1.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import o0 from "./o0.js"
  2. module.exports = {
  3. SandbagAlgorithm: class {
  4. constructor() {
  5. this.stableAcc = new o0.Vector2(0, 0);
  6. this.stableGyr = new o0.Vector2(0, 0);
  7. this.stableCount = 0;
  8. this.stableCountMax = 3000;
  9. this.frameCapacity = 6;
  10. this.frame = [];
  11. this.frameLength = 5;
  12. this.frameOffset = 0;
  13. this.frameHitCapacity = 11;
  14. this.frameHit = []; //打击后的n帧
  15. for (var i = 0; i < this.frameCapacity; ++i) {
  16. var o = new Object();
  17. o.acc = new o0.Vector2(0, 0);
  18. o.gyr = new o0.Vector2(0, 0);
  19. o.timeGap = 20;
  20. o.accFixed = 0;
  21. o.accSlope = 0;
  22. o.pos = new o0.Vector2(0, 0);
  23. o.predict = new o0.Vector2(0, 0);
  24. o.shake = 0;
  25. o.shakeFixed = 0;
  26. o.shakeSlope = 0;
  27. o.hit = 0;
  28. this.frame.push(o);
  29. }
  30. this.quitHitCount = 0;
  31. }
  32. //这个函数不建议外部调用
  33. GetDirection() {
  34. var direction = new o0.Vector2(0, 0);
  35. var directionDistance = 0.0;
  36. for (var fi = 0; fi < this.frameHit.length - 1; ++fi) {
  37. for (var li = fi + 1; li < this.frameHit.length; ++li) {
  38. var newDirection = this.frameHit[li].gyr.minus(this.frameHit[fi].gyr);
  39. var newDirectionDistance = newDirection.length;
  40. if (directionDistance < newDirectionDistance) {
  41. directionDistance = newDirectionDistance;
  42. direction = newDirection.multiply(this.frameHit[this.frameHit.length - 1].time -
  43. this.frameHit[0].time);
  44. } else {
  45. //cout << "false" << endl;
  46. }
  47. }
  48. }
  49. return direction;
  50. }
  51. // 输入俯视的平面坐标系下的xy轴坐标 的 加速计向量/陀螺仪向量。
  52. // timeGap 代表当前帧读取传感器与上一帧读取传感器 之间的时间差
  53. Update(accX, accY, gyrX, gyrY, timeGap) {
  54. var rawAcc = new o0.Vector2(accX, accY);
  55. var rawGyr = new o0.Vector2(gyrX, gyrY);
  56. let lastFrame = this.frame[(this.frameOffset + this.frameLength - 1) % this.frameCapacity];
  57. let last2Frame = this.frame[(this.frameOffset + this.frameLength - 2) % this.frameCapacity];
  58. let last3Frame = this.frame[(this.frameOffset + this.frameLength - 3) % this.frameCapacity];
  59. let last4Frame = this.frame[(this.frameOffset + this.frameLength - 4) % this.frameCapacity];
  60. let last5Frame = this.frame[(this.frameOffset + this.frameLength - 5) % this.frameCapacity];
  61. var newFrame = this.frame[(this.frameOffset + this.frameLength) % this.frameCapacity];
  62. newFrame.timeGap = timeGap;
  63. newFrame.acc = rawAcc.minus(this.stableAcc);
  64. newFrame.gyr = rawGyr.minus(this.stableGyr);
  65. if (this.stableCount < this.stableCountMax) {
  66. this.stableCount += 1;
  67. }
  68. this.stableAcc = this.stableAcc.multiply((this.stableCount - 1.0) / this.stableCount).plus(
  69. rawAcc.multiply(1 / this.stableCount));
  70. this.stableGyr = this.stableGyr.multiply((this.stableCount - 1.0) / this.stableCount).plus(
  71. rawGyr.multiply(1 / this.stableCount));
  72. //////////////////////////////////////////////////////////////////////////////////
  73. newFrame.accFixed = newFrame.acc.length * 100;
  74. if (newFrame.accFixed < lastFrame.accFixed * 0.85) {
  75. newFrame.accFixed = lastFrame.accFixed * 0.85;
  76. }
  77. lastFrame.accFixed = Math.max(lastFrame.accFixed, Math.min(newFrame.accFixed, last2Frame
  78. .accFixed), Math.min(newFrame.accFixed, last3Frame.accFixed));
  79. ///////////////////////////////////////////////////////////////////////
  80. //newFrame.pos = lastFrame.pos.plus(lastFrame.acc.plus(newFrame.acc).multiply(timeGap/60)).multiply(Math.max(1-timeGap/200,0));
  81. newFrame.pos = lastFrame.pos.plus(newFrame.acc.multiply(timeGap / 30)).multiply(Math.max(1 -
  82. timeGap / 1000, 0));
  83. ////////////////////////////////////////////
  84. newFrame.accSlope = Math.max(newFrame.accFixed - lastFrame.accFixed, 0);
  85. var lastI = this.frame.length - 1;
  86. var t2 = this.frame[lastI - 1].timeGap;
  87. var t3 = this.frame[lastI].timeGap + t2;
  88. var t4 = newFrame.timeGap + t3;
  89. newFrame.predict = new o0.Vector2(
  90. new o0.QuadraticEquation(0, this.frame[lastI - 2].pos.x, t2, this.frame[lastI - 1].pos
  91. .x, t3, this.frame[lastI].pos.x).y(t4),
  92. new o0.QuadraticEquation(0, this.frame[lastI - 2].pos.y, t2, this.frame[lastI - 1].pos
  93. .y, t3, this.frame[lastI].pos.y).y(t4)); /** */
  94. newFrame.shake = o0.distance2(newFrame.predict, newFrame.pos) * 100;
  95. if (isNaN(newFrame.shake)) {
  96. newFrame.shake = 0.0;
  97. }
  98. newFrame.shakeFixed = lastFrame.shakeFixed * 0.85;
  99. if (newFrame.shake > newFrame.shakeFixed) {
  100. newFrame.shakeFixed = newFrame.shake;
  101. } /* */
  102. lastFrame.shakeFixed = Math.max(lastFrame.shakeFixed, Math.min(newFrame.shakeFixed, last2Frame
  103. .shakeFixed), Math.min(newFrame.shakeFixed, last3Frame.shakeFixed));
  104. ////////////////////////////////////////////////////////////////
  105. newFrame.shakeSlope = Math.max(newFrame.shakeFixed - lastFrame.shakeFixed, 0);
  106. ///////////////////////////////////////////////////////////////
  107. // (newFrame.accSlope >= 15 || lastFrame.accSlope >= 20) &&
  108. // (newFrame.shakeSlope >= 20 || lastFrame.shakeSlope >= 40)
  109. var direction = undefined;
  110. let _threshold = [15,20,20,40];//判定的阈值
  111. if (lastFrame.hit == 0 &&
  112. last2Frame.hit == 0 &&
  113. last3Frame.hit == 0 &&
  114. last4Frame.hit == 0 &&
  115. last5Frame.hit == 0 &&
  116. (newFrame.accSlope >= _threshold[0] || lastFrame.accSlope >= _threshold[1]) &&
  117. (newFrame.shakeSlope >= _threshold[2] || lastFrame.shakeSlope >= _threshold[3])) {
  118. newFrame.hit = 1;
  119. if (this.frameHit.length < this.frameHitCapacity && this.frameHit.length !=
  120. 0) { //判断到第二次hit,但还未输出第一次hit的方向,强制输出方向
  121. direction = this.GetDirection();
  122. } /**/
  123. this.frameHit = [];
  124. /*
  125. var o3 = new Object();
  126. o3.time = 0;
  127. o3.gyr = lastFrame3.gyr;
  128. this.frameHit.push(o3);/* */
  129. var o2 = new Object();
  130. o2.time = last2Frame.timeGap;
  131. o2.gyr = last2Frame.gyr;
  132. this.frameHit.push(o2);
  133. var o = new Object();
  134. o.time = last2Frame.timeGap + lastFrame.timeGap;
  135. o.gyr = lastFrame.gyr;
  136. this.frameHit.push(o);
  137. } else {
  138. newFrame.hit = 0;
  139. }
  140. if (this.frameHit.length < this.frameHitCapacity && this.frameHit.length != 0) {
  141. var o = new Object();
  142. o.time = this.frameHit[this.frameHit.length - 1].time + newFrame.timeGap;
  143. //o.gyr = this.frameHit[this.frameHit.length-1].gyr.plus(newFrame.gyr);
  144. o.gyr = newFrame.gyr;
  145. this.frameHit.push(o);
  146. if (this.frameHit.length == this.frameHitCapacity) { //累计达到设定的延迟帧数,输出方向
  147. direction = this.GetDirection();
  148. }
  149. }
  150. if ((this.frameOffset += 1) >= this.frameCapacity) {
  151. this.frameOffset -= this.frameCapacity;
  152. }
  153. return [newFrame.hit, direction];
  154. }
  155. getTempValue(curDirection) {
  156. let result = Math.atan2(curDirection.y, curDirection.x) * 180 / (Math.PI);
  157. result = Math.round(result);
  158. let curAngle = result > 0 ? result : (360 + result);
  159. let directionPunch = "all",
  160. name = "击中",
  161. ename = "hit";
  162. //min , max
  163. let pLeftBetween = [105, 180];
  164. let pStraightBetween = [75, 105];
  165. let pRightBetween = [0, 75];
  166. if (curAngle >= pStraightBetween[0] && curAngle < pStraightBetween[1]) {
  167. directionPunch = "straightPunch";
  168. name = "正向的直拳";
  169. ename = "front-straight";
  170. } else if (curAngle >= pRightBetween[0] && curAngle < pRightBetween[1]) {
  171. directionPunch = "rightPunch";
  172. name = "正向的右拳";
  173. ename = "front-right";
  174. } else if (curAngle >= pLeftBetween[0] && curAngle <= pLeftBetween[1]) {
  175. directionPunch = "leftPunch";
  176. name = "正向的左拳";
  177. ename = "front-left";
  178. }
  179. //相反方向击打
  180. //正方向
  181. else {
  182. let mAngle = curAngle - 180;
  183. if (mAngle > pRightBetween[0] && mAngle <= pRightBetween[1]) {
  184. directionPunch = "rightPunch";
  185. name = "负向的右拳";
  186. ename = "back-right";
  187. } else if (mAngle > pStraightBetween[0] && mAngle <= pStraightBetween[1]) {
  188. directionPunch = "straightPunch";
  189. name = "负向的直拳";
  190. ename = "back-straight";
  191. } else if (mAngle > pLeftBetween[0] && mAngle <= pLeftBetween[1]) {
  192. directionPunch = "leftPunch";
  193. name = "负向的左拳";
  194. ename = "back-left";
  195. }
  196. }
  197. this.quitHitCount++;
  198. let temp = {
  199. type: 'hit',
  200. hit: curDirection.length,
  201. hitCount: this.quitHitCount,
  202. direction: directionPunch,
  203. directionVect: {
  204. 'x': curDirection.x,
  205. 'y': curDirection.y
  206. },
  207. angle: curAngle,
  208. name: name,
  209. ename: ename
  210. }
  211. console.log("hit:" + JSON.stringify(temp));
  212. return temp;
  213. }
  214. }
  215. };