first commit
[blok] / Box2D / Source / Collision / b2Collision.cpp
1 /*
2 * Copyright (c) 2007 Erin Catto http://www.gphysics.com
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty.  In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18
19 #include "b2Collision.h"
20
21 // Collision Detection in Interactive 3D Environments by Gino van den Bergen
22 // From Section 3.4.1
23 // x = mu1 * p1 + mu2 * p2
24 // mu1 + mu2 = 1 && mu1 >= 0 && mu2 >= 0
25 // mu1 = 1 - mu2;
26 // x = (1 - mu2) * p1 + mu2 * p2
27 //   = p1 + mu2 * (p2 - p1)
28 // x = s + a * r (s := start, r := end - start)
29 // s + a * r = p1 + mu2 * d (d := p2 - p1)
30 // -a * r + mu2 * d = b (b := s - p1)
31 // [-r d] * [a; mu2] = b
32 // Cramer's rule:
33 // denom = det[-r d]
34 // a = det[b d] / denom
35 // mu2 = det[-r b] / denom
36 bool b2Segment::TestSegment(float32* lambda, b2Vec2* normal, const b2Segment& segment, float32 maxLambda) const
37 {
38         b2Vec2 s = segment.p1;
39         b2Vec2 r = segment.p2 - s;
40         b2Vec2 d = p2 - p1;
41         b2Vec2 n = b2Cross(d, 1.0f);
42
43         const float32 k_slop = 100.0f * B2_FLT_EPSILON;
44         float32 denom = -b2Dot(r, n);
45
46         // Cull back facing collision and ignore parallel segments.
47         if (denom > k_slop)
48         {
49                 // Does the segment intersect the infinite line associated with this segment?
50                 b2Vec2 b = s - p1;
51                 float32 a = b2Dot(b, n);
52
53                 if (0.0f <= a && a <= maxLambda * denom)
54                 {
55                         float32 mu2 = -r.x * b.y + r.y * b.x;
56
57                         // Does the segment intersect this segment?
58                         if (-k_slop * denom <= mu2 && mu2 <= denom * (1.0f + k_slop))
59                         {
60                                 a /= denom;
61                                 n.Normalize();
62                                 *lambda = a;
63                                 *normal = n;
64                                 return true;
65                         }
66                 }
67         }
68
69         return false;
70 }
71
72