|
| 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 | + |
| 18 | +namespace DirectX |
| 19 | +{ |
| 20 | + |
| 21 | +enum ContainmentType |
| 22 | +{ |
| 23 | + DISJOINT = 0, |
| 24 | + INTERSECTS = 1, |
| 25 | + CONTAINS = 2, |
| 26 | +}; |
| 27 | + |
| 28 | +enum PlaneIntersectionType |
| 29 | +{ |
| 30 | + FRONT = 0, |
| 31 | + INTERSECTING = 1, |
| 32 | + BACK = 2, |
| 33 | +}; |
| 34 | + |
| 35 | +struct BoundingBox; |
| 36 | + |
| 37 | +#pragma warning(push) |
| 38 | +#pragma warning(disable:4324) |
| 39 | + |
| 40 | +//------------------------------------------------------------------------------------- |
| 41 | +// Bounding sphere |
| 42 | +//------------------------------------------------------------------------------------- |
| 43 | +__declspec(align(16)) struct BoundingSphere |
| 44 | +{ |
| 45 | + XMFLOAT3 Center; // Center of the sphere. |
| 46 | + float Radius; // Radius of the sphere. |
| 47 | + |
| 48 | + // Creators |
| 49 | + BoundingSphere() : Center(0,0,0), Radius( 1.f ) {} |
| 50 | + BoundingSphere( _In_ const XMFLOAT3& center, _In_ float radius ) |
| 51 | + : Center(center), Radius(radius) { assert( radius >= 0.f ); }; |
| 52 | + BoundingSphere( _In_ const BoundingSphere& sp ) |
| 53 | + : Center(sp.Center), Radius(sp.Radius) {} |
| 54 | + |
| 55 | + // Methods |
| 56 | + BoundingSphere& operator=( _In_ const BoundingSphere& sp ) { Center = sp.Center; Radius = sp.Radius; return *this; } |
| 57 | + |
| 58 | + void Transform( _Out_ BoundingSphere& Out, _In_ CXMMATRIX M ) const; |
| 59 | + void Transform( _Out_ BoundingSphere& Out, _In_ float Scale, _In_ FXMVECTOR Rotation, _In_ FXMVECTOR Translation ) const; |
| 60 | + // Transform the sphere |
| 61 | + |
| 62 | + ContainmentType Contains( _In_ FXMVECTOR Point ) const; |
| 63 | + ContainmentType Contains( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const; |
| 64 | + ContainmentType Contains( _In_ const BoundingSphere& sh ) const; |
| 65 | + ContainmentType Contains( _In_ const BoundingBox& box ) const; |
| 66 | + |
| 67 | + bool Intersects( _In_ const BoundingSphere& sh ) const; |
| 68 | + bool Intersects( _In_ const BoundingBox& box ) const; |
| 69 | + |
| 70 | + bool Intersects( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const; |
| 71 | + // Triangle-sphere test |
| 72 | + |
| 73 | + PlaneIntersectionType Intersects( _In_ FXMVECTOR Plane ) const; |
| 74 | + // Plane-sphere test |
| 75 | + |
| 76 | + bool Intersects( _In_ FXMVECTOR Origin, _In_ FXMVECTOR Direction, _Out_ float& Dist ) const; |
| 77 | + // Ray-sphere test |
| 78 | + |
| 79 | + // Static methods |
| 80 | + static void CreateMerged( _Out_ BoundingSphere& Out, _In_ const BoundingSphere& S1, _In_ const BoundingSphere& S2 ); |
| 81 | + |
| 82 | + static void CreateFromBoundingBox( _Out_ BoundingSphere& Out, _In_ const BoundingBox& box ); |
| 83 | + |
| 84 | + static void CreateFromPoints( _Out_ BoundingSphere& Out, _In_ size_t Count, |
| 85 | + _In_reads_bytes_(sizeof(XMFLOAT3)+Stride*(Count-1)) const XMFLOAT3* pPoints, _In_ size_t Stride ); |
| 86 | +}; |
| 87 | + |
| 88 | +//------------------------------------------------------------------------------------- |
| 89 | +// Axis-aligned bounding box |
| 90 | +//------------------------------------------------------------------------------------- |
| 91 | +__declspec(align(16)) struct BoundingBox |
| 92 | +{ |
| 93 | + static const size_t CORNER_COUNT = 8; |
| 94 | + |
| 95 | + XMFLOAT3 Center; // Center of the box. |
| 96 | + XMFLOAT3 Extents; // Distance from the center to each side. |
| 97 | + |
| 98 | + // Creators |
| 99 | + BoundingBox() : Center(0,0,0), Extents( 1.f, 1.f, 1.f ) {} |
| 100 | + BoundingBox( _In_ const XMFLOAT3& center, _In_ const XMFLOAT3& extents ) |
| 101 | + : Center(center), Extents(extents) { assert(extents.x >= 0 && extents.y >= 0 && extents.z >= 0); } |
| 102 | + BoundingBox( _In_ const BoundingBox& box ) : Center(box.Center), Extents(box.Extents) {} |
| 103 | + |
| 104 | + // Methods |
| 105 | + BoundingBox& operator=( _In_ const BoundingBox& box) { Center = box.Center; Extents = box.Extents; return *this; } |
| 106 | + |
| 107 | + void Transform( _Out_ BoundingBox& Out, _In_ CXMMATRIX M ) const; |
| 108 | + void Transform( _Out_ BoundingBox& Out, _In_ float Scale, _In_ FXMVECTOR Rotation, _In_ FXMVECTOR Translation ) const; |
| 109 | + |
| 110 | + void GetCorners( _Out_writes_(8) XMFLOAT3* Corners ) const; |
| 111 | + // Gets the 8 corners of the box |
| 112 | + |
| 113 | + ContainmentType Contains( _In_ FXMVECTOR Point ) const; |
| 114 | + ContainmentType Contains( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const; |
| 115 | + ContainmentType Contains( _In_ const BoundingSphere& sh ) const; |
| 116 | + ContainmentType Contains( _In_ const BoundingBox& box ) const; |
| 117 | + |
| 118 | + bool Intersects( _In_ const BoundingSphere& sh ) const; |
| 119 | + bool Intersects( _In_ const BoundingBox& box ) const; |
| 120 | + |
| 121 | + bool Intersects( _In_ FXMVECTOR V0, _In_ FXMVECTOR V1, _In_ FXMVECTOR V2 ) const; |
| 122 | + // Triangle-Box test |
| 123 | + |
| 124 | + PlaneIntersectionType Intersects( _In_ FXMVECTOR Plane ) const; |
| 125 | + // Plane-box test |
| 126 | + |
| 127 | + bool Intersects( _In_ FXMVECTOR Origin, _In_ FXMVECTOR Direction, _Out_ float& Dist ) const; |
| 128 | + // Ray-Box test |
| 129 | + |
| 130 | + // Static methods |
| 131 | + static void CreateMerged( _Out_ BoundingBox& Out, _In_ const BoundingBox& b1, _In_ const BoundingBox& b2 ); |
| 132 | + |
| 133 | + static void CreateFromSphere( _Out_ BoundingBox& Out, _In_ const BoundingSphere& sh ); |
| 134 | + |
| 135 | + static void CreateFromPoints( _Out_ BoundingBox& Out, _In_ FXMVECTOR pt1, _In_ FXMVECTOR pt2 ); |
| 136 | + static void CreateFromPoints( _Out_ BoundingBox& Out, _In_ size_t Count, |
| 137 | + _In_reads_bytes_(sizeof(XMFLOAT3)+Stride*(Count-1)) const XMFLOAT3* pPoints, _In_ size_t Stride ); |
| 138 | +}; |
| 139 | + |
| 140 | +#pragma warning(pop) |
| 141 | + |
| 142 | +/**************************************************************************** |
| 143 | + * |
| 144 | + * Implementation |
| 145 | + * |
| 146 | + ****************************************************************************/ |
| 147 | + |
| 148 | +#pragma warning(push) |
| 149 | +#pragma warning(disable:4068) |
| 150 | + |
| 151 | +#pragma prefast(push) |
| 152 | +#pragma prefast(disable : 25000, "FXMVECTOR is 16 bytes") |
| 153 | + |
| 154 | +#include "DirectXCollision.inl" |
| 155 | + |
| 156 | +#pragma prefast(pop) |
| 157 | +#pragma warning(pop) |
| 158 | + |
| 159 | +}; // namespace DirectX |
| 160 | + |
0 commit comments