o0ProjectRelease.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. }
  31. //这个函数不建议外部调用
  32. GetDirection(){
  33. var direction = new o0.Vector2(0,0);
  34. var directionDistance = 0.0;
  35. for (var fi = 0;fi < this.frameHit.length - 1;++fi) {
  36. for (var li = fi + 1;li < this.frameHit.length;++li) {
  37. var newDirection = this.frameHit[li].gyr - this.frameHit[fi].gyr;
  38. var newDirectionDistance = newDirection.length;
  39. if (directionDistance < newDirectionDistance) {
  40. directionDistance = newDirectionDistance;
  41. direction = newDirection.multiply(this.frameHit[this.frameHit.length -1].time - this.frameHit[0].time);
  42. }
  43. else {
  44. //cout << "false" << endl;
  45. }
  46. }
  47. }
  48. return direction;
  49. }
  50. // 输入俯视的平面坐标系下的xy轴坐标 的 加速计向量/陀螺仪向量。
  51. // timeGap 代表当前帧读取传感器与上一帧读取传感器 之间的时间差
  52. Update(accX, accY, gyrX,gyrY,timeGap,callback){
  53. var rawAcc = new o0.Vector2(accX,accY);
  54. var rawGyr = new o0.Vector2(gyrX,gyrY);
  55. let lastFrame = this.frame[(this.frameOffset + this.frameLength - 1) % this.frameCapacity];
  56. let last2Frame = this.frame[(this.frameOffset + this.frameLength - 2) % this.frameCapacity];
  57. let last3Frame = this.frame[(this.frameOffset + this.frameLength - 3) % this.frameCapacity];
  58. let last4Frame = this.frame[(this.frameOffset + this.frameLength - 4) % this.frameCapacity];
  59. let last5Frame = this.frame[(this.frameOffset + this.frameLength - 5) % this.frameCapacity];
  60. var newFrame = this.frame[(this.frameOffset + this.frameLength) % this.frameCapacity];
  61. newFrame.timeGap = timeGap;
  62. newFrame.acc = rawAcc.minus(this.stableAcc);
  63. newFrame.gyr = rawGyr.minus(this.stableGyr);
  64. if (this.stableCount < this.stableCountMax){
  65. this.stableCount += 1;
  66. }
  67. this.stableAcc = this.stableAcc.multiply((this.stableCount - 1.0) / this.stableCount).plus(rawAcc.multiply(1/this.stableCount));
  68. this.stableGyr = this.stableGyr.multiply((this.stableCount - 1.0) / this.stableCount).plus(rawGyr.multiply(1/this.stableCount));
  69. //////////////////////////////////////////////////////////////////////////////////
  70. newFrame.accFixed = newFrame.acc.length * 100;
  71. if(newFrame.accFixed < lastFrame.accFixed * 0.85){
  72. newFrame.accFixed = lastFrame.accFixed * 0.85;
  73. }
  74. lastFrame.accFixed = Math.max(lastFrame.accFixed, Math.min(newFrame.accFixed,last2Frame.accFixed), Math.min(newFrame.accFixed,last3Frame.accFixed));
  75. ///////////////////////////////////////////////////////////////////////
  76. //newFrame.pos = lastFrame.pos.plus(lastFrame.acc.plus(newFrame.acc).multiply(timeGap/60)).multiply(Math.max(1-timeGap/200,0));
  77. newFrame.pos = lastFrame.pos.plus(newFrame.acc.multiply(timeGap/30)).multiply(Math.max(1-timeGap/1000,0));
  78. ////////////////////////////////////////////
  79. newFrame.accSlope = Math.max(newFrame.accFixed-lastFrame.accFixed,0);
  80. var lastI = this.frame.length-1;
  81. var t2 = this.frame[lastI-1].timeGap;
  82. var t3 = this.frame[lastI].timeGap + t2;
  83. var t4 = newFrame.timeGap + t3;
  84. newFrame.predict = new o0.Vector2(
  85. 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),
  86. 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));/** */
  87. newFrame.shake = o0.distance2(newFrame.predict,newFrame.pos) * 100;
  88. if(isNaN(newFrame.shake)){
  89. newFrame.shake = 0.0;
  90. }
  91. newFrame.shakeFixed = lastFrame.shakeFixed * 0.85;
  92. if(newFrame.shake > newFrame.shakeFixed){
  93. newFrame.shakeFixed = newFrame.shake;
  94. }/* */
  95. lastFrame.shakeFixed = Math.max(lastFrame.shakeFixed, Math.min(newFrame.shakeFixed,last2Frame.shakeFixed), Math.min(newFrame.shakeFixed,last3Frame.shakeFixed));
  96. ////////////////////////////////////////////////////////////////
  97. newFrame.shakeSlope = Math.max(newFrame.shakeFixed-lastFrame.shakeFixed,0);
  98. ///////////////////////////////////////////////////////////////
  99. var direction = new o0.Vector2(0,0);
  100. if(lastFrame.hit==0
  101. && last2Frame.hit==0
  102. && last3Frame.hit==0
  103. && last4Frame.hit==0
  104. && last5Frame.hit==0
  105. && (newFrame.accSlope >= 15 || lastFrame.accSlope >= 20)
  106. && (newFrame.shakeSlope >= 20 || lastFrame.shakeSlope >= 40)){
  107. newFrame.hit = 1;
  108. if (this.frameHit.length < this.frameHitCapacity) {
  109. direction = this.GetDirection();
  110. }/**/
  111. this.frameHit = [];
  112. var o = new Object();
  113. o.time = 0;
  114. o.gyr = lastFrame.gyr;
  115. this.frameHit.push(o);
  116. }else{
  117. newFrame.hit = 0;
  118. }
  119. if (this.frameHit.length < this.frameHitCapacity) {
  120. var o = new Object();
  121. o.time = this.frameHit[this.frameHit.length - 1].time + newFrame.timeGap;
  122. o.gyr = newFrame.gyr;
  123. if (this.frameHit.length == this.frameHitCapacity) {
  124. direction = this.GetDirection();
  125. }
  126. }
  127. if ((this.frameOffset+=1) >= this.frameCapacity){
  128. this.frameOffset -= this.frameCapacity;
  129. }
  130. if(newFrame.hit != 0){
  131. console.log(newFrame.hit, direction);
  132. }
  133. return (newFrame.hit, direction);
  134. }
  135. test(){
  136. return "123123131";
  137. }
  138. }
  139. };