o0ProjectRelease0.1.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. var direction = undefined;
  108. //15 20 20 40 , 13 17 17 35
  109. let slopeArr = [15, 20, 20, 40];
  110. if (lastFrame.hit == 0 &&
  111. last2Frame.hit == 0 &&
  112. last3Frame.hit == 0 &&
  113. last4Frame.hit == 0 &&
  114. last5Frame.hit == 0 &&
  115. (newFrame.accSlope >= slopeArr[0] || lastFrame.accSlope >= slopeArr[1]) &&
  116. (newFrame.shakeSlope >= slopeArr[2] || lastFrame.shakeSlope >= slopeArr[3])) {
  117. newFrame.hit = 1;
  118. if (this.frameHit.length < this.frameHitCapacity && this.frameHit.length !=
  119. 0) { //判断到第二次hit,但还未输出第一次hit的方向,强制输出方向
  120. direction = this.GetDirection();
  121. } /**/
  122. this.frameHit = [];
  123. /*
  124. var o3 = new Object();
  125. o3.time = 0;
  126. o3.gyr = lastFrame3.gyr;
  127. this.frameHit.push(o3);/* */
  128. var o2 = new Object();
  129. o2.time = last2Frame.timeGap;
  130. o2.gyr = last2Frame.gyr;
  131. this.frameHit.push(o2);
  132. var o = new Object();
  133. o.time = last2Frame.timeGap + lastFrame.timeGap;
  134. o.gyr = lastFrame.gyr;
  135. this.frameHit.push(o);
  136. } else {
  137. newFrame.hit = 0;
  138. }
  139. if (this.frameHit.length < this.frameHitCapacity && this.frameHit.length != 0) {
  140. var o = new Object();
  141. o.time = this.frameHit[this.frameHit.length - 1].time + newFrame.timeGap;
  142. //o.gyr = this.frameHit[this.frameHit.length-1].gyr.plus(newFrame.gyr);
  143. o.gyr = newFrame.gyr;
  144. this.frameHit.push(o);
  145. if (this.frameHit.length == this.frameHitCapacity) { //累计达到设定的延迟帧数,输出方向
  146. direction = this.GetDirection();
  147. }
  148. }
  149. if ((this.frameOffset += 1) >= this.frameCapacity) {
  150. this.frameOffset -= this.frameCapacity;
  151. }
  152. return [newFrame.hit, direction];
  153. }
  154. getTempValue(curDirection) {
  155. let result = Math.atan2(curDirection.y, curDirection.x) * 180 / (Math.PI);
  156. result = Math.round(result);
  157. let curAngle = result > 0 ? result : (360 + result);
  158. let directionPunch = "all",
  159. name = "击中",
  160. ename = "hit";
  161. let positiveMidSlope = [72.5,107.5];//min max
  162. let negativeMidSlope = [252.5,287.5];//min max
  163. if (curAngle < positiveMidSlope[1] && curAngle >= positiveMidSlope[0]) {
  164. directionPunch = "straightPunch";
  165. name = "正向的直拳";
  166. ename = "front-straight";
  167. } else if (curAngle < positiveMidSlope[0] && curAngle >= 0) {
  168. directionPunch = "rightPunch";
  169. name = "正向的右拳";
  170. ename = "front-right";
  171. } else if (curAngle <= 180 && curAngle >= positiveMidSlope[1]) {
  172. directionPunch = "leftPunch";
  173. name = "正向的左拳";
  174. ename = "front-left";
  175. }
  176. //相反方向击打
  177. //正方向
  178. else if (curAngle <= negativeMidSlope[1] && curAngle > negativeMidSlope[0]) {
  179. directionPunch = "straightPunch";
  180. name = "负向的直拳";
  181. ename = "back-straight";
  182. } else if (curAngle <= negativeMidSlope[0] && curAngle > 180) {
  183. directionPunch = "rightPunch";
  184. name = "负向的右拳";
  185. ename = "back-right";
  186. } else if (curAngle <= 360 && curAngle > negativeMidSlope[1]) {
  187. directionPunch = "leftPunch";
  188. name = "负向的左拳";
  189. ename = "back-left";
  190. }
  191. this.quitHitCount++;
  192. let temp = {
  193. type: 'hit',
  194. hit: curDirection.length,
  195. hitCount: this.quitHitCount,
  196. direction: directionPunch,
  197. directionVect: {
  198. 'x': curDirection.x,
  199. 'y': curDirection.y
  200. },
  201. angle: curAngle,
  202. name: name,
  203. ename: ename
  204. }
  205. return temp;
  206. }
  207. }
  208. };