Hiya:

I have tried to reproduce this equation to simple reflect a ball after colliding with a paddle (or screen bounds)

R = V – (2 * V[dot]N) N

I don't want to take the easy rout of just negating the velocity but is my first time trying to implement this equation (with little vector practice :( ) Perhaps I'm simply misunderstanding the formula.

Here's a little code snippet where I'm checking to see if the ball hits the right face of a paddle on the left side of the screen:

if(CheckCollision(bPos, b_Width, b_Height, p1Pos, p1_Width, p1_Height))
	{
		Vector norm;
		norm.myVector.x = 1;
		norm.myVector.y = 0;

		float number = 0;
		number = (2 * bPos.DotProduct(norm));


		bPos.myVector.x = bPos.myVector.x - 2 * number * norm.myVector.x;
		bPos.myVector.y = bPos.myVector.y - 2 * number * norm.myVector.y;

		
	}
	
}

I don't want to take the easy rout of just negating the velocity but is my first time trying to implement this equation (with little vector practice :( ) Perhaps I'm simply misunderstanding the formula.

I'd say that you should think about how important it is for you to have a generalised reflection of a sphere from a surface at this point in time?

If this version of your program is only ever going to have vertical paddles, then you can easily implement that reflection (the x-coordinate is just reversed when you hit a paddle). This will allow you to get on and get something working. I'd leave the function that you have in place, but just have it do something simple for now (like reverse the x-coordinate). Then, when you want to make it more complex in version 2.0 of your program, you just need to edit this method to look at the angle of the paddle and do the more complicated thing later.

Of course, if you're only making this program so that you can see how things reflect off surfaces at different angles, then you have nothing for it but to try and get this to work now :)

I think you're implementing it wrong. It should be,

if(CheckCollision(bPos, b_Width, b_Height, p1Pos, p1_Width, p1_Height))
{
		Vector norm;
		norm.myVector.x = 1;
		norm.myVector.y = 0;

                // i - (2 * n * dot(i, n)) 
		bPos.myVector.x = bPos.myVector.x - 2 * norm.myVector.x * bPos.DotProduct(norm);
		bPos.myVector.y = bPos.myVector.y - 2 * norm.myVector.y * bPos.DotProduct(norm);

		
	}
	
}

You had an extra 2 term

i got it now. turns out needed a direction vector where the ball is heading instead of using the position of the ball. Both formulas needed that ^^ anyway thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.