the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1//-------------------------------------------------------------------------------------
2// DirectXCollision.h -- C++ Collision Math library
3//
4// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
5// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
6// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
7// PARTICULAR PURPOSE.
8//
9// Copyright (c) Microsoft Corporation. All rights reserved.
10//-------------------------------------------------------------------------------------
11
12#ifdef _MSC_VER
13#pragma once
14#endif
15
16#include "DirectXMath.h"
17
18namespace DirectX
19{
20
21enum ContainmentType
22{
23 DISJOINT = 0,
24 INTERSECTS = 1,
25 CONTAINS = 2,
26};
27
28enum PlaneIntersectionType
29{
30 FRONT = 0,
31 INTERSECTING = 1,
32 BACK = 2,
33};
34
35struct BoundingBox;
36struct BoundingOrientedBox;
37struct BoundingFrustum;
38
39#pragma warning(push)
40#pragma warning(disable:4324 4820)
41
42//-------------------------------------------------------------------------------------
43// Bounding sphere
44//-------------------------------------------------------------------------------------
45struct BoundingSphere
46{
47 XMFLOAT3 Center; // Center of the sphere.
48 float Radius; // Radius of the sphere.
49
50 // Creators
51 BoundingSphere() : Center(0,0,0), Radius( 1.f ) {}
52 BoundingSphere( _In_ const XMFLOAT3& center, _In_ float radius )
53 : Center(center), Radius(radius) { assert( radius >= 0.f ); };
54 BoundingSphere( _In_ const BoundingSphere& sp )
55 : Center(sp.Center), Radius(sp.Radius) {}
56
57 // Methods
58 BoundingSphere& operator=( _In_ const BoundingSphere& sp ) { Center = sp.Center; Radius = sp.Radius; return *this; }
59
60 void Transform( _Out_ BoundingSphere& Out, _In_ CXMMATRIX M ) const;
61 void Transform( _Out_ BoundingSphere& Out, _In_ float Scale, _In_ FXMVECTOR Rotation, _In_ FXMVECTOR Translation ) const;
62 // Transform the sphere
63
64 ContainmentType Contains( _In_ FXMVECTOR Point ) const;
65 ContainmentType Contains( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
66 ContainmentType Contains( _In_ const BoundingSphere& sh ) const;
67 ContainmentType Contains( _In_ const BoundingBox& box ) const;
68 ContainmentType Contains( _In_ const BoundingOrientedBox& box ) const;
69 ContainmentType Contains( _In_ const BoundingFrustum& fr ) const;
70
71 bool Intersects( _In_ const BoundingSphere& sh ) const;
72 bool Intersects( _In_ const BoundingBox& box ) const;
73 bool Intersects( _In_ const BoundingOrientedBox& box ) const;
74 bool Intersects( _In_ const BoundingFrustum& fr ) const;
75
76 bool Intersects( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
77 // Triangle-sphere test
78
79 PlaneIntersectionType Intersects( _In_ FXMVECTOR Plane ) const;
80 // Plane-sphere test
81
82 bool Intersects( _In_ FXMVECTOR Origin, _In_ FXMVECTOR Direction, _Out_ float& Dist ) const;
83 // Ray-sphere test
84
85 ContainmentType ContainedBy( _In_ FXMVECTOR Plane0, _In_ FXMVECTOR Plane1, _In_ FXMVECTOR Plane2,
86 _In_ GXMVECTOR Plane3, _In_ CXMVECTOR Plane4, _In_ CXMVECTOR Plane5 ) const;
87 // Test sphere against six planes (see BoundingFrustum::GetPlanes)
88
89 // Static methods
90 static void CreateMerged( _Out_ BoundingSphere& Out, _In_ const BoundingSphere& S1, _In_ const BoundingSphere& S2 );
91
92 static void CreateFromBoundingBox( _Out_ BoundingSphere& Out, _In_ const BoundingBox& box );
93 static void CreateFromBoundingBox( _Out_ BoundingSphere& Out, _In_ const BoundingOrientedBox& box );
94
95 static void CreateFromPoints( _Out_ BoundingSphere& Out, _In_ size_t Count,
96 _In_reads_bytes_(sizeof(XMFLOAT3)+Stride*(Count-1)) const XMFLOAT3* pPoints, _In_ size_t Stride );
97
98 static void CreateFromFrustum( _Out_ BoundingSphere& Out, _In_ const BoundingFrustum& fr );
99};
100
101//-------------------------------------------------------------------------------------
102// Axis-aligned bounding box
103//-------------------------------------------------------------------------------------
104struct BoundingBox
105{
106 static const size_t CORNER_COUNT = 8;
107
108 XMFLOAT3 Center; // Center of the box.
109 XMFLOAT3 Extents; // Distance from the center to each side.
110
111 // Creators
112 BoundingBox() : Center(0,0,0), Extents( 1.f, 1.f, 1.f ) {}
113 BoundingBox( _In_ const XMFLOAT3& center, _In_ const XMFLOAT3& extents )
114 : Center(center), Extents(extents) { assert(extents.x >= 0 && extents.y >= 0 && extents.z >= 0); }
115 BoundingBox( _In_ const BoundingBox& box ) : Center(box.Center), Extents(box.Extents) {}
116
117 // Methods
118 BoundingBox& operator=( _In_ const BoundingBox& box) { Center = box.Center; Extents = box.Extents; return *this; }
119
120 void Transform( _Out_ BoundingBox& Out, _In_ CXMMATRIX M ) const;
121 void Transform( _Out_ BoundingBox& Out, _In_ float Scale, _In_ FXMVECTOR Rotation, _In_ FXMVECTOR Translation ) const;
122
123 void GetCorners( _Out_writes_(8) XMFLOAT3* Corners ) const;
124 // Gets the 8 corners of the box
125
126 ContainmentType Contains( _In_ FXMVECTOR Point ) const;
127 ContainmentType Contains( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
128 ContainmentType Contains( _In_ const BoundingSphere& sh ) const;
129 ContainmentType Contains( _In_ const BoundingBox& box ) const;
130 ContainmentType Contains( _In_ const BoundingOrientedBox& box ) const;
131 ContainmentType Contains( _In_ const BoundingFrustum& fr ) const;
132
133 bool Intersects( _In_ const BoundingSphere& sh ) const;
134 bool Intersects( _In_ const BoundingBox& box ) const;
135 bool Intersects( _In_ const BoundingOrientedBox& box ) const;
136 bool Intersects( _In_ const BoundingFrustum& fr ) const;
137
138 bool Intersects( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
139 // Triangle-Box test
140
141 PlaneIntersectionType Intersects( _In_ FXMVECTOR Plane ) const;
142 // Plane-box test
143
144 bool Intersects( _In_ FXMVECTOR Origin, _In_ FXMVECTOR Direction, _Out_ float& Dist ) const;
145 // Ray-Box test
146
147 ContainmentType ContainedBy( _In_ FXMVECTOR Plane0, _In_ FXMVECTOR Plane1, _In_ FXMVECTOR Plane2,
148 _In_ GXMVECTOR Plane3, _In_ CXMVECTOR Plane4, _In_ CXMVECTOR Plane5 ) const;
149 // Test box against six planes (see BoundingFrustum::GetPlanes)
150
151 // Static methods
152 static void CreateMerged( _Out_ BoundingBox& Out, _In_ const BoundingBox& b1, _In_ const BoundingBox& b2 );
153
154 static void CreateFromSphere( _Out_ BoundingBox& Out, _In_ const BoundingSphere& sh );
155
156 static void CreateFromPoints( _Out_ BoundingBox& Out, _In_ FXMVECTOR pt1, _In_ FXMVECTOR pt2 );
157 static void CreateFromPoints( _Out_ BoundingBox& Out, _In_ size_t Count,
158 _In_reads_bytes_(sizeof(XMFLOAT3)+Stride*(Count-1)) const XMFLOAT3* pPoints, _In_ size_t Stride );
159};
160
161//-------------------------------------------------------------------------------------
162// Oriented bounding box
163//-------------------------------------------------------------------------------------
164struct BoundingOrientedBox
165{
166 static const size_t CORNER_COUNT = 8;
167
168 XMFLOAT3 Center; // Center of the box.
169 XMFLOAT3 Extents; // Distance from the center to each side.
170 XMFLOAT4 Orientation; // Unit quaternion representing rotation (box -> world).
171
172 // Creators
173 BoundingOrientedBox() : Center(0,0,0), Extents( 1.f, 1.f, 1.f ), Orientation(0,0,0, 1.f ) {}
174 BoundingOrientedBox( _In_ const XMFLOAT3& _Center, _In_ const XMFLOAT3& _Extents, _In_ const XMFLOAT4& _Orientation )
175 : Center(_Center), Extents(_Extents), Orientation(_Orientation)
176 {
177 assert(_Extents.x >= 0 && _Extents.y >= 0 && _Extents.z >= 0);
178 }
179 BoundingOrientedBox( _In_ const BoundingOrientedBox& box )
180 : Center(box.Center), Extents(box.Extents), Orientation(box.Orientation) {}
181
182 // Methods
183 BoundingOrientedBox& operator=( _In_ const BoundingOrientedBox& box ) { Center = box.Center; Extents = box.Extents; Orientation = box.Orientation; return *this; }
184
185 void Transform( _Out_ BoundingOrientedBox& Out, _In_ CXMMATRIX M ) const;
186 void Transform( _Out_ BoundingOrientedBox& Out, _In_ float Scale, _In_ FXMVECTOR Rotation, _In_ FXMVECTOR Translation ) const;
187
188 void GetCorners( _Out_writes_(8) XMFLOAT3* Corners ) const;
189 // Gets the 8 corners of the box
190
191 ContainmentType Contains( _In_ FXMVECTOR Point ) const;
192 ContainmentType Contains( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
193 ContainmentType Contains( _In_ const BoundingSphere& sh ) const;
194 ContainmentType Contains( _In_ const BoundingBox& box ) const;
195 ContainmentType Contains( _In_ const BoundingOrientedBox& box ) const;
196 ContainmentType Contains( _In_ const BoundingFrustum& fr ) const;
197
198 bool Intersects( _In_ const BoundingSphere& sh ) const;
199 bool Intersects( _In_ const BoundingBox& box ) const;
200 bool Intersects( _In_ const BoundingOrientedBox& box ) const;
201 bool Intersects( _In_ const BoundingFrustum& fr ) const;
202
203 bool Intersects( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
204 // Triangle-OrientedBox test
205
206 PlaneIntersectionType Intersects( _In_ FXMVECTOR Plane ) const;
207 // Plane-OrientedBox test
208
209 bool Intersects( _In_ FXMVECTOR Origin, _In_ FXMVECTOR Direction, _Out_ float& Dist ) const;
210 // Ray-OrientedBox test
211
212 ContainmentType ContainedBy( _In_ FXMVECTOR Plane0, _In_ FXMVECTOR Plane1, _In_ FXMVECTOR Plane2,
213 _In_ GXMVECTOR Plane3, _In_ CXMVECTOR Plane4, _In_ CXMVECTOR Plane5 ) const;
214 // Test OrientedBox against six planes (see BoundingFrustum::GetPlanes)
215
216 // Static methods
217 static void CreateFromBoundingBox( _Out_ BoundingOrientedBox& Out, _In_ const BoundingBox& box );
218
219 static void CreateFromPoints( _Out_ BoundingOrientedBox& Out, _In_ size_t Count,
220 _In_reads_bytes_(sizeof(XMFLOAT3)+Stride*(Count-1)) const XMFLOAT3* pPoints, _In_ size_t Stride );
221};
222
223//-------------------------------------------------------------------------------------
224// Bounding frustum
225//-------------------------------------------------------------------------------------
226struct BoundingFrustum
227{
228 static const size_t CORNER_COUNT = 8;
229
230 XMFLOAT3 Origin; // Origin of the frustum (and projection).
231 XMFLOAT4 Orientation; // Quaternion representing rotation.
232
233 float RightSlope; // Positive X slope (X/Z).
234 float LeftSlope; // Negative X slope.
235 float TopSlope; // Positive Y slope (Y/Z).
236 float BottomSlope; // Negative Y slope.
237 float Near, Far; // Z of the near plane and far plane.
238
239 // Creators
240 BoundingFrustum() : Origin(0,0,0), Orientation(0,0,0, 1.f), RightSlope( 1.f ), LeftSlope( -1.f ),
241 TopSlope( 1.f ), BottomSlope( -1.f ), Near(0), Far( 1.f ) {}
242 BoundingFrustum( _In_ const XMFLOAT3& _Origin, _In_ const XMFLOAT4& _Orientation,
243 _In_ float _RightSlope, _In_ float _LeftSlope, _In_ float _TopSlope, _In_ float _BottomSlope,
244 _In_ float _Near, _In_ float _Far )
245 : Origin(_Origin), Orientation(_Orientation),
246 RightSlope(_RightSlope), LeftSlope(_LeftSlope), TopSlope(_TopSlope), BottomSlope(_BottomSlope),
247 Near(_Near), Far(_Far) { assert( _Near <= _Far ); }
248 BoundingFrustum( _In_ const BoundingFrustum& fr )
249 : Origin(fr.Origin), Orientation(fr.Orientation), RightSlope(fr.RightSlope), LeftSlope(fr.LeftSlope),
250 TopSlope(fr.TopSlope), BottomSlope(fr.BottomSlope), Near(fr.Near), Far(fr.Far) {}
251 BoundingFrustum( _In_ CXMMATRIX Projection ) { CreateFromMatrix( *this, Projection ); }
252
253 // Methods
254 BoundingFrustum& operator=( _In_ const BoundingFrustum& fr ) { Origin=fr.Origin; Orientation=fr.Orientation;
255 RightSlope=fr.RightSlope; LeftSlope=fr.LeftSlope;
256 TopSlope=fr.TopSlope; BottomSlope=fr.BottomSlope;
257 Near=fr.Near; Far=fr.Far; return *this; }
258
259 void Transform( _Out_ BoundingFrustum& Out, _In_ CXMMATRIX M ) const;
260 void Transform( _Out_ BoundingFrustum& Out, _In_ float Scale, _In_ FXMVECTOR Rotation, _In_ FXMVECTOR Translation ) const;
261
262 void GetCorners( _Out_writes_(8) XMFLOAT3* Corners ) const;
263 // Gets the 8 corners of the frustum
264
265 ContainmentType Contains( _In_ FXMVECTOR Point ) const;
266 ContainmentType Contains( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
267 ContainmentType Contains( _In_ const BoundingSphere& sp ) const;
268 ContainmentType Contains( _In_ const BoundingBox& box ) const;
269 ContainmentType Contains( _In_ const BoundingOrientedBox& box ) const;
270 ContainmentType Contains( _In_ const BoundingFrustum& fr ) const;
271 // Frustum-Frustum test
272
273 bool Intersects( _In_ const BoundingSphere& sh ) const;
274 bool Intersects( _In_ const BoundingBox& box ) const;
275 bool Intersects( _In_ const BoundingOrientedBox& box ) const;
276 bool Intersects( _In_ const BoundingFrustum& fr ) const;
277
278 bool Intersects( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const;
279 // Triangle-Frustum test
280
281 PlaneIntersectionType Intersects( _In_ FXMVECTOR Plane ) const;
282 // Plane-Frustum test
283
284 bool Intersects( _In_ FXMVECTOR Origin, _In_ FXMVECTOR Direction, _Out_ float& Dist ) const;
285 // Ray-Frustum test
286
287 ContainmentType ContainedBy( _In_ FXMVECTOR Plane0, _In_ FXMVECTOR Plane1, _In_ FXMVECTOR Plane2,
288 _In_ GXMVECTOR Plane3, _In_ CXMVECTOR Plane4, _In_ CXMVECTOR Plane5 ) const;
289 // Test frustum against six planes (see BoundingFrustum::GetPlanes)
290
291 void GetPlanes( _Out_opt_ XMVECTOR* NearPlane, _Out_opt_ XMVECTOR* FarPlane, _Out_opt_ XMVECTOR* RightPlane,
292 _Out_opt_ XMVECTOR* LeftPlane, _Out_opt_ XMVECTOR* TopPlane, _Out_opt_ XMVECTOR* BottomPlane ) const;
293 // Create 6 Planes representation of Frustum
294
295 // Static methods
296 static void CreateFromMatrix( _Out_ BoundingFrustum& Out, _In_ CXMMATRIX Projection );
297};
298
299//-----------------------------------------------------------------------------
300// Triangle intersection testing routines.
301//-----------------------------------------------------------------------------
302namespace TriangleTests
303{
304 bool Intersects( _In_ FXMVECTOR Origin, _In_ FXMVECTOR Direction, _In_ FXMVECTOR V0, _In_ GXMVECTOR V1, _In_ CXMVECTOR V2, _Out_ float& Dist );
305 // Ray-Triangle
306
307 bool Intersects( _In_ FXMVECTOR A0, _In_ FXMVECTOR A1, _In_ FXMVECTOR A2, _In_ GXMVECTOR B0, _In_ CXMVECTOR B1, _In_ CXMVECTOR B2 );
308 // Triangle-Triangle
309
310 PlaneIntersectionType Intersects( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2, _In_ GXMVECTOR Plane );
311 // Plane-Triangle
312
313 ContainmentType ContainedBy( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2,
314 _In_ GXMVECTOR Plane0, _In_ CXMVECTOR Plane1, _In_ CXMVECTOR Plane2,
315 _In_ CXMVECTOR Plane3, _In_ CXMVECTOR Plane4, _In_ CXMVECTOR Plane5 );
316 // Test a triangle against six planes at once (see BoundingFrustum::GetPlanes)
317};
318
319#pragma warning(pop)
320
321/****************************************************************************
322 *
323 * Implementation
324 *
325 ****************************************************************************/
326
327#pragma warning(push)
328#pragma warning(disable : 4068 4616 6001)
329
330#pragma prefast(push)
331#pragma prefast(disable : 25000, "FXMVECTOR is 16 bytes")
332
333#include "DirectXCollision.inl"
334
335#pragma prefast(pop)
336#pragma warning(pop)
337
338}; // namespace DirectX
339