I've been reading this article on using Minkowski Differences for game collision, but unfortunately stuck on a particular part. I managed to create the Minkowski differenced AABB, and collision detection works on any side. But I fail to understand (intuitively) how to get the penetration vector of said collision.
Please note that this article uses the Y inverted, but I re-wrote the code and as I mentioned earlier, got collision detection working. Up in my implementation is +y.
Accoridng to this article:
QuoteQuite conveniently, the penetration vector is simply the minimum distance from the origin to the Minkowski-differenced resultant AABB...
I'm guessing he's referring to the distance from the origin in Minkowski Space to the Minkowski differenced box? I really can't seem to wrap my mind around this penetration vector code to be honest . He describes a function called closestPointOnBoundsToPoint, which does exactly that, but I'm not getting it. A Vector.zero is passed to the function, so in what situation do you pass something different? . If I can give a wild guess, I'm assuming it's due to the fact that if there's a collision, the Minkowski AABB will always surround the Minkowski space origin. I don't know if I'm right, I'm assuming.
Hopefully somebody can point me towards the right direction. Below I'll share the code I have so far in order to get the Minkowski differenced aabb. If anymore information or code is needed, let me know. Thank you very much in advance!
// AABB constructor arguments: center, size of AABB
// Btw, this particular example collides.
const a = new AABB (new Vec2(0, 0), new Vec2(1, 1));
const b = new AABB (new Vec2(2, 0), new Vec2(1, 1));
const diff = Vec2();
diff.x = a.min.x - b.max.x;
diff.y = a.min.y - b.max.y;
const size = new Vec2();
size.x = a.size.x + b.size.x;
size.y = a.size.y + b.size.y;
// Minkowski AABB
const md = new AABB();
md.center.x = (diff.x + size.x);
md.center.y = (diff.y + size.y);
md.size = size;
// Check For Collision
if (md.min.x <= 0 && md.min.y <= 0 &&
md.max.x >= 0 && md.max.y >= 0) {
// Collision, but needing the penetration vector, so I can push object out :(
}
else {
// No Colision.
}
Edit:
I tried visualizing the AABBs and noticed that the Minkowski AABB is a mirror of the point of collision but in some other space around the origin, let me call it Minkowski space for now.
I still don't understand how to get the penetration vector though. I'm guessing since the Minkowski AABB is a mirror of the point of collision, we can use the origin (Vector.zero) in Minkowski space to calculate the vector I can use as an offset. By the way, I'm sorry if I'm not using the right terminology.