Tutorial #5 - Collisions (2) - Introduction
These tutorials try to solve basic problems using easy to understand solutions rather than optimized complex math. Basically, this is a simple way of getting to the answer, not an elegant collection of mathematical skill.
Basic background
In Tutorial #4, we went over how to find the boundaries of triangles. This allowed us to find out which side a point is on, as well as helping to find out if a moving point crosses through the polygon. Using this idea, we can look at two models and see if they collide.
Finding the collision point
When looking for collisions, it may be difficult to find exactly where the collision occurs. The exact point can help improve collisions both visually and calculation wise. The way collisions are currently being done in the Neurosis Engine is to take every line of every triangle from the main object (model_1) and check if they cross the polygons of the potential colliding object (model2).

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.

Reacting to a collision
First we will discuss what happens if the collision is based on a sphere like reaction. We could use this for a spherical collision, or for just a basic bouncing look. This can be done by taking the negative of the normal and flipping it based on the plane normal. The way I'm going to explain it will be more graphical and probably will need more calculations, but then again it works and it's easy to understand.

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.

This ends the tutorial. As indicated in the beginning, this tutorial is only to give an idea of how to get things going and because of that some cases and/or calculations may be missing or just skipped to simplify things. If anyone has any comments or questions, please feel free to email.
© 2023 Luigi Pino. All rights reserved.
Privacy | Links