Updated Copyright year to 2013
[getmangos.git] / src / game / vmap / VMapTools.h
blob2e34235bf70a304809bbb3b8c94171ddc712a466
1 /*
2 * Copyright (C) 2005-2013 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef _VMAPTOOLS_H
20 #define _VMAPTOOLS_H
22 #include <G3D/CollisionDetection.h>
23 #include <G3D/AABox.h>
25 #include "NodeValueAccess.h"
27 /**
28 The Class is mainly taken from G3D/AABSPTree.h but modified to be able to use our internal data structure.
29 This is an iterator that helps us analysing the BSP-Trees.
30 The collision detection is modified to return true, if we are inside an object.
33 namespace VMAP
35 template<class TValue>
36 class IntersectionCallBack
38 public:
39 TValue* closestEntity;
40 G3D::Vector3 hitLocation;
41 G3D::Vector3 hitNormal;
43 void operator()(const G3D::Ray& ray, const TValue* entity, bool pStopAtFirstHit, float& distance)
45 entity->intersect(ray, distance, pStopAtFirstHit, hitLocation, hitNormal);
49 //==============================================================
50 //==============================================================
51 //==============================================================
53 class MyCollisionDetection
55 private:
56 public:
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)
66 // Integer representation of a floating-point value.
67 #define IR(x) (reinterpret_cast<G3D::uint32 const&>(x))
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);
74 // Find candidate planes.
75 for (int i = 0; i < 3; ++i)
77 if (origin[i] < MinB[i])
79 location[i] = MinB[i];
80 Inside = false;
82 // Calculate T distances to candidate planes
83 if (IR(dir[i]))
85 MaxT[i] = (MinB[i] - origin[i]) / dir[i];
88 else if (origin[i] > MaxB[i])
90 location[i] = MaxB[i];
91 Inside = false;
93 // Calculate T distances to candidate planes
94 if (IR(dir[i]))
96 MaxT[i] = (MaxB[i] - origin[i]) / dir[i];
101 if (Inside)
103 // definite hit
104 location = origin;
105 return true;
108 // Get largest of the maxT's for final choice of intersection
109 int WhichPlane = 0;
110 if (MaxT[1] > MaxT[WhichPlane])
112 WhichPlane = 1;
115 if (MaxT[2] > MaxT[WhichPlane])
117 WhichPlane = 2;
120 // Check final candidate actually inside box
121 if (IR(MaxT[WhichPlane]) & 0x80000000)
123 // Miss the box
124 return false;
127 for (int i = 0; i < 3; ++i)
129 if (i != WhichPlane)
131 location[i] = origin[i] + MaxT[WhichPlane] * dir[i];
132 if ((location[i] < MinB[i]) ||
133 (location[i] > MaxB[i]))
135 // On this plane we're outside the box extents, so
136 // we miss the box
137 return false;
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;
146 return true;
148 #undef IR
152 #endif