TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
VMapTools.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef _VMAPTOOLS_H
20 #define _VMAPTOOLS_H
21 
22 #include <G3D/CollisionDetection.h>
23 #include <G3D/AABox.h>
24 
25 #include "Define.h"
26 
27 #include "NodeValueAccess.h"
28 
35 namespace VMAP
36 {
37  template<class TValue>
39  public:
40  TValue* closestEntity;
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 
54  {
55  private:
56  public:
57 
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) (reinterpret_cast<G3D::uint32 const&>(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
Definition: VMapTools.h:53
static bool collisionLocationForMovingPointFixedAABox(const G3D::Vector3 &origin, const G3D::Vector3 &dir, const G3D::AABox &box, G3D::Vector3 &location, bool &Inside)
Definition: VMapTools.h:58
const Point3 & low() const
Definition: AABox.h:136
#define IR(x)
Definition: VMapTools.h:38
Definition: IVMapManager.h:31
double distance(double x, double y)
Definition: g3dmath.h:731
Definition: Vector3.h:58
G3D::Vector3 hitNormal
Definition: VMapTools.h:42
G3D::Vector3 hitLocation
Definition: VMapTools.h:41
Definition: Ray.h:24
Definition: AABox.h:32
#define TC_COMMON_API
Definition: Define.h:116
void operator()(const G3D::Ray &ray, const TValue *entity, bool pStopAtFirstHit, float &distance)
Definition: VMapTools.h:44
const Point3 & high() const
Definition: AABox.h:141
TValue * closestEntity
Definition: VMapTools.h:40