# How to get a translation vector in a certain direction? SAT

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;
}

• Do you only have boxes? – Bálint Aug 17 '17 at 7:42
• no. I have convex shapes – Greg Williamson Aug 17 '17 at 7:49

## 1 Answer

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.

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