Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to have certain circular objects bounce against all four sides of the screen. After searching a lot on the internet, I found the following formula:

Vout = Vin - ((1+e) *dot(Vnormal, Vin) * Vnormal)

However, I am having difficulties to implement this formula in my current engine, for the following reasons:

  • My object's next position is determined using a Velocity and a Direction(in radians) property. This means that I need to convert it to and from vectors each time I use this formula.
  • Because screen-coordinates move from the top left (and not the bottom left as in math), I calculate the next position of the objects using the following:

var newx = -Math.sin(this.rotation)*this.speed;
var newy = Math.cos(this.rotation)*this.speed;

  • I am using Javascript+Html5 Canvas. This does not have built-in vector objects, meaning the math gets even more confusing.

Currently, I am doing the following:

vinx = -Math.sin(this.rotation)*this.speed;
viny = Math.cos(this.rotation)*this.speed;
var vnormalx = 0;
var vnormaly = 1;//Should bounce against bottom of the screen.
var e = 1;//Restitution. Is 1, thus object should get full energy back.
var voutx = vinx - (1+e) * ((vnormalx*vinx)+(vnormaly*viny)) *vnormalx;
var vouty = viny - (1+e) * ((vnormalx*vinx)+(vnormaly*viny)) *vnormaly;
var newspeed = Math.sqrt(Math.pow(voutx, 2)+Math.pow(vouty, 2)); //pythagoras to get magnitude(the length) of the vector.
var newrotation = Math.atan(voutx/vouty); //angle of vector

this.rotation = newrotation;
this.speed = newspeed;

So, this is not working. I have no clue why. I tried changing the normal vector to (0, -1) and even (1, 0) and (-1, 0), but no cigar. I think this might have to do with the way I have rotated the x and y perspective, but I have no idea how to change this, or if it indeed is the cause of the problem or not.

share|improve this question
Maybe this will help, as it covers a lot of stuff you're attempting: gamedev.tutsplus.com/tutorials/implementation/… –  RandyGaul Sep 11 '13 at 17:08
Thank you very much for that resource. Although it gives much -greatly appreciated- insight in the background of restitution physics equations, it does not help with fixing this specific problem. –  Qqwy Sep 12 '13 at 21:20
Wait why not? You can use an impulse solver to resolve the "penetration" with the world boundaries. I'm fairly sure it does solve the problem, perhaps you could try explaining how it doesn't? I'd be willing to help if I understood a little more. –  RandyGaul Sep 13 '13 at 6:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.