Advertisement
Guest User

Untitled

a guest
Oct 14th, 2016
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. //returns the minimum distance required to move p2 out of p1. Will be +/- depending if it needs to be moved up/down.
  2. //if p1 and p2 are not overlapping it returns 0
  3. float displacement = p1.getDisplacement(p2);
  4.  
  5. if (!displacement)
  6. return false;
  7. else
  8. {
  9. if (abs(displacement) <= abs(smallestDisplacement)) //displacement can be +/- so we must compare abs value
  10. {
  11. smallestDisplacement = displacement;
  12. smallestAxis = axis;
  13. }
  14. }
  15.  
  16.  
  17. 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.
  18.  
  19. Here's the implementation:
  20.  
  21. float Projection::getDisplacement(Projection other)
  22. {
  23. if (min > other.max || other.min > max) //tests if
  24. return 0;
  25. else
  26. {
  27. float mid = (max+min)/2;
  28. if (abs(mid-max) > abs(mid-min))
  29. return max - other.min;
  30. else
  31. return other.max - min;
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement