o0ProjectRelease0.1.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 - this.frameHit[0].time);
  43. }
  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(rawAcc.multiply(1 / this.stableCount));
  69. this.stableGyr = this.stableGyr.multiply((this.stableCount - 1.0) / this.stableCount).plus(rawGyr.multiply(1 / this.stableCount));
  70. //////////////////////////////////////////////////////////////////////////////////
  71. newFrame.accFixed = newFrame.acc.length * 100;
  72. if (newFrame.accFixed < lastFrame.accFixed * 0.85) {
  73. newFrame.accFixed = lastFrame.accFixed * 0.85;
  74. }
  75. lastFrame.accFixed = Math.max(lastFrame.accFixed, Math.min(newFrame.accFixed, last2Frame.accFixed), Math.min(newFrame.accFixed, last3Frame.accFixed));
  76. ///////////////////////////////////////////////////////////////////////
  77. //newFrame.pos = lastFrame.pos.plus(lastFrame.acc.plus(newFrame.acc).multiply(timeGap/60)).multiply(Math.max(1-timeGap/200,0));
  78. newFrame.pos = lastFrame.pos.plus(newFrame.acc.multiply(timeGap / 30)).multiply(Math.max(1 - timeGap / 1000, 0));
  79. ////////////////////////////////////////////
  80. newFrame.accSlope = Math.max(newFrame.accFixed - lastFrame.accFixed, 0);
  81. var lastI = this.frame.length - 1;
  82. var t2 = this.frame[lastI - 1].timeGap;
  83. var t3 = this.frame[lastI].timeGap + t2;
  84. var t4 = newFrame.timeGap + t3;
  85. newFrame.predict = new o0.Vector2(
  86. new o0.QuadraticEquation(0, this.frame[lastI - 2].pos.x, t2, this.frame[lastI - 1].pos.x, t3, this.frame[lastI].pos.x).y(t4),
  87. new o0.QuadraticEquation(0, this.frame[lastI - 2].pos.y, t2, this.frame[lastI - 1].pos.y, t3, this.frame[lastI].pos.y).y(t4));/** */
  88. newFrame.shake = o0.distance2(newFrame.predict, newFrame.pos) * 100;
  89. if (isNaN(newFrame.shake)) {
  90. newFrame.shake = 0.0;
  91. }
  92. newFrame.shakeFixed = lastFrame.shakeFixed * 0.85;
  93. if (newFrame.shake > newFrame.shakeFixed) {
  94. newFrame.shakeFixed = newFrame.shake;
  95. }/* */
  96. lastFrame.shakeFixed = Math.max(lastFrame.shakeFixed, Math.min(newFrame.shakeFixed, last2Frame.shakeFixed), Math.min(newFrame.shakeFixed, last3Frame.shakeFixed));
  97. ////////////////////////////////////////////////////////////////
  98. newFrame.shakeSlope = Math.max(newFrame.shakeFixed - lastFrame.shakeFixed, 0);
  99. ///////////////////////////////////////////////////////////////
  100. var direction = undefined;
  101. if (lastFrame.hit == 0
  102. && last2Frame.hit == 0
  103. && last3Frame.hit == 0
  104. && last4Frame.hit == 0
  105. && last5Frame.hit == 0
  106. && (newFrame.accSlope >= 15 || lastFrame.accSlope >= 20)
  107. && (newFrame.shakeSlope >= 20 || lastFrame.shakeSlope >= 40)) {
  108. newFrame.hit = 1;
  109. if (this.frameHit.length < this.frameHitCapacity && this.frameHit.length != 0) {//判断到第二次hit,但还未输出第一次hit的方向,强制输出方向
  110. direction = this.GetDirection();
  111. }/**/
  112. this.frameHit = [];
  113. /*
  114. var o3 = new Object();
  115. o3.time = 0;
  116. o3.gyr = lastFrame3.gyr;
  117. this.frameHit.push(o3);/* */
  118. var o2 = new Object();
  119. o2.time = last2Frame.timeGap;
  120. o2.gyr = last2Frame.gyr;
  121. this.frameHit.push(o2);
  122. var o = new Object();
  123. o.time = last2Frame.timeGap + lastFrame.timeGap;
  124. o.gyr = lastFrame.gyr;
  125. this.frameHit.push(o);
  126. } else {
  127. newFrame.hit = 0;
  128. }
  129. if (this.frameHit.length < this.frameHitCapacity && this.frameHit.length != 0) {
  130. var o = new Object();
  131. o.time = this.frameHit[this.frameHit.length - 1].time + newFrame.timeGap;
  132. //o.gyr = this.frameHit[this.frameHit.length-1].gyr.plus(newFrame.gyr);
  133. o.gyr = newFrame.gyr;
  134. this.frameHit.push(o);
  135. if (this.frameHit.length == this.frameHitCapacity) {//累计达到设定的延迟帧数,输出方向
  136. direction = this.GetDirection();
  137. }
  138. }
  139. if ((this.frameOffset += 1) >= this.frameCapacity) {
  140. this.frameOffset -= this.frameCapacity;
  141. }
  142. return [newFrame.hit, direction];
  143. }
  144. getTempValue(curDirection) {
  145. let result = Math.atan2(curDirection.y,curDirection.x) * 180 / (Math.PI);
  146. result = Math.round(result);
  147. let curAngle = result > 0 ? result:(360 + result);
  148. let directionPunch = "all",
  149. name = "击中",
  150. ename = "hit";
  151. if (curAngle < 110 && curAngle >= 70) {
  152. directionPunch = "straightPunch";
  153. name = "正向的直拳";
  154. ename = "front-straight";
  155. } else if (curAngle < 70 && curAngle >= 0) {
  156. directionPunch = "rightPunch";
  157. name = "正向的右拳";
  158. ename = "front-right";
  159. } else if (curAngle >= 110 && curAngle <= 180) {
  160. directionPunch = "leftPunch";
  161. name = "正向的左拳";
  162. ename = "front-left";
  163. }
  164. //相反方向击打
  165. //正方向
  166. else if (curAngle > 180 && curAngle <= 250) {
  167. directionPunch = "rightPunch";
  168. name = "负向的右拳";
  169. ename = "back-right";
  170. }
  171. else if (curAngle > 250 && curAngle <= 290) {
  172. directionPunch = "straightPunch";
  173. name = "负向的直拳";
  174. ename = "back-straight";
  175. } else if (curAngle > 290 && curAngle <= 360) {
  176. directionPunch = "leftPunch";
  177. name = "负向的左拳";
  178. ename = "back-left";
  179. }
  180. this.quitHitCount++;
  181. let temp = {
  182. type: 'hit',
  183. hit: curDirection.length,
  184. hitCount: this.quitHitCount,
  185. direction: directionPunch,
  186. directionVect: {
  187. 'x': curDirection.x,
  188. 'y': curDirection.y
  189. },
  190. angle: curAngle,
  191. name: name,
  192. ename: ename
  193. }
  194. return temp;
  195. }
  196. }
  197. };