o0Project.js 13 KB

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