I try to code the vec of failure for an animation with a ball with a line in 2D. I have the homogeneoous coordinates of the line, and the coordinates of the ball and the velocity. When the incidence realtions homo. coordinates of the line and the ball go to near to zero, I want to run a method which let bounce the ball from the border with the same angle of incidence like the same angle of failure. How is it to compute?
In my opinion I need the normal vec of the line where the ball bounces and have to compute new velocity`s for x,y for the ball but there I´m struggling.... Help would be great.
Let us assume we have given the following:
P1 = (px1, py1) ... line point 1
P2 = (px2, py2) ... line point 2
P1h = (px1, py1, 1) ... homogenous coordinates of line point 1
P2h = (px2, py2, 1) ... homogenous coordinates of line point 2
lh = P1h x P2h ... homogenous coordinates of line (computed with cross product)
v = (vx, vy) ... vector of ball movement
B1 = (bx1, by1) ... ball position 1
B2 = (bx2, by2) = B1 + v ... ball position 2
B1h = (bx1, by1, 1) ... homogenous coordinates of ball position 1
B2h = (bx2, by2, 1) ... homogenous coordinates of ball position 2
Then we can detect, if the ball has crossed the line by comparing the signs of the following scalar products:
ball crossed line <==> sign(B1h*lh) != 0 and sign(B1h*lh) != sign(B2h*lh)
To mirror the movement you can compute the mirror images B1m
and B2m
of B1
and B2
, resp., across the line. Then B2m
is the new ball position and vm = B2m - B1m
is the new (mirrored) direction of the ball movement.
How to compute the mirror image of a point P
across the line l
? Let us assume that
P = (px, py) ... point to be mirrored
Ph = (px, py, 1) ... homogenous coordinates of point to be mirrored
Also note that (lh.x, lh.y)
is a normal vector of line l
. Now follow these steps to compute the mirror image Pm
of P
across l
:
|nl| = sqrt(lh.x^2+lh.y^2) ... length of normal vector
lh0 = lh / |nl| ... "normalized" homogenous line, i.e. HNF (Hesse normal form) of line
d = Ph*lh0 ... signed distance of P to l
lhP0 = lh0 + (0,0,d) ... HNF of line parallel to l running through Pm
mh0 = (lh0.y, -lh0.x, 0) ... HNF of line perpendicular to l (parallel to line
through P and Pm)
md = mh0*Ph ... signed distance of P to mh0
mhP0 = mh0 - (0,0,md) ... HNF of line through P and Pm
Pmh = lhP0 x mhP0 ... homogenous coordinates of mirrored point Pm