Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
VMapTools.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#ifndef _VMAPTOOLS_H
7#define _VMAPTOOLS_H
8
9#include <G3D/CollisionDetection.h>
10#include <G3D/AABox.h>
11
12#include "NodeValueAccess.h"
13
19
20namespace VMAP
21{
22 template<class TValue>
24 public:
26 G3D::Vector3 hitLocation;
27 G3D::Vector3 hitNormal;
28
29 void operator()(const G3D::Ray& ray, const TValue* entity, bool pStopAtFirstHit, float& distance) {
30 entity->intersect(ray, distance, pStopAtFirstHit, hitLocation, hitNormal);
31 }
32 };
33
34 //==============================================================
35 //==============================================================
36 //==============================================================
37
39 {
40 public:
42 const G3D::Vector3& origin,
43 const G3D::Vector3& dir,
44 const G3D::AABox& box,
45 G3D::Vector3& location,
46 bool& Inside)
47 {
48 // Integer representation of a floating-point value.
49#define IR(x) (reinterpret_cast<G3D::uint32 const&>(x))
50
51 Inside = true;
52 const G3D::Vector3& MinB = box.low();
53 const G3D::Vector3& MaxB = box.high();
54 G3D::Vector3 MaxT(-1.0f, -1.0f, -1.0f);
55
56 // Find candidate planes.
57 for (int i = 0; i < 3; ++i)
58 {
59 if (origin[i] < MinB[i])
60 {
61 location[i] = MinB[i];
62 Inside = false;
63
64 // Calculate T distances to candidate planes
65 if (IR(dir[i]))
66 {
67 MaxT[i] = (MinB[i] - origin[i]) / dir[i];
68 }
69 }
70 else if (origin[i] > MaxB[i])
71 {
72 location[i] = MaxB[i];
73 Inside = false;
74
75 // Calculate T distances to candidate planes
76 if (IR(dir[i]))
77 {
78 MaxT[i] = (MaxB[i] - origin[i]) / dir[i];
79 }
80 }
81 }
82
83 if (Inside)
84 {
85 // definite hit
86 location = origin;
87 return true;
88 }
89
90 // Get largest of the maxT's for final choice of intersection
91 int WhichPlane = 0;
92 if (MaxT[1] > MaxT[WhichPlane])
93 {
94 WhichPlane = 1;
95 }
96
97 if (MaxT[2] > MaxT[WhichPlane])
98 {
99 WhichPlane = 2;
100 }
101
102 // Check final candidate actually inside box
103 if (IR(MaxT[WhichPlane]) & 0x80000000)
104 {
105 // Miss the box
106 return false;
107 }
108
109 for (int i = 0; i < 3; ++i)
110 {
111 if (i != WhichPlane)
112 {
113 location[i] = origin[i] + MaxT[WhichPlane] * dir[i];
114 if ((location[i] < MinB[i]) ||
115 (location[i] > MaxB[i]))
116 {
117 // On this plane we're outside the box extents, so
118 // we miss the box
119 return false;
120 }
121 }
122 }
123 /*
124 // Choose the normal to be the plane normal facing into the ray
125 normal = G3D::Vector3::zero();
126 normal[WhichPlane] = (dir[WhichPlane] > 0) ? -1.0 : 1.0;
127 */
128 return true;
129#undef IR
130 }
131 };
132}
133#endif
#define IR(x)
void operator()(const G3D::Ray &ray, const TValue *entity, bool pStopAtFirstHit, float &distance)
Definition VMapTools.h:29
static bool collisionLocationForMovingPointFixedAABox(const G3D::Vector3 &origin, const G3D::Vector3 &dir, const G3D::AABox &box, G3D::Vector3 &location, bool &Inside)
Definition VMapTools.h:41