|
So for each initial triangle, three checks are being made - [0][1], [0][2] and [1][2]. These represent two points of the model_1 triangle and for each check - whether the two points intercept a colliding polygon from model_2. Using what was previously learned in Tutorial #4, we can calculate the exact collision point.
For every two points from the given triangle of model_1 - see if they are on separate sides and if they are find the collision point. To find the point, calculate the exact position of the collision with the colliding triangle. To do this, use both points and the distance from the collision plane and linearly interpolate the position for each border line of a triangle.
proportion = abs(point_distance[0] / temp_distance);
collision_point.x = point_pos[0].x - ((point_pos[0].x - point_pos[1].x) * proportion);
collision_point.y = point_pos[0].y - ((point_pos[0].y - point_pos[1].y) * proportion);
collision_point.z = point_pos[0].z - ((point_pos[0].z - point_pos[1].z) * proportion);
Then average out all the collision points and get the exact point. This may take some extra processing power, but the cool thing is you could use a separate collision model to get a quicker representation. Also, a bounding box algorithm could be used to speed up the checks before any other calculations take place. Once the collision point is found to exist, the collision can take place.
Imagine a wall straight in front of you. If you bounce a ball right at it it's going to come back - the y value is strictly a sign change. Now if you bounce the ball at an angle, the y will again have a sign change, but the x value will continue exactly the same. So as long as the collision plane is facing straight out, all we have to do is take the negative of the y normal of the collision.
Now for ease of complexity, any collision that occurs - take the plane and rotate it and everything else we need like the actual collision point and normal. Then all we need to do is take the (x, -y, z) of the normal and then re-rotate against what we previously did to get back to the original rotations. Now the collision will occur as needed and we're happy.
If it's a non-spherical object, all we need to do is rotate the object according to its center of gravity - or the center of all points within the object. Then, depending on where the collision point is, rotate accordingly. For example, if a car hits on the front right, rotate the car counter-clockwise. This is also easier because instead of dealing with all points of collision, we only have to focus on the point we calculated.