I still havent figured out the shallow penetration thing, but im writing a small java program (2d) that shows the shapes, the minkowski difference, simpex and contact info.
I got the basic GJK working that tells me if two bodys are colliding or not, and I have tried to do what bullet does for deep penetrations by sampling the support function and finding the minimum distance, but its not really working how its supposed to (at least I think)
For two spheres, the sampling works perfectly and gives the right results all the time, but whenever I introduce something like a box or convex polygon with defined verticies, it either returns one vertex or the other, never a point on the line connecting them liek it should.
Its hard to explain exactly whats going on, but here is what I have if you would'nt mind taking a look. Its fairly cleanly written so its easy to follow. GJK.java contains the main method.
CollisionDetection.java contains all the collision detection and closest point code, I suspect my problem is in there somewhere, though it could also be in the support functions for all my shape types..
controls:
a,s,d,w - move the current shape
z,x - rotate current shape
q - change current shape
Link for the code + classes:
http://www.upshack.com/./uploaded-files/200611/GJKJava.zip
The FindPenetration method in that class is where I most need help.
(heres the method if you dont have time to download it)
Code:
static Vec2 FindPenetration(Shape s1, Shape s2)
{
final float iter = 8;
Vec2 diff = s2.position.Subtract(s1.position);
//diff.Normalise();
float ang = (float)Math.atan2(diff.y, diff.x);
Vec2 norm = new Vec2();
float minNorm = 100000;
for (int i=0;i<iter;i++)
{
Vec2 dir = new Vec2(ang + (float)((i-(iter/2))/iter * Math.PI * 0.25f));
//Vec2 dir = new Vec2(ang);
Vec2 p1 = s1.Support(dir);
Vec2 p2 = s2.Support(dir.Negate());
Vec2 s = p1.Subtract(p2);
float d = Vec2.Dot(s, dir);
if (d < minNorm)
{
norm = s;
minNorm = d;
lastPointA = p1;
lastPointB = p2;
}
}
return norm;
}