-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollision.cpp
308 lines (256 loc) · 10 KB
/
Collision.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "Collision.h"
using namespace DirectX;
Ray::Ray()
: origin(), direction(0.0f, 0.0f, 1.0f)
{
}
Ray::Ray(const DirectX::XMFLOAT3 & origin, const DirectX::XMFLOAT3 & direction)
: origin(origin)
{
// 射线的direction长度必须为1.0f,误差在1e-5f内
XMVECTOR dirLength = XMVector3Length(XMLoadFloat3(&direction));
XMVECTOR error = XMVectorAbs(dirLength - XMVectorSplatOne());
assert(XMVector3Less(error, XMVectorReplicate(1e-5f)));
XMStoreFloat3(&this->direction, XMVector3Normalize(XMLoadFloat3(&direction)));
}
Ray Ray::ScreenToRay(const Camera & camera, float screenX, float screenY)
{
// ******************
// 节选自DirectX::XMVector3Unproject函数,并省略了从世界坐标系到局部坐标系的变换
//
// 将屏幕坐标点从视口变换回NDC坐标系
static const XMVECTORF32 D = { { { -1.0f, 1.0f, 0.0f, 0.0f } } };
XMVECTOR V = XMVectorSet(screenX, screenY, 0.0f, 1.0f);
D3D11_VIEWPORT viewPort = camera.GetViewPort();
XMVECTOR Scale = XMVectorSet(viewPort.Width * 0.5f, -viewPort.Height * 0.5f, viewPort.MaxDepth - viewPort.MinDepth, 1.0f);
Scale = XMVectorReciprocal(Scale);
XMVECTOR Offset = XMVectorSet(-viewPort.TopLeftX, -viewPort.TopLeftY, -viewPort.MinDepth, 0.0f);
Offset = XMVectorMultiplyAdd(Scale, Offset, D.v);
// 从NDC坐标系变换回世界坐标系
XMMATRIX Transform = XMMatrixMultiply(camera.GetViewXM(), camera.GetProjXM());
Transform = XMMatrixInverse(nullptr, Transform);
XMVECTOR Target = XMVectorMultiplyAdd(V, Scale, Offset);
Target = XMVector3TransformCoord(Target, Transform);
// 求出射线
XMFLOAT3 direction;
XMStoreFloat3(&direction, XMVector3Normalize(Target - camera.GetPositionXM()));
return Ray(camera.GetPosition(), direction);
}
bool Ray::Hit(const DirectX::BoundingBox & box, float * pOutDist, float maxDist)
{
float dist;
bool res = box.Intersects(XMLoadFloat3(&origin), XMLoadFloat3(&direction), dist);
if (pOutDist)
*pOutDist = dist;
return dist > maxDist ? false : res;
}
bool Ray::Hit(const DirectX::BoundingOrientedBox & box, float * pOutDist, float maxDist)
{
float dist;
bool res = box.Intersects(XMLoadFloat3(&origin), XMLoadFloat3(&direction), dist);
if (pOutDist)
*pOutDist = dist;
return dist > maxDist ? false : res;
}
bool Ray::Hit(const DirectX::BoundingSphere & sphere, float * pOutDist, float maxDist)
{
float dist;
bool res = sphere.Intersects(XMLoadFloat3(&origin), XMLoadFloat3(&direction), dist);
if (pOutDist)
*pOutDist = dist;
return dist > maxDist ? false : res;
}
bool XM_CALLCONV Ray::Hit(FXMVECTOR V0, FXMVECTOR V1, FXMVECTOR V2, float * pOutDist, float maxDist)
{
float dist;
bool res = TriangleTests::Intersects(XMLoadFloat3(&origin), XMLoadFloat3(&direction), V0, V1, V2, dist);
if (pOutDist)
*pOutDist = dist;
return dist > maxDist ? false : res;
}
Collision::WireFrameData Collision::CreateBoundingBox(const DirectX::BoundingBox& box, const DirectX::XMFLOAT4& color)
{
XMFLOAT3 corners[8];
box.GetCorners(corners);
return CreateFromCorners(corners, color);
}
Collision::WireFrameData Collision::CreateBoundingOrientedBox(const DirectX::BoundingOrientedBox& box, const DirectX::XMFLOAT4& color)
{
XMFLOAT3 corners[8];
box.GetCorners(corners);
return CreateFromCorners(corners, color);
}
Collision::WireFrameData Collision::CreateBoundingSphere(const DirectX::BoundingSphere& sphere, const DirectX::XMFLOAT4& color, int slices)
{
WireFrameData data;
XMVECTOR center = XMLoadFloat3(&sphere.Center), posVec;
XMFLOAT3 pos;
float theta = 0.0f;
for (int i = 0; i < slices; ++i)
{
posVec = XMVector3Transform(center + XMVectorSet(1.0f, 0.0f, 0.0f, 1.0f), XMMatrixRotationY(theta));
XMStoreFloat3(&pos, posVec);
data.vertexVec.push_back({ pos, color });
posVec = XMVector3Transform(center + XMVectorSet(0.0f, 1.0f, 0.0f, 1.0f), XMMatrixRotationZ(theta));
XMStoreFloat3(&pos, posVec);
data.vertexVec.push_back({ pos, color });
posVec = XMVector3Transform(center + XMVectorSet(0.0f, 0.0f, 1.0f, 1.0f), XMMatrixRotationX(theta));
XMStoreFloat3(&pos, posVec);
data.vertexVec.push_back({ pos, color });
theta += XM_2PI / slices;
}
for (int i = 0; i < slices; ++i)
{
data.indexVec.push_back(i * 3);
data.indexVec.push_back((i + 1) % slices * 3);
data.indexVec.push_back(i * 3 + 1);
data.indexVec.push_back((i + 1) % slices * 3 + 1);
data.indexVec.push_back(i * 3 + 2);
data.indexVec.push_back((i + 1) % slices * 3 + 2);
}
return data;
}
Collision::WireFrameData Collision::CreateBoundingFrustum(const DirectX::BoundingFrustum& frustum, const DirectX::XMFLOAT4& color)
{
XMFLOAT3 corners[8];
frustum.GetCorners(corners);
return CreateFromCorners(corners, color);
}
std::vector<XMMATRIX> XM_CALLCONV Collision::FrustumCulling(
const std::vector<XMMATRIX>& Matrices, const BoundingBox& localBox, FXMMATRIX View, CXMMATRIX Proj)
{
std::vector<DirectX::XMMATRIX> acceptedData;
BoundingFrustum frustum;
BoundingFrustum::CreateFromMatrix(frustum, Proj);
XMMATRIX InvView = XMMatrixInverse(nullptr, View);
// 将视锥体从局部坐标系变换到世界坐标系中
frustum.Transform(frustum, InvView);
BoundingOrientedBox localOrientedBox, orientedBox;
BoundingOrientedBox::CreateFromBoundingBox(localOrientedBox, localBox);
for (auto& mat : Matrices)
{
// 将有向包围盒从局部坐标系变换到世界坐标系中
localOrientedBox.Transform(orientedBox, mat);
// 相交检测
if (frustum.Intersects(orientedBox))
acceptedData.push_back(mat);
}
return acceptedData;
}
std::vector<DirectX::XMMATRIX> XM_CALLCONV Collision::FrustumCulling2(
const std::vector<DirectX::XMMATRIX>& Matrices, const DirectX::BoundingBox& localBox, DirectX::FXMMATRIX View, DirectX::CXMMATRIX Proj)
{
std::vector<DirectX::XMMATRIX> acceptedData;
BoundingFrustum frustum, localFrustum;
BoundingFrustum::CreateFromMatrix(frustum, Proj);
XMMATRIX InvView = XMMatrixInverse(nullptr, View);
for (auto& mat : Matrices)
{
XMMATRIX InvWorld = XMMatrixInverse(nullptr, mat);
// 将视锥体从观察坐标系(或局部坐标系)变换到物体所在的局部坐标系中
frustum.Transform(localFrustum, InvView * InvWorld);
// 相交检测
if (localFrustum.Intersects(localBox))
acceptedData.push_back(mat);
}
return acceptedData;
}
std::vector<DirectX::XMMATRIX> XM_CALLCONV Collision::FrustumCulling3(
const std::vector<DirectX::XMMATRIX>& Matrices, const DirectX::BoundingBox& localBox, DirectX::FXMMATRIX View, DirectX::CXMMATRIX Proj)
{
std::vector<DirectX::XMMATRIX> acceptedData;
BoundingFrustum frustum;
BoundingFrustum::CreateFromMatrix(frustum, Proj);
BoundingOrientedBox localOrientedBox, orientedBox;
BoundingOrientedBox::CreateFromBoundingBox(localOrientedBox, localBox);
for (auto& mat : Matrices)
{
// 将有向包围盒从局部坐标系变换到视锥体所在的局部坐标系(观察坐标系)中
localOrientedBox.Transform(orientedBox, mat * View);
// 相交检测
if (frustum.Intersects(orientedBox))
acceptedData.push_back(mat);
}
return acceptedData;
}
std::vector<Transform> XM_CALLCONV Collision::FrustumCulling(
const std::vector<Transform>& transforms, const DirectX::BoundingBox& localBox, DirectX::FXMMATRIX View, DirectX::CXMMATRIX Proj)
{
std::vector<Transform> acceptedData;
BoundingFrustum frustum;
BoundingFrustum::CreateFromMatrix(frustum, Proj);
BoundingOrientedBox localOrientedBox, orientedBox;
BoundingOrientedBox::CreateFromBoundingBox(localOrientedBox, localBox);
for (auto& t : transforms)
{
XMMATRIX W = t.GetLocalToWorldMatrixXM();
// 将有向包围盒从局部坐标系变换到视锥体所在的局部坐标系(观察坐标系)中
localOrientedBox.Transform(orientedBox, W * View);
// 相交检测
if (frustum.Intersects(orientedBox))
acceptedData.push_back(t);
}
return acceptedData;
}
std::vector<Transform> XM_CALLCONV Collision::FrustumCulling2(
const std::vector<Transform>& transforms, const DirectX::BoundingBox& localBox, DirectX::FXMMATRIX View, DirectX::CXMMATRIX Proj)
{
std::vector<Transform> acceptedData;
BoundingFrustum frustum, localFrustum;
BoundingFrustum::CreateFromMatrix(frustum, Proj);
XMMATRIX InvView = XMMatrixInverse(nullptr, View);
for (auto& t : transforms)
{
XMMATRIX W = t.GetLocalToWorldMatrixXM();
XMMATRIX InvWorld = XMMatrixInverse(nullptr, W);
// 将视锥体从观察坐标系(或局部坐标系)变换到物体所在的局部坐标系中
frustum.Transform(localFrustum, InvView * InvWorld);
// 相交检测
if (localFrustum.Intersects(localBox))
acceptedData.push_back(t);
}
return acceptedData;
}
std::vector<Transform> XM_CALLCONV Collision::FrustumCulling3(
const std::vector<Transform>& transforms, const DirectX::BoundingBox& localBox, DirectX::FXMMATRIX View, DirectX::CXMMATRIX Proj)
{
std::vector<Transform> acceptedData;
BoundingFrustum frustum;
BoundingFrustum::CreateFromMatrix(frustum, Proj);
BoundingOrientedBox localOrientedBox, orientedBox;
BoundingOrientedBox::CreateFromBoundingBox(localOrientedBox, localBox);
for (auto& t : transforms)
{
XMMATRIX W = t.GetLocalToWorldMatrixXM();
// 将有向包围盒从局部坐标系变换到视锥体所在的局部坐标系(观察坐标系)中
localOrientedBox.Transform(orientedBox, W * View);
// 相交检测
if (frustum.Intersects(orientedBox))
acceptedData.push_back(t);
}
return acceptedData;
}
Collision::WireFrameData Collision::CreateFromCorners(const DirectX::XMFLOAT3(&corners)[8], const DirectX::XMFLOAT4& color)
{
WireFrameData data;
// AABB/OBB顶点索引如下 视锥体顶点索引如下
// 3_______2 4__________5
// /| /| |\ /|
// 7/_|____6/ | | \ / |
// | |____|__| 7|_0\____/1_|6
// | /0 | /1 \ | | /
// |/______|/ \|____|/
// 4 5 3 2
for (int i = 0; i < 8; ++i)
data.vertexVec.push_back({ corners[i], color });
for (int i = 0; i < 4; ++i)
{
data.indexVec.push_back(i);
data.indexVec.push_back(i + 4);
data.indexVec.push_back(i);
data.indexVec.push_back((i + 1) % 4);
data.indexVec.push_back(i + 4);
data.indexVec.push_back((i + 1) % 4 + 4);
}
return data;
}