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
22 #include <G3D/CollisionDetection.h>
23 #include <G3D/AABox.h>
25 #include "NodeValueAccess.h"
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.
35 template<class TValue
>
36 class IntersectionCallBack
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
58 static bool collisionLocationForMovingPointFixedAABox(
59 const G3D::Vector3
& origin
,
60 const G3D::Vector3
& dir
,
61 const G3D::AABox
& box
,
62 G3D::Vector3
& location
,
66 // Integer representation of a floating-point value.
67 #define IR(x) (reinterpret_cast<G3D::uint32 const&>(x))
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
];
82 // Calculate T distances to candidate planes
85 MaxT
[i
] = (MinB
[i
] - origin
[i
]) / dir
[i
];
88 else if (origin
[i
] > MaxB
[i
])
90 location
[i
] = MaxB
[i
];
93 // Calculate T distances to candidate planes
96 MaxT
[i
] = (MaxB
[i
] - origin
[i
]) / dir
[i
];
108 // Get largest of the maxT's for final choice of intersection
110 if (MaxT
[1] > MaxT
[WhichPlane
])
115 if (MaxT
[2] > MaxT
[WhichPlane
])
120 // Check final candidate actually inside box
121 if (IR(MaxT
[WhichPlane
]) & 0x80000000)
127 for (int i
= 0; i
< 3; ++i
)
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
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;