![]() |
|
Ball in the cornerNot all the walls go on forever, fading into mist on the distance mountains where the pure snow covers the high peaks and lonely yetis hide in the cold caves. Poor yetis have to hide from the greedy photographers and journalists who are ready to do anything to get photo of yeti or an interview from him. Of course after being interviewed the life is not same anymore for the yetis, countless tourists run into their caves, screaming, playing with yetifood and breaking vodkabottles against walls leaving sharp pieces of broken glass everywhere. So, some walls start and end at specific points. We already know those points are called starting point and ending point. We now find collision with those points and new movement vector if the ball manages to hit corner point before side of the wall.
Red line is the wall. Grey line is the vector which goes straight to wall, either to the side of the wall or to its corner point. In the findIntersection function we find which is closest to the ball and which vector will be used to move ball out of intersection. Wall is v2, ball is v1. First find vector from the starting point of the wall to the center of ball: var v3={};
v3.vx=v1.p1.x-v2.p0.x;
v3.vy=v1.p1.y-v2.p0.y;
Now we find dot product between this new vector v3 and the wall var dp=v3.vx*v2.dx + v3.vy*v2.dy; The ball is closer to the starting point if this dot product is negative. And the vector to move ball back is vector v3: if(dp<0){
var v=v3;
}
If the dot product was zero or positive we check if the ball is closer to the end point of the wall. So, find vector from the end point of the wall to the center of ball: else{
var v4={};
v4.vx=v1.p1.x-v2.p1.x;
v4.vy=v1.p1.y-v2.p1.y;
and like with first point, we again calculate the dot product between new vector v4 and the wall var dp=v4.vx*v2.dx + v4.vy*v2.dy; The ball hits end point first if the dot product is positive and v4 is used to move ball away: if(dp>0){
var v=v4;
}
If however dot product was this time zero or negative then the side of the wall is closest to the ball. Now we project the vector from start point of the wall to center of ball (v3) on the wall normal. The ball will be moved back in the direction of walls normal same way we did it in last chapter: else{
var v=projectVector(v3, v2.lx, v2.ly);
}
Finally we return correct vector return v; Remember that the vector to bounce off the ball will be perpendicular to the vector used to move ball away from the wall. When the ball hits side of the wall then we can use vector of the wall to find new movement vector. When it hits the corners then normal of the vector from endpoint of the wall to the center of ball can be used. Try to drag walls around in this example to see the ball hitting corners.
Because we only check for the end position of the ball then with big enough speed it is possible for the ball to go through the wall without seeing it. To avoid this situation you should make sure the speed of the ball is never bigger then then its radius. You can download the source fla of example with 1 wall and with multiple walls. Next: Ball vs ball. |
|