从圆形边界弹出一个球

时间:2013-11-11 23:20:59

标签: javascript html math canvas vector

enter image description here

这是我发现的公式:

r = v - [2(n·v)n]

这是我应用的方式

    //Calculating normal
    nx = 350 -  SmileyReds[i].xpos ;
    ny = 350 -  SmileyReds[i].ypos ;

  //new calc
   v_newx = SmileyReds[i].xspeed - (2 *( nx * SmileyReds[i].xspeed + ny * SmileyReds[i].yspeed ) ) * nx;
   v_newy = SmileyReds[i].yspeed - (2 *( nx * SmileyReds[i].xspeed + ny * SmileyReds[i].yspeed ) ) * ny;

   SmileyReds[i].xspeed = v_newx;
   SmileyReds[i].yspeed = v_newy;

但是当它们撞到边界时,球不会弹跳,而是会消失:

完整的src和预览http://jsfiddle.net/gj4Q7/4/

感谢您的时间,欢迎提出任何建议!

1 个答案:

答案 0 :(得分:3)

我认为你需要规范正常情况:)

//Calculating normal
nx = 350 -  SmileyReds[i].xpos;
ny = 350 -  SmileyReds[i].ypos;

插入

var len = Math.sqrt(nx * nx + ny * ny);
nx = nx / len;
ny = ny / len;

你可以看到,你可以很容易地获得350 * 350的速度值,将物体弹射到太空......