Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / CDTestFramework / Opcode / Ice / IceRay.cpp
1 /*
2  *      ICE / OPCODE - Optimized Collision Detection
3  * http://www.codercorner.com/Opcode.htm
4  * 
5  * Copyright (c) 2001-2008 Pierre Terdiman,  pierre@codercorner.com
6
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:
12
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.
16 */
17 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
18 /**
19  *      Contains code for rays.
20  *      \file           IceRay.cpp
21  *      \author         Pierre Terdiman
22  *      \date           April, 4, 2000
23  */
24 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
25
26 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
27 /**
28  *      Ray class.
29  *      A ray is a half-line P(t) = mOrig + mDir * t, with 0 <= t <= +infinity
30  *      \class          Ray
31  *      \author         Pierre Terdiman
32  *      \version        1.0
33  */
34 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
35
36 /*
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
43         R = Reflexion vector
44
45               ^y
46               |
47               |
48               |
49        _  _  _| _ _ _
50        *      *      *|
51         \     |     /
52          \    |N   /  |
53          R\   |   /D
54            \  |  /    |
55             \ | /
56     _________\|/______*_______>x
57                O    T
58
59         Let define theta = angle between D and N. Then cos(theta) = |N| / |D| = |N| since D is normalized.
60
61         j|D = |j|*|D|*cos(theta) => |N| = j|D
62
63         Then we simply have:
64
65         D = N + T
66
67         To compute tangential reaction :
68
69         T = D - N
70
71         To compute reflexion vector :
72
73         R = N - T = N - (D-N) = 2*N - D
74 */
75
76 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
77 // Precompiled Header
78 #include "Stdafx.h"
79
80 using namespace Opcode;
81
82 float Ray::SquareDistance(const Point& point, float* t) const
83 {
84         Point Diff = point - mOrig;
85         float fT = Diff | mDir;
86
87         if(fT<=0.0f)
88         {
89                 fT = 0.0f;
90         }
91         else
92         {
93                 fT /= mDir.SquareMagnitude();
94                 Diff -= fT*mDir;
95         }
96
97         if(t) *t = fT;
98
99         return Diff.SquareMagnitude();
100 }