Search code examples
collisionseparating-axis-theorem

Separation Axis Theorem MVT along only one axis


I am having trouble calculating 3D penetration vector along one axis. I already implemented SAT and it works. I want to calculate how much i need to offset first box from other so it will always sit on top of other. Kind of doing simple box cast with very long box.

How should i proceed with finding offset which would push one object in direction of specified axis.

enter image description here


Solution

  • The first part of this you should already know; when you project each shape onto each axis, there should be some min and max scalar value for shape A, let's say AMIN and AMAX, and the same for shape B (BMIN / BMAX).

    If the objects are apparently colliding on an axis, their projections will overlap, meaning either AMIN < BMIN < AMAX < BMAX or BMIN < AMIN < BMAX < AMAX. Let's assume the first.

    The value of AMAX-BMIN is the distance needed to move either shape to bring them into touching contact, and the axis being tested gives you the direction.

    Usually, as one iterates through all of the axes, one tracks the minimum value and its corresponding axis, and that becomes the vector needed to un-collide the shapes. (Usually called the 'minimum displacement vector' if you want to Google it.)

    For you, wanting to displace them in a specific direction, you would just store the value corresponding to that specific axis, and that becomes your displacement vector (which would then be added to one shape's position to separate them).

    I highly recommend Googling "minimum displacement vector sat" and checking out the first few links, specifically this one: http://www.dyn4j.org/2010/01/sat/. It's a bit dense, but it's where I learned everything I know about SAT.

    EDIT And...I missed a piece. This is kinda rough, but if you're wanting to displace the shapes along one axis (vertical in your example), based on a displacement vector gained from another axis (the normal of the long side of the bottom box), you need to project the displacement vector onto the desired (normalized) axis (using dot product) to get the proper distance, then combined with your desired axis.