a guest Oct 14th, 2016 131 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- //returns the minimum distance required to move p2 out of p1. Will be +/- depending if it needs to be moved up/down.
- //if p1 and p2 are not overlapping it returns 0
- float displacement = p1.getDisplacement(p2);
- if (!displacement)
- return false;
- if (abs(displacement) <= abs(smallestDisplacement)) //displacement can be +/- so we must compare abs value
- smallestDisplacement = displacement;
- smallestAxis = axis;
- Getting the displacement is pretty easy. First you get the midpoint of p1. If the p2.max is closer to the midpoint, you want to move the interval down. If p2.min is closer to the midpoint, you want to move the interval up.
- Here's the implementation:
- float Projection::getDisplacement(Projection other)
- if (min > other.max || other.min > max) //tests if
- return 0;
- float mid = (max+min)/2;
- if (abs(mid-max) > abs(mid-min))
- return max - other.min;
- return other.max - min;
RAW Paste Data