Repository navigation
Expand file tree
/
Copy pathcollision.cpp
More file actions
162 lines (129 loc) · 4.27 KB
/
Copy pathcollision.cpp
File metadata and controls
162 lines (129 loc) · 4.27 KB
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
//=============================================================================
//
// 当たり判定処理 [collision.cpp]
// Author :
//
//=============================================================================
#include <cmath>
#include <algorithm>
#include "main.h"
#include "collision.h"
//*****************************************************************************
// マクロ定義
//*****************************************************************************
//*****************************************************************************
// 構造体定義
//*****************************************************************************
//*****************************************************************************
// プロトタイプ宣言
//*****************************************************************************
//*****************************************************************************
// グローバル変数
//*****************************************************************************
//=============================================================================
// BBによる当たり判定処理
// 回転は考慮しない
// 戻り値:当たってたらtrue
//=============================================================================
BOOL CollisionBB(XMFLOAT3 mpos, float mw, float mh,
XMFLOAT3 ypos, float yw, float yh)
{
BOOL ans = FALSE; // 外れをセットしておく
// 座標が中心点なので計算しやすく半分にしている
mw /= 2;
mh /= 2;
yw /= 2;
yh /= 2;
// バウンディングボックス(BB)の処理
if ((mpos.x + mw > ypos.x - yw) &&
(mpos.x - mw < ypos.x + yw) &&
(mpos.y + mh > ypos.y - yh) &&
(mpos.y - mh < ypos.y + yh))
{
// 当たった時の処理
ans = TRUE;
}
return ans;
}
//=============================================================================
// BCによる当たり判定処理
// サイズは半径
// 戻り値:当たってたらTRUE
//=============================================================================
BOOL CollisionBC(XMFLOAT3 pos1, XMFLOAT3 pos2, float r1, float r2)
{
BOOL ans = FALSE; // 外れをセットしておく
float len = (r1 + r2) * (r1 + r2); // 半径を2乗した物
XMVECTOR temp = XMLoadFloat3(&pos1) - XMLoadFloat3(&pos2);
temp = XMVector3LengthSq(temp); // 2点間の距離(2乗した物)
float lenSq = 0.0f;
XMStoreFloat(&lenSq, temp);
// 半径を2乗した物より距離が短い?
if (len > lenSq)
{
ans = TRUE; // 当たっている
}
return ans;
}
//回ったArrow当たり判定
BOOL CollisionBBArrow(XMFLOAT3 Apos, float Aw, float Ah, float Arot,
XMFLOAT3 ypos, float yw, float yh)
{
auto RotatePoint = [](float x, float y, float angle) -> XMFLOAT2 {
float s = sin(angle);
float c = cos(angle);
return XMFLOAT2(x * c - y * s, x * s + y * c);
};
// 回った四頂点を取る
XMFLOAT2 mCorners[4] = {
RotatePoint(-Aw / 2, -Ah / 2, Arot),
RotatePoint(Aw / 2, -Ah / 2, Arot),
RotatePoint(Aw / 2, Ah / 2, Arot),
RotatePoint(-Aw / 2, Ah / 2, Arot)
};
for (auto& corner : mCorners)
{
corner.x += Apos.x;
corner.y += Apos.y;
}
// 回ってない四頂点を取る
XMFLOAT2 yCorners[4] = {
XMFLOAT2(ypos.x - yw / 2, ypos.y - yh / 2),
XMFLOAT2(ypos.x + yw / 2, ypos.y - yh / 2),
XMFLOAT2(ypos.x + yw / 2, ypos.y + yh / 2),
XMFLOAT2(ypos.x - yw / 2, ypos.y + yh / 2)
};
auto GetAxis = [](const XMFLOAT2& p1, const XMFLOAT2& p2) -> XMFLOAT2 {
return XMFLOAT2(p2.y - p1.y, p1.x - p2.x); // 法線
};
XMFLOAT2 axes[4] = {
GetAxis(mCorners[0], mCorners[1]),
GetAxis(mCorners[1], mCorners[2]),
XMFLOAT2(1, 0), // 回ってない軸
XMFLOAT2(0, 1) // 回ってない軸
};
auto ProjectOntoAxis = [](const XMFLOAT2* corners, const XMFLOAT2& axis) -> std::pair<float, float> {
float min = (corners[0].x * axis.x + corners[0].y * axis.y);
float max = min;
for (int i = 1; i < 4; i++)
{
float projection = (corners[i].x * axis.x + corners[i].y * axis.y);
if (projection < min) min = projection;
if (projection > max) max = projection;
}
return { min, max };
};
auto Overlap = [](const std::pair<float, float>& proj1, const std::pair<float, float>& proj2) -> bool {
return !(proj1.second < proj2.first || proj2.second < proj1.first);
};
for (const auto& axis : axes)
{
std::pair<float, float> proj1 = ProjectOntoAxis(mCorners, axis);
std::pair<float, float> proj2 = ProjectOntoAxis(yCorners, axis);
if (!Overlap(proj1, proj2))
{
return FALSE; // 当たってない
}
}
return TRUE; // 当たり
}