-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollision.h
93 lines (71 loc) · 3.67 KB
/
Collision.h
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
//***************************************************************************************
// Collision.h by X_Jun(MKXJun) (C) 2018-2020 All Rights Reserved.
// Licensed under the MIT License.
//
// 提供一些封装好的对象和碰撞检测方法
// 注意:WireFrameData目前仍未经过稳定测试,未来有可能会移植到Geometry.h中
// Provide encapsulated collision classes and detection method.
//***************************************************************************************
#ifndef COLLISION_H
#define COLLISION_H
#include <DirectXCollision.h>
#include <vector>
#include "Vertex.h"
#include "Camera.h"
struct Ray
{
Ray();
Ray(const DirectX::XMFLOAT3& origin, const DirectX::XMFLOAT3& direction);
static Ray ScreenToRay(const Camera& camera, float screenX, float screenY);
bool Hit(const DirectX::BoundingBox& box, float* pOutDist = nullptr, float maxDist = FLT_MAX);
bool Hit(const DirectX::BoundingOrientedBox& box, float* pOutDist = nullptr, float maxDist = FLT_MAX);
bool Hit(const DirectX::BoundingSphere& sphere, float* pOutDist = nullptr, float maxDist = FLT_MAX);
bool XM_CALLCONV Hit(DirectX::FXMVECTOR V0, DirectX::FXMVECTOR V1, DirectX::FXMVECTOR V2, float* pOutDist = nullptr, float maxDist = FLT_MAX);
DirectX::XMFLOAT3 origin; // 射线原点
DirectX::XMFLOAT3 direction; // 单位方向向量
};
class Collision
{
public:
// 线框顶点/索引数组
struct WireFrameData
{
std::vector<VertexPosColor> vertexVec; // 顶点数组
std::vector<WORD> indexVec; // 索引数组
};
//
// 包围盒线框的创建
//
// 创建AABB盒线框
static WireFrameData CreateBoundingBox(const DirectX::BoundingBox& box, const DirectX::XMFLOAT4& color);
// 创建OBB盒线框
static WireFrameData CreateBoundingOrientedBox(const DirectX::BoundingOrientedBox& box, const DirectX::XMFLOAT4& color);
// 创建包围球线框
static WireFrameData CreateBoundingSphere(const DirectX::BoundingSphere& sphere, const DirectX::XMFLOAT4& color, int slices = 20);
// 创建视锥体线框
static WireFrameData CreateBoundingFrustum(const DirectX::BoundingFrustum& frustum, const DirectX::XMFLOAT4& color);
//
// 三种等价的测试视锥体裁剪的方法,获取所有与视锥体碰撞的碰撞体对应的世界矩阵数组
//
// 视锥体裁剪
static std::vector<DirectX::XMMATRIX> XM_CALLCONV FrustumCulling(
const std::vector<DirectX::XMMATRIX>& Matrices, const DirectX::BoundingBox& localBox, DirectX::FXMMATRIX View, DirectX::CXMMATRIX Proj);
// 视锥体裁剪2
static std::vector<DirectX::XMMATRIX> XM_CALLCONV FrustumCulling2(
const std::vector<DirectX::XMMATRIX>& Matrices, const DirectX::BoundingBox& localBox, DirectX::FXMMATRIX View, DirectX::CXMMATRIX Proj);
// 视锥体裁剪3
static std::vector<DirectX::XMMATRIX> XM_CALLCONV FrustumCulling3(
const std::vector<DirectX::XMMATRIX>& Matrices, const DirectX::BoundingBox& localBox, DirectX::FXMMATRIX View, DirectX::CXMMATRIX Proj);
// 视锥体裁剪
static std::vector<Transform> XM_CALLCONV FrustumCulling(
const std::vector<Transform>& transforms, const DirectX::BoundingBox& localBox, DirectX::FXMMATRIX View, DirectX::CXMMATRIX Proj);
// 视锥体裁剪2
static std::vector<Transform> XM_CALLCONV FrustumCulling2(
const std::vector<Transform>& transforms, const DirectX::BoundingBox& localBox, DirectX::FXMMATRIX View, DirectX::CXMMATRIX Proj);
// 视锥体裁剪3
static std::vector<Transform> XM_CALLCONV FrustumCulling3(
const std::vector<Transform>& transforms, const DirectX::BoundingBox& localBox, DirectX::FXMMATRIX View, DirectX::CXMMATRIX Proj);
private:
static WireFrameData CreateFromCorners(const DirectX::XMFLOAT3(&corners)[8], const DirectX::XMFLOAT4& color);
};
#endif