# Implementing Separating Axis Theorem (SAT) and Minimum Translation Vector (MTV)

I was following codezealot's tutorial on SAT and MTV and trying to implement it myself but I've come a cropper when it comes to getting the correct MTV. Here is my example: (Cue pretty pictures...)

I'm well aware how to obtain the length of the MTV and the axis on which it lies. However I can't work out whether the length should be 'positive' or 'negative' to push the object in the correct direction.

In the example we are separating the objects by moving the 'red' of the 'blue', the top example is moving the object negatively and the bottom moving it positively.

Here is my actual implementation.

• I was looking for similar questions. They're all by you. Been a tricky problem eh? Commented Apr 17, 2012 at 23:43
• I don't suppose you still have that code? I'm trying to figure out how you can get an MTV with SAT, and there doesn't seem to be a single instance of actual, working code. Yes, I'm counting the tutorial you linked, because it's glossing over some very important details.
– anon
Commented Dec 12, 2017 at 5:27

The solution is to simply turn around the displacement vector (multiply it by -1) if it's pointing towards the shape from which the object needs to be pushed away.

To find out if the displacement vector is pointing towards the shape, you first have to get the general direction from object a to object b by subtracting their centers from each other. After that, you check the dot product between the displacement vector and the direction(a,b) vector that you just created. If it's > 0, the displacement vector and direction(a,b) are pointing in the same direction, hence you need to flip the displacement vector.

Well I worked out a way but I'm not sure if it's the best way:

What I did was once I'd found the smallest overlap is check if x1 < x2 if so the length must be positive else the length must be negative.

It's worked so far and given me satisfactory results.

• This is what I found myself having to do: I compared the positions of the two bodies along the axes checked to see if A's position was in the positive or negative direction from B along that axis, and adjust the sign of the displacement accordingly. Commented Apr 18, 2012 at 21:52
• Last 2 days of problems... solved! I don't know why the dot product solution didn't work because I've seen it used in multiple places... but this solution seems to be working!! Thank you :) Commented Jun 19, 2020 at 12:17

From what is understood, this overlap value should always be positive. The actual direction of the force should be derived from the projection vector (the MTV axis).

Note the interactive example on the page referenced by codezealot (figure 5): http://www.metanetsoftware.com/technique/tutorialA.html

It seems like the actual direction is the MTV axis pointed by where the centers of the objects meet along the axis. Figure 5 of that tutorial is the best illustrator of this. Move the object around and watch the centers of the objects vs an invisible line drawn parallel with the axis from the center of both objects. The direction of the vectors change when the centers cross that line.

• Is MVT different from MTV? Commented Apr 18, 2012 at 12:37
• No, just the order of the letters. Editing. Commented Apr 19, 2012 at 0:24