2 * ICE / OPCODE - Optimized Collision Detection
3 * http://www.codercorner.com/Opcode.htm
5 * Copyright (c) 2001-2008 Pierre Terdiman, pierre@codercorner.com
7 This software is provided 'as-is', without any express or implied warranty.
8 In no event will the authors be held liable for any damages arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it freely,
11 subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
15 3. This notice may not be removed or altered from any source distribution.
17 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
19 * Contains code for rays.
21 * \author Pierre Terdiman
22 * \date April, 4, 2000
24 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
29 * A ray is a half-line P(t) = mOrig + mDir * t, with 0 <= t <= +infinity
31 * \author Pierre Terdiman
34 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
37 O = Origin = impact point
38 i = normalized vector along the x axis
39 j = normalized vector along the y axis = actually the normal vector in O
40 D = Direction vector, norm |D| = 1
41 N = Projection of D on y axis, norm |N| = normal reaction
42 T = Projection of D on x axis, norm |T| = tangential reaction
56 _________\|/______*_______>x
59 Let define theta = angle between D and N. Then cos(theta) = |N| / |D| = |N| since D is normalized.
61 j|D = |j|*|D|*cos(theta) => |N| = j|D
67 To compute tangential reaction :
71 To compute reflexion vector :
73 R = N - T = N - (D-N) = 2*N - D
76 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
80 using namespace Opcode;
82 float Ray::SquareDistance(const Point& point, float* t) const
84 Point Diff = point - mOrig;
85 float fT = Diff | mDir;
93 fT /= mDir.SquareMagnitude();
99 return Diff.SquareMagnitude();