1
\$\begingroup\$

I'm using the seperating axis thereom for discrete collision detection. I have code to find the minnimum translation vector (or distance) to seperate two shapes but how can I adjust it to get a vector in a certain direction that seperates the shapes? For example: If a box A is overaping box B 10 pixels horizontaly and 40 pixels vertically the MTV would calculate to move the box horizontally 10 px or so but I want to seperate the boxes vertically (move 40px up). How can I adjust my code to calculate a MTV in a direction of my choosing?

    const Vector2 ShapeImpl::CalcDisplacement(const AxesVec &axes, const Shape &a, const Shape &b) const
{
    Vector2 displacement;
    Precision_t Overlap = std::numeric_limits<Precision_t>::infinity();
    Axis smallest;

    for (auto && axis : axes)
    {
        const Projection pA = b.Project(axis);
        const Projection pB = a.Project(axis);

        // No Collision
        if (!pA.IsOverlap(pB))
            return Vector2(0, 0);

        else
        {
            const Precision_t o = pA.GetOverlap(pB);

            if (std::abs(o) < std::abs(Overlap))
            {
                Overlap = o;
                smallest = axis;
            }
        }

}

    displacement = smallest * Overlap;

    return displacement;
}
\$\endgroup\$
  • \$\begingroup\$ Do you only have boxes? \$\endgroup\$ – Bálint Aug 17 '17 at 7:42
  • \$\begingroup\$ no. I have convex shapes \$\endgroup\$ – Greg Williamson Aug 17 '17 at 7:49
1
\$\begingroup\$

When you know there's a collision, you can just use an arbitary axis (e.g. (0, 1) if you want to separate them upwards), then project the points onto this axis. The distance you need to move the objects to separate them in this direction is the overlap between the projections.

\$\endgroup\$
  • \$\begingroup\$ I tried that but it didnt seem to work. I'm proabably doing it wrong but not sure. \$\endgroup\$ – Greg Williamson Aug 17 '17 at 8:00
  • \$\begingroup\$ @GregWilliamson What do you mean by "doesn't work"? \$\endgroup\$ – Bálint Aug 17 '17 at 8:32
  • \$\begingroup\$ It returned a vector with the wrong magnitude. \$\endgroup\$ – Greg Williamson Aug 17 '17 at 8:40
  • \$\begingroup\$ @GregWilliamson Can you link a pastebin? \$\endgroup\$ – Bálint Aug 17 '17 at 9:57
  • \$\begingroup\$ Extracting only the releveant parts into a pastebin would take me days. \$\endgroup\$ – Greg Williamson Aug 17 '17 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.