javascript – 根据质量和弹跳系数计算球与球碰撞的速度和方向

前端之家收集整理的这篇文章主要介绍了javascript – 根据质量和弹跳系数计算球与球碰撞的速度和方向前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用了基于 this的以下代码
  1. ballA.vx = (u1x * (m1 - m2) + 2 * m2 * u2x) / (m1 + m2);
  2. ballA.vy = (u1y * (m1 - m2) + 2 * m2 * u2y) / (m1 + m2);
  3.  
  4. ballB.vx = (u2x * (m2 - m1) + 2 * m1 * u1x) / (m1 + m2);
  5. ballB.vy = (u2y * (m2 - m1) + 2 * m1 * u1y) / (m1 + m2);

但由于该公式是针对一维碰撞而设计的,因此显然效果不佳.

所以我尝试使用this section以下的公式.

但问题是我不知道偏转角是什么以及如何计算它.另外,如何考虑这个公式中的弹跳系数?

编辑:我可能还不清楚.上面的代码确实有效,尽管它可能不是预期的行为,因为原始公式是针对一维碰撞而设计的.因此,我正在尝试的问题是:

>什么是2D等价物?
>如何考虑弹跳系数?
>如何计算碰撞后两个球的方向(用vx和vy表示)?

解决方法

我应该首先说:我创造了一个新的答案,因为我觉得旧的答案具有简单的价值

这里承诺的是一个更复杂的物理引擎,但我仍然觉得它很容易跟随(希望!或者我只是浪费了我的时间……大声笑),(网址:http://jsbin.com/otipiv/edit#javascript,live)

  1. function Vector(x,y) {
  2. this.x = x;
  3. this.y = y;
  4. }
  5.  
  6. Vector.prototype.dot = function (v) {
  7. return this.x * v.x + this.y * v.y;
  8. };
  9.  
  10. Vector.prototype.length = function() {
  11. return Math.sqrt(this.x * this.x + this.y * this.y);
  12. };
  13.  
  14. Vector.prototype.normalize = function() {
  15. var s = 1 / this.length();
  16. this.x *= s;
  17. this.y *= s;
  18. return this;
  19. };
  20.  
  21. Vector.prototype.multiply = function(s) {
  22. return new Vector(this.x * s,this.y * s);
  23. };
  24.  
  25. Vector.prototype.tx = function(v) {
  26. this.x += v.x;
  27. this.y += v.y;
  28. return this;
  29. };
  30.  
  31. function BallObject(elasticity,vx,vy) {
  32. this.v = new Vector(vx || 0,vy || 0); // velocity: m/s^2
  33. this.m = 10; // mass: kg
  34. this.r = 15; // radius of obj
  35. this.p = new Vector(0,0); // position
  36. this.cr = elasticity; // elasticity
  37. }
  38.  
  39. BallObject.prototype.draw = function(ctx) {
  40. ctx.beginPath();
  41. ctx.arc(this.p.x,this.p.y,this.r,2 * Math.PI);
  42. ctx.closePath();
  43. ctx.fill();
  44. ctx.stroke();
  45. };
  46.  
  47. BallObject.prototype.update = function(g,dt,ppm) {
  48.  
  49. this.v.y += g * dt;
  50. this.p.x += this.v.x * dt * ppm;
  51. this.p.y += this.v.y * dt * ppm;
  52.  
  53. };
  54.  
  55. BallObject.prototype.collide = function(obj) {
  56.  
  57. var dt,mT,v1,v2,cr,sm,dn = new Vector(this.p.x - obj.p.x,this.p.y - obj.p.y),sr = this.r + obj.r,// sum of radii
  58. dx = dn.length(); // pre-normalized magnitude
  59.  
  60. if (dx > sr) {
  61. return; // no collision
  62. }
  63.  
  64. // sum the masses,normalize the collision vector and get its tangential
  65. sm = this.m + obj.m;
  66. dn.normalize();
  67. dt = new Vector(dn.y,-dn.x);
  68.  
  69. // avoid double collisions by "un-deforming" balls (larger mass == less tx)
  70. // this is susceptible to rounding errors,"jiggle" behavior and anti-gravity
  71. // suspension of the object get into a strange state
  72. mT = dn.multiply(this.r + obj.r - dx);
  73. this.p.tx(mT.multiply(obj.m / sm));
  74. obj.p.tx(mT.multiply(-this.m / sm));
  75.  
  76. // this interaction is strange,as the CR describes more than just
  77. // the ball's bounce properties,it describes the level of conservation
  78. // observed in a collision and to be "true" needs to describe,rigidity,// elasticity,level of energy lost to deformation or adhesion,and crazy
  79. // values (such as cr > 1 or cr < 0) for stange edge cases obvIoUsly not
  80. // handled here (see: http://en.wikipedia.org/wiki/Coefficient_of_restitution)
  81. // for now assume the ball with the least amount of elasticity describes the
  82. // collision as a whole:
  83. cr = Math.min(this.cr,obj.cr);
  84.  
  85. // cache the magnitude of the applicable component of the relevant velocity
  86. v1 = dn.multiply(this.v.dot(dn)).length();
  87. v2 = dn.multiply(obj.v.dot(dn)).length();
  88.  
  89. // maintain the unapplicatble component of the relevant velocity
  90. // then apply the formula for inelastic collisions
  91. this.v = dt.multiply(this.v.dot(dt));
  92. this.v.tx(dn.multiply((cr * obj.m * (v2 - v1) + this.m * v1 + obj.m * v2) / sm));
  93.  
  94. // do this once for each object,since we are assuming collide will be called
  95. // only once per "frame" and its also more effiecient for calculation cacheing
  96. // purposes
  97. obj.v = dt.multiply(obj.v.dot(dt));
  98. obj.v.tx(dn.multiply((cr * this.m * (v1 - v2) + obj.m * v2 + this.m * v1) / sm));
  99. };
  100.  
  101. function FloorObject(floor) {
  102. var py;
  103.  
  104. this.v = new Vector(0,0);
  105. this.m = 5.9722 * Math.pow(10,24);
  106. this.r = 10000000;
  107. this.p = new Vector(0,py = this.r + floor);
  108. this.update = function() {
  109. this.v.x = 0;
  110. this.v.y = 0;
  111. this.p.x = 0;
  112. this.p.y = py;
  113. };
  114. // custom to minimize unnecessary filling:
  115. this.draw = function(ctx) {
  116. var c = ctx.canvas,s = ctx.scale;
  117. ctx.fillRect(c.width / -2 / s,floor,ctx.canvas.width / s,(ctx.canvas.height / s) - floor);
  118. };
  119. }
  120.  
  121. FloorObject.prototype = new BallObject(1);
  122.  
  123. function createCanvasWithControls(objs) {
  124. var addBall = function() { objs.unshift(new BallObject(els.value / 100,(Math.random() * 10) - 5,-20)); },d = document,c = d.createElement('canvas'),b = d.createElement('button'),els = d.createElement('input'),clr = d.createElement('input'),cnt = d.createElement('input'),clrl = d.createElement('label'),cntl = d.createElement('label');
  125.  
  126. b.innerHTML = 'add ball with elasticity: <span>0.70</span>';
  127. b.onclick = addBall;
  128.  
  129. els.type = 'range';
  130. els.min = 0;
  131. els.max = 100;
  132. els.step = 1;
  133. els.value = 70;
  134. els.style.display = 'block';
  135. els.onchange = function() {
  136. b.getElementsByTagName('span')[0].innerHTML = (this.value / 100).toFixed(2);
  137. };
  138.  
  139. clr.type = cnt.type = 'checkBox';
  140. clr.checked = cnt.checked = true;
  141. clrl.style.display = cntl.style.display = 'block';
  142.  
  143. clrl.appendChild(clr);
  144. clrl.appendChild(d.createTextNode('clear each frame'));
  145.  
  146. cntl.appendChild(cnt);
  147. cntl.appendChild(d.createTextNode('continuous shower!'));
  148.  
  149. c.style.border = 'solid 1px #3369ff';
  150. c.style.display = 'block';
  151. c.width = 700;
  152. c.height = 550;
  153. c.shouldClear = function() { return clr.checked; };
  154.  
  155. d.body.appendChild(c);
  156. d.body.appendChild(els);
  157. d.body.appendChild(b);
  158. d.body.appendChild(clrl);
  159. d.body.appendChild(cntl);
  160.  
  161. setInterval(function() {
  162. if (cnt.checked) {
  163. addBall();
  164. }
  165. },333);
  166.  
  167. return c;
  168. }
  169.  
  170. // start:
  171. var objs = [],c = createCanvasWithControls(objs),ctx = c.getContext('2d'),fps = 30,// target frames per second
  172. ppm = 20,// pixels per meter
  173. g = 9.8,// m/s^2 - acceleration due to gravity
  174. t = new Date().getTime();
  175.  
  176. // add the floor:
  177. objs.push(new FloorObject(c.height - 10));
  178.  
  179. // as expando so its accessible in draw [this overides .scale(x,y)]
  180. ctx.scale = 0.5;
  181. ctx.fillStyle = 'rgb(100,200,255)';
  182. ctx.strokeStyle = 'rgb(33,69,233)';
  183. ctx.transform(ctx.scale,ctx.scale,c.width / 2,c.height / 2);
  184.  
  185. setInterval(function() {
  186.  
  187. var i,j,nw = c.width / ctx.scale,nh = c.height / ctx.scale,nt = new Date().getTime(),dt = (nt - t) / 1000;
  188.  
  189. if (c.shouldClear()) {
  190. ctx.clearRect(nw / -2,nh / -2,nw,nh);
  191. }
  192.  
  193. for (i = 0; i < objs.length; i++) {
  194.  
  195. // if a ball > viewport width away from center remove it
  196. while (objs[i].p.x < -nw || objs[i].p.x > nw) {
  197. objs.splice(i,1);
  198. }
  199.  
  200. objs[i].update(g,ppm,objs,i);
  201.  
  202. for (j = i + 1; j < objs.length; j++) {
  203. objs[j].collide(objs[i]);
  204. }
  205.  
  206. objs[i].draw(ctx);
  207. }
  208.  
  209. t = nt;
  210.  
  211. },1000 / fps);

真正的“肉”和讨论的起源是obj.collide(obj)方法.

如果我们潜入(我这次评论它,因为它比“最后”复杂得多),你会看到这个等式:,仍然是这一行中唯一使用的:this.v.tx(dn .multiply((cr * obj.m *(v2-v1)this.m * v1 obj.m * v2)/ sm));现在我确定你还在说:“zomg wtf!这是一个相同的单维方程!”但是当你停下来思考它时,“碰撞”只会在一个维度上发生.这就是为什么我们使用矢量方程来提取适用的分量并将碰撞仅应用于那些特定的部分而使其他部分不受影响地继续它们的快乐方式(忽略摩擦并简化碰撞以不考虑动态能量转换力,如评论CR).随着对象复杂性的增加和场景数据点的数量增加,这个概念显然变得更加复杂,以解决诸如畸形,转动惯量,不均匀的质量分布和摩擦点等问题……但是这远远超出了它的范围,它几乎不会值得一提..

基本上,你真正需要“掌握”这个让你觉得直观的概念是Vector方程的基础知识(都位于Vector原型中),它们如何与每个相互作用(它实际上意味着规范化,或者采取一个点) /标量产品,例如,阅读/与知识渊博的人交谈)以及对碰撞如何作用于物体属性的基本理解(质量,速度等……再次,阅读/与知识渊博的人交谈)

我希望这有帮助,祝你好运! -ck

猜你在找的JavaScript相关文章