![]() |
|
2 moving ballsSo, the moving ball is probably most happy already because it can move and explore the wonders of the world. But what happens if other ball decides to stop standing in single place and become moving also? It has every right to do so. In such case our collision might fail. Lets look at some examples:
Ball1 moves by the amount of red vector and same time ball2 moves by the amount of blue vector. We must take into account both movement vectors if we wish to detect collision correctly.
In this picture ball1 has reached point p2 and ball2 has reached point p3 when they collide. We are very lucky to use vectors because we can add up vectors easily. So, instead finding collision between 2 moving balls, we substract movement vector of ball2 from movement vector of ball1 and then we can use moving ball vs non-moving ball collision calculation from last chapter.
Find vector v3 from ball1 starting coordinates which is ball1 movement vector substracted ball2 movement vector: v3={};
v3.p0=ball1.p0;
v3.vx=ball1.vx-ball2.vx;
v3.vy=ball1.vy-ball2.vy;
Now we take collision calculation from last chapter, when ball1 would move by the vector v3 and ball2 is not moving at all. If the balls collide then ball1 is at the coordinates of point p4. We find new vector v4 which is movement of ball1 to the point when balls collide: game.v4={p0:ball1.p0, p1:p4};
Because we know both balls move over same time, we can find variable "t" which is length of v4 divided by length of v3. t=v4.len/v3.len; Variable "t" is between 0 and 1. When it is 1 then collision happens when both ball1 reach their end point. If t is 0 then collision happens when balls are at their starting points. To find coordinates of both balls when they collide we need to multiply their movement vectors with t: ball1.p1.x=ball1.p0.x+t*ball1.vx; ball1.p1.y=ball1.p0.y+t*ball1.vy; ball2.p1.x=ball2.p0.x+t*ball2.vx; ball2.p1.y=ball2.p0.y+t*ball2.vy; I have made an example of 2 balls moving:
You can drag the balls or endpoints of vectors. You can download the source fla of example here. Bouncing 2 moving ballsAfter collision positions are found we can change their movement vectors like explained in "Ball vs ball" and "Bounce" chapters. But we only had 1 ball moving so far, and with 1 moving ball it bounced from the wall with same speed (only direction of movement changed). When both balls move, their movement affects other ball too and resulting movement vectors must be recalculated from both movement vectors. Bounce depends on the mass of both balls. To make life simpler we suppose that balls have equal mass. In such case they exchange their movement vectors component on the direction of vector between their center points:
vc is vector between center points of balls, vcn is normal of vc. Now we project both movement vectors v1 and v2 onto vc and vcn:
v1 is broken into components v1a and v1b, same way v2 breaks into v2a and v2b. For resulting movement vectors v1a and v2a are exchanged and ball1 will get new movement vector made from v1b and v2a while ball2 will get new movement from v1a and v2b. And in this example some balls move and bounce:
You can download the source fla of example here. Next: Ball vs arc. |
|