1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
inline plane createPlaneNormal (const vector &_point, const vector &_normal)
{
return plane (_normal, -_normal.x * _point.x - _normal.y * _point.y - _normal.z * _point.z);
}
inline plane PlaneTransform (const plane& _plane, const matrix& _matrix)
{
float a = _plane.x * _matrix._11 + _plane.y * _matrix._21 + _plane.z * _matrix._31;
float b = _plane.x * _matrix._12 + _plane.y * _matrix._22 + _plane.z * _matrix._32;
float c = _plane.x * _matrix._13 + _plane.y * _matrix._23 + _plane.z * _matrix._33;
return plane(a, b, c, _plane.distance - (a * _matrix._41 + b * _matrix._42 + c * _matrix._43));
}
inline void ComputeBoxPlanes (const vector& boxMin, const vector& boxMax, const matrix& boxTrans, plane* out)
{
out[0] = createPlaneNormal (boxMin, vector(-1.0f, 0.0f, 0.0f)); // Linke Ebene
out[1] = createPlaneNormal (boxMax, vector( 1.0f, 0.0f, 0.0f)); // Rechte Ebene
out[2] = createPlaneNormal (boxMax, vector( 0.0f, 1.0f, 0.0f)); // Obere Ebene
out[3] = createPlaneNormal (boxMin, vector( 0.0f, -1.0f, 0.0f)); // Untere Ebene
out[4] = createPlaneNormal (boxMin, vector( 0.0f, 0.0f, -1.0f)); // Vordere Ebene
out[5] = createPlaneNormal (boxMax, vector( 0.0f, 0.0f, 1.0f)); // Hintere Ebene
for(int i=0; i<6; ++i)
{
out[i] = PlaneTransform(out[i], boxTrans);
}
}
inline float dotPlaneProductCoords (plane _plane, const vector &_vector)
{
return (_plane.x * _vector.x + _plane.y * _vector.y + _plane.z * _vector.z) + _plane.distance;
}
inline bool PointHitsBox (const vector& point, const plane* boxPlanes)
{
if(dotPlaneProductCoords (boxPlanes[0], point) > 0.0001f) return (false);
if(dotPlaneProductCoords (boxPlanes[1], point) > 0.0001f) return (false);
if(dotPlaneProductCoords (boxPlanes[2], point) > 0.0001f) return (false);
if(dotPlaneProductCoords (boxPlanes[3], point) > 0.0001f) return (false);
if(dotPlaneProductCoords (boxPlanes[4], point) > 0.0001f) return (false);
return (dotPlaneProductCoords (boxPlanes[5], point) <= 0.0001f);
}
inline bool PointHitsBox (const vector& point, const vector& boxMin, const vector& boxMax, const matrix& boxTrans)
{
plane BoxPlane[6];
ComputeBoxPlanes(boxMin, boxMax, boxTrans, BoxPlane);
return PointHitsBox(point, BoxPlane);
}
|