Hi, I'm having trouble computing reflection angles for a ball hitting an oblique wall. I'm using an algorithm lifted from this tutorial. It looks like this (in Actionscript 3), with p1 being the current velocity vector and p2 the normal of the wall:
private function getReflect2(p1 : Point, p2 : Point) : Point
{
var wallvec : Point = getNorm(p2);
var wallnorm : Point = p2;
var t : Number = dotProduct(wallvec, p1);
var n : Number = dotProduct(wallnorm, p1);
var vt : Point = new Point(wallvec.x * t, wallvec.y * t);
var vn : Point = new Point(wallnorm.x * -n, wallnorm.y * -n);
var vx : Number = dotProduct(new Point(1,0), vn) + dotProduct(new Point(1,0), vt);
var vy : Number = dotProduct(new Point(0,1), vn) + dotProduct(new Point(0,1), vt);
return new Point(vx, vy);
}
The function returns the new velocity vector, and does so correctly for collisions with perpendicular walls but not for oblique ones. The ball may hit the wall from both "sides" (ie. the normal may be jutting in the other direction).
Can anyone spot my error? Or suggest a better algorithm?