One of the things that we can find useful about vectors is Intersection. we can find out at which point a vector intersection with the path of another vector. Although this is the sort of thing most programmers will consider useful since it can detect the exact point of collision in a dynamic way. It is not the only way and will not be the one we are going to concentrate on but for general purpose and for the sake of becoming familiar with it I will discuss it in a quick step here. for detail explanation the formula is explained here in detail.
For programming purposes lets establish a few rules. The first rule for a vector to intersect another the vector, the vectors must not be parallel. Second a calculation must be performed for each vector to determine at what point in the path of the other vector did collision happened. And third if a collision is to be determined, a collision test must be performed for each of the vectors.
Now lets work toward our example. Lest assume we have two vectors and lets say one is a wall(vector B) and one is a point moving at 100 pixels per frame(vector A). Something like this:
Notice that there is a vector ‘C’ this vector is created from the start of vector a to the start of vector b and is done like this:
C.vx = B.px – A.px
C.vy = B.py – A.py
vx and vy are the vector components, while px and py are the vector position. So far vector ‘C’ does not have a position and since it does not have any other use but to help us figure out the point of collision we are not going to bother with its position. The formula to find the point of collision goes like this.
First we find the perpendicular dot product of vector ‘A’ into vector ‘B’:
dp1 = B.vx * C.vy – B.vy * C.vx
dp2 = A.vx * B.vy – A.vy * B.vx
t = dp1/dp2
“t” in this case is a number that will tell us the exact point of collision. Its a ration that when multiplied by the source vector in this case vector ‘A’ will tell us at what point the collision happened.
to find the exact point of collision for vector ‘A’ to the path of vector ‘B’ the collision will have to be done like this:
p.x = A.px + A.vx * t
p.y = A.py + A.vy * t
One thing to note with this point of collision. is that even if the vector is not colliding with the wall there will still be a point of collision. As stated before ‘t’ is a ratio. The ratio can represent several things. First if ‘t’ is less than 0 then the collision happened in a previous frame. if the collision is greater than 1 then the collision will happen in another frame in the future. if ‘t’ is greater than or equal to 0 or ‘t’ is less than or equal to 1 then the collision happened in the current frame. now if ‘t’ is between 0 and 1 it does not necessarily mean that there is a collision with the wall. To find if there is a collision with the wall, we must check if the wall collides with the path of the point also and to solve that we need to do a reverse collision:
C.vx = A.px – B.px
C.vy = A.py – B.py
dp1 = C.vx * B.vy – C.vy * B.vx
dp2 = B.vx * A.vy – B.vy * A.vx
t2 = dp1/dp2
p.x = B.px + B.vx * t2
p.y = B.py + B.vy * t2
Ok, so now we can determine if there was an actual collision. If both t and t2 are both greater than or equal to 0 and both are less than or equal to 1 then there was an actual collision and p.x,p.y is the point of collision.
Note that due to floating point inaccuracies the p.x,p.y obtained from the second equations may or may not match the p.x,p.y from the first equations but should be pretty close and should be taken into consideration when solving this problem.
The above is the common way of finding the exact point of collision and usable as long as the vector does not have a relationship to an object. But what if one of the vector is the movement of a ball and the other is a wall? A couple of considerations have to be taken such as the radius of the ball and where the actual ball hits the wall. Consider this illustration:
Lets explain what happens here. the diagram illustrate exactly what happens with in a specific period of time. In computer time this can be a frame and the vector represents how much the ball would move if there was no wall in its path during a single frame. Note that there are two points that represent collision and intersection and do not happen in the same spot. As can be seen the point of collision has very little to do with the vector point of intersection. Since the vector point of intersection is farther than, in this case, the ball radius, We need to find an alternative method to determine the object exact point of collision. There are several ways to do this. One is to move the wall toward the ball the amount of distance of the ball radius during the calculation. The problem with this is that depending from which side the ball is approaching the wall, we need to move the wall in that direction, which is quite a bit of calculations to do to get it right. The other is to move the ball instead of the wall but either way there are too many obstacles to overcome. This Third one, which is the one I like, is not as intensive and resolves the problem in a simpler way. Not that much simpler but simpler none the less.
—– to be continued ——–