root/trunk/src/shared/vmap/VMapTools.h @ 8

Revision 2, 4.6 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2* Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3*
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.
8*
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.
13*
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
17*/
18
19#ifndef _VMAPTOOLS_H
20#define _VMAPTOOLS_H
21
22#include <G3D/CollisionDetection.h>
23#include <G3D/AABox.h>
24
25#include "NodeValueAccess.h"
26
27/**
28The Class is mainly taken from G3D/AABSPTree.h but modified to be able to use our internal data structure.
29This is an iterator that helps us analysing the BSP-Trees.
30The collision detection is modified to return true, if we are inside an object.
31*/
32
33namespace VMAP
34{
35    template<class TValue> 
36    class IntersectionCallBack {
37    public:
38        TValue*      closestEntity;
39        G3D::Vector3 hitLocation;
40        G3D::Vector3 hitNormal;
41
42        void operator()(const G3D::Ray& ray, const TValue* entity, bool pStopAtFirstHit, float& distance) {
43            entity->intersect(ray, distance, pStopAtFirstHit, hitLocation, hitNormal);
44        }
45    };
46
47    //==============================================================
48    //==============================================================
49    //==============================================================
50
51    class MyCollisionDetection
52    {
53    private:
54    public:
55
56        static bool collisionLocationForMovingPointFixedAABox(
57            const G3D::Vector3&     origin,
58            const G3D::Vector3&     dir,
59            const G3D::AABox&       box,
60            G3D::Vector3&           location,
61            bool&                   Inside)
62        {
63
64            // Integer representation of a floating-point value.
65#define IR(x)   ((G3D::uint32&)x)
66
67            Inside = true;
68            const G3D::Vector3& MinB = box.low();
69            const G3D::Vector3& MaxB = box.high();
70            G3D::Vector3 MaxT(-1.0f, -1.0f, -1.0f);
71
72            // Find candidate planes.
73            for (int i = 0; i < 3; ++i)
74            {
75                if (origin[i] < MinB[i])
76                {
77                    location[i] = MinB[i];
78                    Inside      = false;
79
80                    // Calculate T distances to candidate planes
81                    if (IR(dir[i]))
82                    {
83                        MaxT[i] = (MinB[i] - origin[i]) / dir[i];
84                    }
85                }
86                else if (origin[i] > MaxB[i])
87                {
88                    location[i] = MaxB[i];
89                    Inside      = false;
90
91                    // Calculate T distances to candidate planes
92                    if (IR(dir[i]))
93                    {
94                        MaxT[i] = (MaxB[i] - origin[i]) / dir[i];
95                    }
96                }
97            }
98
99            if (Inside)
100            {
101                // definite hit
102                location = origin;
103                return true;
104            }
105
106            // Get largest of the maxT's for final choice of intersection
107            int WhichPlane = 0;
108            if (MaxT[1] > MaxT[WhichPlane])
109            {
110                WhichPlane = 1;
111            }
112
113            if (MaxT[2] > MaxT[WhichPlane])
114            {
115                WhichPlane = 2;
116            }
117
118            // Check final candidate actually inside box
119            if (IR(MaxT[WhichPlane]) & 0x80000000)
120            {
121                // Miss the box
122                return false;
123            }
124
125            for (int i = 0; i < 3; ++i)
126            {
127                if (i != WhichPlane)
128                {
129                    location[i] = origin[i] + MaxT[WhichPlane] * dir[i];
130                    if ((location[i] < MinB[i]) ||
131                        (location[i] > MaxB[i]))
132                    {
133                        // On this plane we're outside the box extents, so
134                        // we miss the box
135                        return false;
136                    }
137                }
138            }
139            /*
140            // Choose the normal to be the plane normal facing into the ray
141            normal = G3D::Vector3::zero();
142            normal[WhichPlane] = (dir[WhichPlane] > 0) ? -1.0 : 1.0;
143            */
144            return true;
145
146#undef IR
147        }
148    };
149}
150#endif
Note: See TracBrowser for help on using the browser.