I have used the following code to calculate the reflection vector for my ball, the problem is it just bounces back in the direction it came from
Any idea why? I followed the theory from one of the tutorials on the nehe site.
a-((a.b)/(b.b))*b is the component of a orthogonal to b. If the magnitude of b is 1 then that reduces to a-(a.b)*b. You don''t want orthogonal, but the reflection so it is a-2*(a.b)*b. (a.b)*b is the component of a parallel to b and (-a).b = -(a.b). If you normal either points the wrong direction or is does not have a magnitude of one then you don''t get the right answer.
Keys to success: Ability, ambition and opportunity.
ballMovement''s length is 1 if it is normalized. In fact, once your vectors are normalized, you have just lost the magnitude of your speed, which isn''t very good. You can try this
Normal = Normalize(Normal); finalspeed = initialspeed + Normal * Dot(initialspeed, Normal) * 2
Don''t normalize the initialspeed. This should work if your normal is pointing outside the paddle. However, if your code really bounces the ball back in the direction it came from, then your code for evaluating the normal might have a problem.
It seems the speed has to be normalised otherwise it tends to be amplified every time a collision occurs. Quite funny but not very helpful.
I am still sure that the normal is fine, it is a flat paddle at the bottom of the screen. When I print all the vertex data, including normal, to a file I get this:
Notice that newVector = (3/5, 4/5, 0) regardless of the magnitude of ballmovement. You could have chose (6, -8, 0) and still get the same results. If you insist on keeping the code as it is, then you have to store the length of ballMovement _before_ normalizing it, and multiply by that variable at the end, instead of multiplying by ballMovement.length().
If you are getting an amplification on reflection then your normal doesn''t have a magnitude of one. Lets use a simple case. You are bouncing off the x axis and the point of collision is the origin. Your velocity vector is (x,y) and the normal is (0,1). Your velocity vector after reflection is (x,y)-2*((0,1).(x,y))*(0,1) = (x,y)-2*y*(0,1) = (x,y)-(0,2y) = (x,y-2y)=(x,-y). Now how did your magnitude change?
Keys to success: Ability, ambition and opportunity.
This is how it stand at present, the speed after collision is fine - looks the same. The problem is in the angle of reflection, it is inconsistent. Looking at it carefully, the ball reflects at an angle increasing closer to 90 degrees, parallel with the normal. Any other thoughts? I was hoping to solve this and make some significant progress today....