• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Original (more pedagogical) intersection tests ulfben   Sep 23rd, 2016 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. /* the original code I got from Tommi / Jerry - saving here for future reference,
2. and for students to read as this is perhaps more student-friendly in its use of
3. local variables and structuring of the if/else-statements.
4.
5. My refactored version is here: http://pastebin.com/dxTU7Asv
6.   */
7.
8. bool CollisionManager::Check(Collider* lhs, Collider* rhs, int& overlapX, int& overlapY)
9. {
10.     // we use something called Separating Axis Theorem
11.     //   link: en.wikipedia.org/wiki/Hyperplane_separation_theorem#Use_in_collision_detection
12.
13.     // we use SAT to check if two axis aligned boxes (the shape can basically be any polygon
14.     // with SAT) are overlapping, and we check one axis at a time:
15.     // if one axis does not overlap (i e separates) we have no collision
16.     //
17.     //               .> ,-------------.
18.     //               |  |             |
19.     //   halfHeightA |  |             |
20.     //               |  |      A      |                              <.
21.     //               `> |           ,---------.  <.                   |
22.     //                  |           |         |   | halfHeightB       |  deltaCenterY
23.     //                  `-----------|    B    |  <´                  <´
24.     //                              |         |
25.     //                              `---------´
26.     //                  ^------^         ^----^
27.     //                  halfWidthA       halfWidthB
28.     //
29.     //                         ^---------^
30.     //                         deltaCenterX
31.     //
32.     //  if deltaCenterX < (halfWidthA + halfWidthB)
33.     //     if deltaCenterY < (halfHeightA + halfHeightB)
34.     //       handle collision
35.     //     endif
36.     //  endif
37.     //
38.
39.
40.     overlapX = 0;
41.     overlapY = 0;
42.
43.     // find center of colliders
44.     int lhsHalfWidth = lhs->GetWidth() / 2;
45.     int rhsHalfWidth = rhs->GetWidth() / 2;
46.     int lhsCenterX = lhs->GetX() + lhsHalfWidth;
47.     int rhsCenterX = rhs->GetX() + rhsHalfWidth;
48.     int centerDeltaX = lhsCenterX - rhsCenterX;
49.
50.     // first we check if the overlap in the x-axis
51.     int halfWidth = (lhsHalfWidth + rhsHalfWidth);
52.     if (abs(centerDeltaX) < halfWidth)
53.     {
54.         int lhsHalfHeight = lhs->GetHeight() / 2;
55.         int rhsHalfHeight = rhs->GetHeight() / 2;
56.         int lhsCenterY = lhs->GetY() + lhsHalfHeight;
57.         int rhsCenterY = rhs->GetY() + rhsHalfHeight;
58.         int centerDeltaY = lhsCenterY - rhsCenterY;
59.
60.         // then check for overlap in y-axis
61.         int halfHeight = (lhsHalfHeight + rhsHalfHeight);
62.         if (abs(centerDeltaY) < halfHeight)
63.         {
64.             int deltaX = halfWidth - abs(centerDeltaX);
65.             int deltaY = halfHeight - abs(centerDeltaY);
66.
67.             // now we find the smallest of the two overlap in axis
68.             if (deltaY < deltaX)
69.             {
70.                 overlapY = deltaY;
71.                 if (centerDeltaY < 0)
72.                     overlapY = -overlapY;
73.             }
74.             else if (deltaY > deltaX)
75.             {
76.                 overlapX = deltaX;
77.                 if (centerDeltaX < 0)
78.                     overlapX = -overlapX;
79.             }
80.             else
81.             {
82.                 if (centerDeltaX < 0)
83.                     overlapX = -deltaX;
84.                 else
85.                     overlapX = deltaX;
86.
87.
88.                 if (centerDeltaY < 0)
89.                     overlapY = -deltaY;
90.                 else
91.                     overlapY = deltaY;
92.
93.             }
94.             return true;
95.         }
96.     }
97.
98.     return false;
99. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top