1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef _VMAPTOOLS_H |
---|
22 | #define _VMAPTOOLS_H |
---|
23 | |
---|
24 | #include <G3D/CollisionDetection.h> |
---|
25 | #include <G3D/AABox.h> |
---|
26 | |
---|
27 | #include "NodeValueAccess.h" |
---|
28 | |
---|
29 | /** |
---|
30 | The Class is mainly taken from G3D/AABSPTree.h but modified to be able to use our internal data structure. |
---|
31 | This is an iterator that helps us analysing the BSP-Trees. |
---|
32 | The collision detection is modified to return true, if we are inside an object. |
---|
33 | */ |
---|
34 | |
---|
35 | namespace VMAP |
---|
36 | { |
---|
37 | template<class TValue> |
---|
38 | class IntersectionCallBack { |
---|
39 | public: |
---|
40 | TValue* closestEntity; |
---|
41 | G3D::Vector3 hitLocation; |
---|
42 | G3D::Vector3 hitNormal; |
---|
43 | |
---|
44 | void operator()(const G3D::Ray& ray, const TValue* entity, bool pStopAtFirstHit, float& distance) { |
---|
45 | entity->intersect(ray, distance, pStopAtFirstHit, hitLocation, hitNormal); |
---|
46 | } |
---|
47 | }; |
---|
48 | |
---|
49 | //============================================================== |
---|
50 | //============================================================== |
---|
51 | //============================================================== |
---|
52 | |
---|
53 | class MyCollisionDetection |
---|
54 | { |
---|
55 | private: |
---|
56 | public: |
---|
57 | |
---|
58 | static bool collisionLocationForMovingPointFixedAABox( |
---|
59 | const G3D::Vector3& origin, |
---|
60 | const G3D::Vector3& dir, |
---|
61 | const G3D::AABox& box, |
---|
62 | G3D::Vector3& location, |
---|
63 | bool& Inside) |
---|
64 | { |
---|
65 | |
---|
66 | // Integer representation of a floating-point value. |
---|
67 | #define IR(x) ((G3D::uint32&)x) |
---|
68 | |
---|
69 | Inside = true; |
---|
70 | const G3D::Vector3& MinB = box.low(); |
---|
71 | const G3D::Vector3& MaxB = box.high(); |
---|
72 | G3D::Vector3 MaxT(-1.0f, -1.0f, -1.0f); |
---|
73 | |
---|
74 | // Find candidate planes. |
---|
75 | for (int i = 0; i < 3; ++i) |
---|
76 | { |
---|
77 | if (origin[i] < MinB[i]) |
---|
78 | { |
---|
79 | location[i] = MinB[i]; |
---|
80 | Inside = false; |
---|
81 | |
---|
82 | // Calculate T distances to candidate planes |
---|
83 | if (IR(dir[i])) |
---|
84 | { |
---|
85 | MaxT[i] = (MinB[i] - origin[i]) / dir[i]; |
---|
86 | } |
---|
87 | } |
---|
88 | else if (origin[i] > MaxB[i]) |
---|
89 | { |
---|
90 | location[i] = MaxB[i]; |
---|
91 | Inside = false; |
---|
92 | |
---|
93 | // Calculate T distances to candidate planes |
---|
94 | if (IR(dir[i])) |
---|
95 | { |
---|
96 | MaxT[i] = (MaxB[i] - origin[i]) / dir[i]; |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | if (Inside) |
---|
102 | { |
---|
103 | // definite hit |
---|
104 | location = origin; |
---|
105 | return true; |
---|
106 | } |
---|
107 | |
---|
108 | // Get largest of the maxT's for final choice of intersection |
---|
109 | int WhichPlane = 0; |
---|
110 | if (MaxT[1] > MaxT[WhichPlane]) |
---|
111 | { |
---|
112 | WhichPlane = 1; |
---|
113 | } |
---|
114 | |
---|
115 | if (MaxT[2] > MaxT[WhichPlane]) |
---|
116 | { |
---|
117 | WhichPlane = 2; |
---|
118 | } |
---|
119 | |
---|
120 | // Check final candidate actually inside box |
---|
121 | if (IR(MaxT[WhichPlane]) & 0x80000000) |
---|
122 | { |
---|
123 | // Miss the box |
---|
124 | return false; |
---|
125 | } |
---|
126 | |
---|
127 | for (int i = 0; i < 3; ++i) |
---|
128 | { |
---|
129 | if (i != WhichPlane) |
---|
130 | { |
---|
131 | location[i] = origin[i] + MaxT[WhichPlane] * dir[i]; |
---|
132 | if ((location[i] < MinB[i]) || |
---|
133 | (location[i] > MaxB[i])) |
---|
134 | { |
---|
135 | // On this plane we're outside the box extents, so |
---|
136 | // we miss the box |
---|
137 | return false; |
---|
138 | } |
---|
139 | } |
---|
140 | } |
---|
141 | /* |
---|
142 | // Choose the normal to be the plane normal facing into the ray |
---|
143 | normal = G3D::Vector3::zero(); |
---|
144 | normal[WhichPlane] = (dir[WhichPlane] > 0) ? -1.0 : 1.0; |
---|
145 | */ |
---|
146 | return true; |
---|
147 | |
---|
148 | #undef IR |
---|
149 | } |
---|
150 | }; |
---|
151 | } |
---|
152 | #endif |
---|