2 * Copyright (c) 2007 Erin Catto http://www.gphysics.com
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.
19 #include "b2Collision.h"
21 // Collision Detection in Interactive 3D Environments by Gino van den Bergen
23 // x = mu1 * p1 + mu2 * p2
24 // mu1 + mu2 = 1 && mu1 >= 0 && mu2 >= 0
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
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
38 b2Vec2 s = segment.p1;
39 b2Vec2 r = segment.p2 - s;
41 b2Vec2 n = b2Cross(d, 1.0f);
43 const float32 k_slop = 100.0f * B2_FLT_EPSILON;
44 float32 denom = -b2Dot(r, n);
46 // Cull back facing collision and ignore parallel segments.
49 // Does the segment intersect the infinite line associated with this segment?
51 float32 a = b2Dot(b, n);
53 if (0.0f <= a && a <= maxLambda * denom)
55 float32 mu2 = -r.x * b.y + r.y * b.x;
57 // Does the segment intersect this segment?
58 if (-k_slop * denom <= mu2 && mu2 <= denom * (1.0f + k_slop))