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...)

enter image description here

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.

Please help I'm really struggling with this.

Here is my actual implementation.

  • 1
    \$\begingroup\$ I was looking for similar questions. They're all by you. Been a tricky problem eh? \$\endgroup\$
    – House
    Apr 17 '12 at 23:43
  • \$\begingroup\$ 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. \$\endgroup\$
    – Nic
    Dec 12 '17 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.

  • \$\begingroup\$ 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. \$\endgroup\$ Apr 18 '12 at 21:52
  • \$\begingroup\$ 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 :) \$\endgroup\$ Jun 19 '20 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.

  • \$\begingroup\$ Is MVT different from MTV? \$\endgroup\$ Apr 18 '12 at 12:37
  • \$\begingroup\$ No, just the order of the letters. Editing. \$\endgroup\$ Apr 19 '12 at 0:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .