o0Project.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. var o0 = require('o0');
  2. module.exports = {
  3. Filter: class{
  4. constructor() {
  5. this.stableAcceleration = null;
  6. this.maxrecordCount = 999999;
  7. this.frame = [];
  8. this.frameHit = [];//打击后的n帧
  9. for(var i = 0;i<200;++i){
  10. var o = new Object();
  11. o.time = new Date().getTime();
  12. o.timeGap = 20;
  13. o.acc = new o0.Vector2(0,0);
  14. o.gyr = new o0.Vector2(0,0);
  15. o.accFixed = 0;
  16. o.accSlope = 0;
  17. o.pos = new o0.Vector2(0,0);
  18. o.predict = new o0.Vector2(0,0);
  19. o.shake = 0;
  20. o.shakeFixed = 1;
  21. o.shakeSlope = 0;
  22. o.reliable = 1;
  23. o.speed = new o0.Vector2(0,0);
  24. o.hit = 0;
  25. this.frame.push(o);
  26. //this.frameHit.push(o);
  27. }
  28. this.frameSwing = this.frame[0];
  29. //this.frame = 0;
  30. this.force = new o0.Vector2(0,0);
  31. this.forceChanged = 0;
  32. this.momentum = 999999;
  33. this.momentunTime = 0;
  34. this.momentunSeek = 0.1;
  35. this.momentunPunch = false;
  36. this.punchCount = 0;
  37. this.leftRight = false;
  38. this.forceRecord = [];
  39. for(var i = 0;i<1;++i){
  40. this.forceRecord.push(new o0.Vector2(0,0));
  41. }/** */
  42. this.angle = 0;
  43. }
  44. Update(vector3,timeGap,gyr){
  45. if(this.stableAcceleration == null){
  46. this.stableAcceleration = vector3;
  47. this.recordCount = 1;
  48. }else{
  49. this.recordCount += 1;
  50. if(this.recordCount > this.maxrecordCount){
  51. this.recordCount = this.maxrecordCount;
  52. }
  53. let recordCount = this.recordCount;
  54. this.stableAcceleration = this.stableAcceleration.multiply((recordCount-1)/recordCount).plus(vector3.multiply(1/recordCount));
  55. }
  56. //++this.frame;
  57. var newForce = vector3.minus(this.stableAcceleration);
  58. var newFrame = new Object();
  59. newFrame.time = new Date().getTime();
  60. newFrame.timeGap = timeGap;
  61. newFrame.acc = new o0.Vector2(newForce.x,newForce.z);
  62. newFrame.gyr = new o0.Vector2(gyr.z,-gyr.x);
  63. let lastFrame = this.frame[this.frame.length-1];
  64. let last2Frame = this.frame[this.frame.length-2];
  65. let last3Frame = this.frame[this.frame.length-3];
  66. let last4Frame = this.frame[this.frame.length-4];
  67. let last5Frame = this.frame[this.frame.length-5];
  68. //////////////////////////////////////////////////////////////////////
  69. newFrame.accFixed = newFrame.acc.length * 100;
  70. if(newFrame.accFixed < lastFrame.accFixed * 0.85){
  71. newFrame.accFixed = lastFrame.accFixed * 0.85;
  72. }
  73. lastFrame.accFixed = Math.max(lastFrame.accFixed, Math.min(newFrame.accFixed,last2Frame.accFixed), Math.min(newFrame.accFixed,last3Frame.accFixed));
  74. ///////////////////////////////////////////////////////////////////////
  75. //newFrame.pos = lastFrame.pos.plus(lastFrame.acc.plus(newFrame.acc).multiply(timeGap/60)).multiply(Math.max(1-timeGap/200,0));
  76. newFrame.pos = lastFrame.pos.plus(newFrame.acc.multiply(timeGap/30)).multiply(Math.max(1-timeGap/1000,0));
  77. ////////////////////////////////////////////
  78. var accSlpoe1 = (newFrame.accFixed-lastFrame.accFixed);
  79. var accSlpoe2 = (newFrame.accFixed-last2Frame.accFixed);
  80. var accSlpoe3 = (newFrame.accFixed-last3Frame.accFixed);
  81. //newFrame.accSlope = accSlpoe1;
  82. //newFrame.accSlope = Math.min(accSlpoe1,accSlpoe2);
  83. //newFrame.accSlope = Math.min(accSlpoe1,accSlpoe2,accSlpoe3);
  84. newFrame.accSlope = Math.max(newFrame.accFixed-lastFrame.accFixed,0);
  85. /*
  86. if(newFrame.accSlope > 8){
  87. newFrame.accSlope = 1;
  88. }else if(newFrame.accSlope <= 8){
  89. newFrame.accSlope = 0;
  90. }/** */
  91. /////////////////////////////////////////////////
  92. var lastI = this.frame.length-1;
  93. var t2 = this.frame[lastI-1].timeGap;
  94. var t3 = this.frame[lastI].timeGap + t2;
  95. var t4 = newFrame.timeGap + t3;
  96. /*newFrame.predict = new o0.Vector2(
  97. new o0.QuadraticEquation(0,this.frame[lastI-2].acc.x,t2,this.frame[lastI-1].acc.x,t3,this.frame[lastI].acc.x).y(t4),
  98. new o0.QuadraticEquation(0,this.frame[lastI-2].acc.y,t2,this.frame[lastI-1].acc.y,t3,this.frame[lastI].acc.y).y(t4));/** */
  99. /*
  100. newFrame.predict = new o0.Vector2(
  101. 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),
  102. 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));/** */
  103. newFrame.predict = new o0.Vector2(
  104. 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),
  105. 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));/** */
  106. //console.log(newFrame.pos);
  107. //////////////////////////////////////////
  108. //newFrame.shake = o0.distance2(newFrame.predict,newFrame.pos) * 500;
  109. //console.log(newFrame.pos.minus(lastFrame.pos).mod.angle(newFrame.predict.minus(lastFrame.pos).mod));
  110. //newFrame.shake = o0.distance2(newFrame.predict,newFrame.acc) * 100;
  111. newFrame.shake = o0.distance2(newFrame.predict,newFrame.pos) * 100;
  112. //newFrame.shake = o0.distance2(newFrame.predict,lastFrame.pos) * newFrame.acc.length / timeGap * 2000;
  113. if(isNaN(newFrame.shake)){
  114. newFrame.shake = 0.0;
  115. }/** */
  116. //newFrame.shake = Math.pow(o0.distance2(newFrame.predict,newFrame.pos) * newFrame.acc.length / timeGap,0.5) * 500;
  117. /////////////////////////////下面的代码有优化空间
  118. //newFrame.shakeFixed = lastFrame.shakeFixed * 0.9;
  119. //newFrame.shakeFixed = lastFrame.shakeFixed*(1.0+newFrame.shake)*0.9;
  120. //newFrame.shakeFixed = Math.pow(lastFrame.shakeFixed*(1.0+newFrame.shake),0.5)+1;
  121. //console.log(newFrame.shakeFixed+1);
  122. //newFrame.shakeFixed = newFrame.shake;
  123. newFrame.shakeFixed = lastFrame.shakeFixed * 0.85;
  124. if(newFrame.shake > newFrame.shakeFixed){
  125. newFrame.shakeFixed = newFrame.shake;
  126. }/* */
  127. lastFrame.shakeFixed = Math.max(lastFrame.shakeFixed, Math.min(newFrame.shakeFixed,last2Frame.shakeFixed), Math.min(newFrame.shakeFixed,last3Frame.shakeFixed));
  128. ////////////////////////////////////////////////////////////////
  129. /*
  130. var shakeSlpoe1 = (newFrame.shakeFixed-lastFrame.shakeFixed);
  131. var shakeSlpoe2 = (newFrame.shakeFixed-last2Frame.shakeFixed);
  132. var shakeSlpoe3 = (newFrame.shakeFixed-last3Frame.shakeFixed);
  133. //newFrame.accSlope = accSlpoe1;
  134. //newFrame.accSlope = Math.min(accSlpoe1,accSlpoe2);
  135. newFrame.shakeSlope = Math.min(shakeSlpoe1,shakeSlpoe2,shakeSlpoe3);
  136. newFrame.shakeSlope = Math.max(newFrame.shakeSlope,0);/** */
  137. newFrame.shakeSlope = Math.max(newFrame.shakeFixed-lastFrame.shakeFixed,0);
  138. /*
  139. if(newFrame.shakeSlope > 40){
  140. newFrame.shakeSlope = 1;
  141. }else{
  142. newFrame.shakeSlope = 0;
  143. }/** */
  144. //////////////////////////////////////////////
  145. /*
  146. var slopeChange = newFrame.shakeFixed - lastFrame.shakeFixed;
  147. if(slopeChange < 0){
  148. newFrame.slope = (lastFrame.slope + (newFrame.shakeFixed - lastFrame.shakeFixed) / timeGap * 100) * 0.8;
  149. }else{
  150. newFrame.slope = (lastFrame.slope + (newFrame.shakeFixed - lastFrame.shakeFixed) / timeGap * 20) * 0.8;
  151. }
  152. if(newFrame.slope<0){
  153. newFrame.slope = 0;
  154. }/** */
  155. /////////////////////////////下面的代码有很大优化空间
  156. if(lastFrame.hit==0
  157. && last2Frame.hit==0
  158. && last3Frame.hit==0
  159. && last4Frame.hit==0
  160. && last5Frame.hit==0
  161. && (newFrame.accSlope >= 15 || lastFrame.accSlope >= 20)
  162. && (newFrame.shakeSlope >= 20 || lastFrame.shakeSlope >= 40)){
  163. newFrame.hit = newFrame.shake;
  164. this.frameHit = [];
  165. //this.frameSwing = last2Frame;
  166. for(var i = this.frame.length-3;i>=1;--i){
  167. let io = this.frame[i];
  168. let pio = this.frame[i-1];
  169. if(io.accFixed*0.85 <= pio.accFixed && io.accFixed >= pio.accFixed * 0.85){
  170. this.frameSwing = io;
  171. }
  172. }
  173. }else{
  174. newFrame.hit = 0;
  175. }
  176. /*
  177. if(lastFrame.hit == 0
  178. && this.frame[this.frame.length-2].hit == 0
  179. && this.frame[this.frame.length-3].hit == 0
  180. && newFrame.shakeFixed > 150
  181. && newFrame.shakeFixed > lastFrame.shakeFixed * 2){
  182. newFrame.hit = newFrame.shakeFixed;
  183. }else{
  184. newFrame.hit = 0;
  185. }/** */
  186. /////////////力量大小
  187. if(lastFrame.hit!=0){
  188. lastFrame.hit = lastFrame.hit + newFrame.shake;
  189. }else if(last2Frame.hit!=0){
  190. last2Frame.hit = last2Frame.hit + newFrame.shake;
  191. }else if(last3Frame.hit!=0){
  192. last3Frame.hit = last3Frame.hit + newFrame.shake;
  193. }else if(last4Frame.hit!=0){
  194. last4Frame.hit = last4Frame.hit + newFrame.shake;
  195. }else if(last5Frame.hit!=0){
  196. last5Frame.hit = last5Frame.hit + newFrame.shake;
  197. console.log(last5Frame.hit);
  198. }/** */
  199. //newFrame.shake = o0.distance2(newFrame.predict,newFrame.pos) * o0.distance2(lastFrame.pos,newFrame.pos);
  200. //////////////////////////////////////////////////////////////////////////////////
  201. newFrame.reliable = Math.pow((Math.PI/2 - Math.atan(newFrame.shakeFixed/10000)) / (Math.PI/2),2000);
  202. let lastFrameAddSpeed = lastFrame.acc.multiply(lastFrame.timeGap/20).multiply(lastFrame.reliable);
  203. let newFrameAddSpeed = newFrame.acc.multiply(newFrame.timeGap/20).multiply(newFrame.reliable);
  204. newFrame.speed = lastFrame.speed.multiply(0.8);
  205. var lastFrameAngle = newFrame.speed.angle(lastFrameAddSpeed);
  206. if(isNaN(lastFrameAngle)){
  207. lastFrameAngle = 180.0;
  208. }
  209. //newFrame.speed = newFrame.speed.plus(lastFrameAddSpeed.multiply(Math.max(1,lastFrameAngle/60.0 - 1)));
  210. //newFrame.speed = newFrame.speed.multiply(1).plus(lastFrameAddSpeed.multiply(Math.max(1,lastFrameAngle/60.0 - 1)));
  211. newFrame.speed = newFrame.speed.multiply(1-lastFrameAngle/360.0).plus(lastFrameAddSpeed.multiply(Math.max(1,lastFrameAngle/60.0-1)));
  212. var newFrameAngle = newFrame.speed.angle(newFrameAddSpeed);
  213. if(isNaN(newFrameAngle)){
  214. newFrameAngle = 180.0;
  215. }
  216. //newFrame.speed = newFrame.speed.plus(newFrameAddSpeed.multiply(Math.min(1,newFrameAngle/60.0-1)));
  217. newFrame.speed = newFrame.speed.multiply(1-newFrameAngle/360.0).plus(newFrameAddSpeed.multiply(Math.max(1,newFrameAngle/60.0-1)));
  218. //////////////////////
  219. this.frame.shift();
  220. this.frame.push(newFrame);
  221. if(this.frameHit.length <=10){
  222. var o = new Object();
  223. o.time = newFrame.time;
  224. o.timeGap = newFrame.timeGap;
  225. o.acc = newFrame.acc;
  226. o.gyr = newFrame.gyr;
  227. let frameHitLength = this.frameHit.length+1;
  228. let lastFrameHit = this.frameHit[this.frameHit.length-1];
  229. if(frameHitLength == 1){
  230. o.allShakeLength = newFrame.shakeFixed;
  231. }else{
  232. o.allShakeLength = lastFrameHit.allShakeLength + newFrame.shakeFixed;
  233. }
  234. o.accFixed = o.acc.multiply(Math.pow(Math.max(0,1-newFrame.shakeFixed*frameHitLength / o.allShakeLength),5) * 20);//数字越大越忽略历史数据
  235. if(frameHitLength == 1){
  236. o.pos = o.accFixed.multiply(o.timeGap/60);
  237. }else{
  238. o.pos = lastFrameHit.pos.plus(lastFrameHit.accFixed.plus(o.accFixed).multiply(o.timeGap/60));
  239. }
  240. this.frameHit.push(o);
  241. }
  242. var vectorChanged = newForce.minus(this.force);
  243. var forceLength = this.force.length;
  244. var ForceChanged = newForce.length - forceLength;
  245. this.force = newForce;
  246. var currentLeftRight;
  247. if(Math.abs(newForce.x + newForce.z) > Math.abs(newForce.x - newForce.z)){
  248. currentLeftRight = Math.abs(newForce.x + newForce.z);
  249. }else{
  250. currentLeftRight = -Math.abs(newForce.x - newForce.z);
  251. }
  252. this.forceRecord.shift();
  253. this.forceRecord.push(newForce);
  254. if(this.forceChanged > 0 && ForceChanged < 0){// && forceLength > 0.1
  255. //&& this.momentum * Math.pow(0.9,(new Date().getTime()-this.momentunTime)/1) < forceLength){
  256. /*console.log(forceLength.toFixed(2));
  257. if( forceLength > 0.1 && forceLength >= this.momentum * 1.5){
  258. console.log(this.frame+" "+forceLength);
  259. }/** */
  260. if(forceLength > 0.1 && this.momentum != 999999){
  261. if(!this.momentunPunch){//等待击打
  262. if(this.momentunSeek > this.momentum){
  263. this.momentunSeek = this.momentum;
  264. }else{
  265. if(this.momentunSeek < this.momentum * 0.8 - 0.1){
  266. this.momentunPunch = !this.momentunPunch;
  267. this.punchCount++;
  268. //this.leftRight = (newForce.x+this.force.x) < 0;
  269. /*
  270. var largestForce = new o0.Vector3(0,0,0)
  271. for(var i = 0;i<this.forceRecord.length;++i){
  272. var element = this.forceRecord[i];
  273. if(largestForce.length < element.length){
  274. largestForce = element;
  275. }
  276. }
  277. if(Math.abs(largestForce.x + largestForce.z) > Math.abs(largestForce.x - largestForce.z)){
  278. currentLeftRight = Math.abs(largestForce.x + largestForce.z);
  279. }else{
  280. currentLeftRight = -Math.abs(largestForce.x - largestForce.z);
  281. }/** */
  282. this.leftRight = currentLeftRight < 0 ? "Right" : "Left";
  283. this.angle = new o0.Vector2(newForce.x,newForce.z).mod.angle(new o0.Vector2(0,1));
  284. if(this.angle > 90){
  285. this.angle = 180 -this.angle;
  286. }
  287. if(currentLeftRight > 0){
  288. this.angle = -this.angle;
  289. }/** */
  290. //this.leftRight = (largestChange.x > 0 && largestChange.z > 0) || (largestChange.x < 0 && largestChange.z < 0);
  291. /* */
  292. //console.log(this.momentum);
  293. }
  294. }
  295. }else{//打完了
  296. if(this.momentunSeek < this.momentum){
  297. this.momentunSeek = this.momentum;
  298. }else if(this.momentunSeek > this.momentum * 1.2 + 0.1){
  299. this.momentunPunch = !this.momentunPunch;
  300. }
  301. }
  302. }
  303. this.momentum = forceLength;
  304. this.momentunTime = new Date().getTime();
  305. }
  306. this.forceChanged = ForceChanged;
  307. //console.log(vector3.x);
  308. return [new o0.Vector3(newForce),currentLeftRight,this.momentum];
  309. }
  310. test(){
  311. return "123123131";
  312. }
  313. }
  314. };