Ball bounce off a circular border

enter image description here

This is the formula I found:

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

This is how i applied

    //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;

But instead of bouncing, the balls disappear when they hit the border:

Full src and preview http://jsfiddle.net/gj4Q7/4/

Thanks for your time, any advice is welcome!

+4
source share
1 answer

I think you need to normalize normal :)

After

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

Embed

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

You can see that otherwise you can easily get values ​​in the range 350 * 350 for speed by catapulting objects into space ...

+3
source

All Articles