Thanks for the help, Its so close to being complete, but im having a bit of a problem, and Im not sure if its because of GJK itself or somthing else. Here is a picture showing the problem
[img=http://img221.imageshack.us/img221/9305/proofje1.th.jpg]
The blue and red shapes are the two object being checked.
The yellow shape is the minkowski difference without margins.
The grayish pink shape is the minkowski difference with margins added.
The purple is the last simplex used in GJK.
The red line *is supposed* to go from the origin to the nearest point on the minkowski difference, and it is not. That is the bug. It works by finding the distance to the last simplex used by GJK, so im guessing since the last simplex does not contain the whole edge of the minkowski difference, it must be a bug with my GJK implementation, or it could be that 'degeneracy' you talked about in your previous post.
Does this look like a bug with my GJK or is this the degeneracy?
Here is my GJK code as well:
Code:
static Vec2[] Collide(Shape s1, Shape s2)
{
Vec2 d = s2.position.Subtract(s1.position);
d.Normalise();
//GJK initialise
Vec2 s = Support(s1,s2,d);
Vec2 simplex[] = new Vec2[3];
byte simpSize = 1;
simplex[0] = new Vec2(s);
d = s.Negate();
Vec2 a;
boolean collide = true;
//4 iterations
for (int i=0;i<numIterations;i++)
{
a = Support(s1, s2, d);
//Potential non - collide
if (collide && Vec2.Dot(a,d) < 0)
collide = false;
simplex[simpSize] = new Vec2(a);
simpSize ++;
//simplex has two points
if (simpSize == 2)
{
Vec2 o = simplex[1].Negate();
Vec2 ab = simplex[0].Subtract(simplex[1]);
if (Vec2.Dot(ab, o) > 0)
{
d = Vec2.Cross( Vec2.Cross(ab, o), ab);
//d.Normalise();
Vec2 temp = new Vec2(simplex[1]);
simplex[1] = new Vec2(simplex[0]);
simplex[0] = temp;
}
else
{
simplex[0] = new Vec2(simplex[1]);
simpSize --;
d = new Vec2(o);
//d.Normalise();
}
}
//simplex has three points
else if (simpSize == 3)
{
Vec2 o = simplex[2].Negate();
Vec2 ab = simplex[1].Subtract(simplex[2]);
Vec2 ac = simplex[0].Subtract(simplex[2]);
float abc = Vec2.Cross(ab, ac);
if (Vec2.Dot(Vec2.Cross(abc, ac), o) > 0)
{
if (Vec2.Dot(ac, o) > 0)
{
simplex[1] = new Vec2(simplex[0]);
simplex[0] = new Vec2(simplex[2]);
simpSize --;
d = Vec2.Cross(abc, ac);
//d.Normalise();
}
else if (Vec2.Dot(ab, o) > 0)
{
simplex[0] = new Vec2(simplex[2]);
simpSize --;
d = Vec2.Cross(abc, ab);
//d.Normalise();
}
else
{
simplex[0] = new Vec2(simplex[2]);
simpSize = 1;
d = new Vec2(o);
//d.Normalise();
}
}
else
{
if (Vec2.Dot(Vec2.Cross(ab, abc), o) > 0)
{
if (Vec2.Dot(ab, o) > 0)
{
simplex[0] = new Vec2(simplex[2]);
simpSize --;
d = Vec2.Cross(ab, abc);
//d.Normalise();
}
else
{
simplex[0] = new Vec2(simplex[2]);
simpSize = 1;
d = new Vec2(o);
//d.Normalise();
}
}
else
{
lastSimplex = simplex;
if (collide)
return simplex;
return null;
}
}
}
}
lastSimplex = simplex;
return null;
}