You can do this using the dot product. Move the problem to 2D first.
Here is a solution in 2D:
Notice that the above equation knows nothing about the wall. In 2D you really don't need to unless you want slanted walls which then you must take the normal into account. But for simple boxed in playing field you can assume that if you have entered the above function...you have hit a wall.
void ComputeReflection2D(ball &_theball)
//Form a right triangle based on last position
//Since we know we have hit a wall our current location is the basis for the right
//The X vector or base of triangle
//The Y vector or height of triangle
//The length of the hypotenuse - the path we just took
//The new vectors normalized
//Update last position for next hit calculation
Quickly you will notice that this can also be accomplished by using cos(). Essentially that is what the code is doing, if I wrote it right. It's been sometime since I approached this problem.
But in 3D this is a very simple calculation and could be used in 2D as well as long as Z was always assumed to be 1.0f.
Here is the formula:
Arccos(theta)=||N dot V||
Or the arccos of theta is equal to the magnitude of the dot product of the normal to the plane and the vector in question. We need the magnitude because we want this to be normalized.
So the angle between two vectors in 3D can be computed by taking the arccos of the dot product of the two vectors (magnitude for non-unit vectors).
Since you know the normal and you know the vector of the ball you can compute the reflection angle quite easily.
The vector for the ball is the current position, but it might have to be computed by:
Just simply using ball.pos will only work if your world is centered at 0,0,0 - which it should be. If the ball is moving to the left that would mean that x would be less than 0 which would make the formula work correctly.
This should work for your needs.
Again if this doesn't work exactly as coded I apologize. I did get perfect reflections working in my pong game but that was ages ago.
void ComputeReflection3D(Vector3 &NormalVector,Vector3 &BallVector,Vector3 &BallVelocityVector)
//Normalize the BallVector
//Compute dot product
//Now based on dot product compute new BallVelocityVectors