I'm writing a 2D rigid body simulator. The objects being simulated are convex polygons. My question involves how to decide on the "point" of collision, so that when I apply a response force, I can also calculate torque.
In 2D, the popular method of narrowphase开发者_开发知识库 collision detection seems to be the Separating Axis Theorem. However, while this gives you the "are they colliding?" as well as "by how much?", it does NOT give you a reference point (that I'm aware of) at which to apply the response force (and thus calculate torque)*.
The other method (which also interests me more, because it's what's used in 3D, which would be the logical next step) is to calculate the Minkowski difference of the two polygons, and decide that they're colliding if (0,0) is contained in the resulting polygon. But, how do you use this to decide the relative point at which to apply the response force? My hypothesis is that, since each face of this difference effectively corresponds to a face of one of the polygons, the separation distance is the shortest distance from (0,0) to the MD, and you apply this to the corresponding face on the polygon.
As a bonus, how does one do this in 3D?
*As I'm writing this, I just realized that, when using SAT, I could just keep track of which points are overlapping, and apply the force at the "average" of those points. But I would have to decide on which of the several non-separating axes to perform this little trick...
Here's how I did it once, but there are many possible solutions:
Separating axis theorem will give you a direction from one body to another, this direction is perpendicular to the separating axis (or separating plane in 3D). You can use this direction with each point on the convex bodies to get the penetration distance, or the distance of each point from the separating axis or plane along the direction.
Sort the points by their penetration distance. Begin with the point that is most penetrating and if it is inside the other body, you have found the first point of contact, otherwise proceed to the next. You probably want to continue finding additional points of contact that are within some threshold of distance from the first contact point along the separating direction. Track these points as a 'contact manifold'. Average the points in the manifold, or use some other method, to determine the point to apply the contact force.
Some advanced techniques:
Keeping the contact manifold in memory will allow you to efficiently test on the next frame by testing the previous manifold first.
For continuous collision detection, replace the test of whether the point is inside the other body with a test of whether and where the path of each point within the frame (approximated as a line segment) intersects the other body. This is much more expensive but prevents small, fast-moving objects from tunneling through thin walls.
精彩评论