I am creating a simple physics engine. Based on some prior vector calculus knowledge I wrote this code right here that takes the x and y components of an instantaneous direction vector "u" and the x and y components of the vector that is the line that the ball collides with "v" and returns a double 'theta' that is the angle between the incoming ball and the line.
private double computeAngle(double uXComponent, double uYComponent,
double vXcomponent, double vYComponent) {
return Math.acos( ( (uXComponent * uYComponent) + (uYComponent * vYComponent) ) / ( Math.sqrt( (uXComponent * uXComponent) + (uYComponent * uYComponent) ) * Math.sqrt( (vXcomponent * vXcomponent) + (vYComponent * vYComponent) ) ) );
}
The program goes on to use this code to determine the new velocity components.
xVelocity *= Math.cos(theta);
yVelocity *= Math.sin(theta);
However if the line is flat i.e v=<500, 0> and the ball is moving in a simple parabolic curve and bouncing across the line the very first bounce is always straight up(xVelocity very near 0). Essentially the value for theta is extremely close to pi/2 (closer than is really should be).
I am sure there is a better way to do this potentially by using dot product. Can anyone help?