質問編集履歴
1
タイトルと文章を編集しました。
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
高速で移動する物体同士の矩形での当たり判定を実装したい
|
1
|
+
高速で移動する物体同士の矩形での当たり判定を実装したい
|
body
CHANGED
@@ -1,108 +1,34 @@
|
|
1
|
-
|
1
|
+
以下のコードでは矩形の当たり判定ですが高速で移動する物体では当たり先の物体を通り抜けてしまうので判定出来ません。その際はどうすれば当たり判定を作るのでしょうか?
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
+
提示コードは矩形の当たり判定ですコードです。
|
5
6
|
|
6
7
|
|
7
8
|
|
8
|
-
|
9
9
|
```cpp
|
10
10
|
|
11
|
-
|
12
11
|
// 矩形同士の当たり判定
|
13
|
-
bool
|
12
|
+
bool Rect_and_Rect_2D(Box_Collision_2D& a, Box_Collision_2D& b)
|
14
|
-
{
|
13
|
+
{
|
15
|
-
|
14
|
+
|
16
|
-
if (a.getOnCollision() == true && b.getOnCollision() == true)
|
15
|
+
if (a.getOnCollision() == true && b.getOnCollision() == true)
|
17
|
-
|
18
|
-
printf("a.x: %d\n",a.getMove().x);
|
19
|
-
printf("a.y: %d\n",a.getMove().y);
|
20
|
-
|
21
|
-
|
16
|
+
{
|
22
|
-
|
23
|
-
int bb = b.getMove().length();
|
24
|
-
|
25
|
-
printf("aa: %d\n", aa);
|
26
|
-
//
|
17
|
+
// 矩形の衝突判定
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
int speed = 0;
|
31
|
-
if (
|
18
|
+
if (((a.getPosition().x + a.getSize().x > b.getPosition().x) && (a.getPosition().x < b.getPosition().x + b.getSize().x))
|
19
|
+
&& ((a.getPosition().y + a.getSize().y > b.getPosition().y) && (a.getPosition().y < b.getPosition().y + b.getSize().y)))
|
32
20
|
{
|
33
|
-
|
21
|
+
return true;
|
34
22
|
}
|
35
|
-
else {
|
36
|
-
|
37
|
-
speed = bb / maxSpeed;
|
38
|
-
}
|
39
|
-
|
40
|
-
printf("speed: %d\n", speed);
|
41
|
-
|
42
|
-
if (speed > 0) {
|
43
|
-
|
44
|
-
Box_Collision_2D box_a = a;
|
45
|
-
Box_Collision_2D box_b = b;
|
46
|
-
|
47
|
-
//A
|
48
|
-
glm::ivec2 pos = a.getPosition();
|
49
|
-
pos = pos - a.getMove();
|
50
|
-
box_a.setPosition(pos);
|
51
|
-
box_a.setColSize(a.getSize());
|
52
|
-
|
53
|
-
//B
|
54
|
-
pos = b.getPosition();
|
55
|
-
pos = pos - b.getMove();
|
56
|
-
box_b.setPosition(pos);
|
57
|
-
box_b.setColSize(b.getSize());
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
for (int i = 0; i < speed; i++)
|
64
|
-
{
|
65
|
-
|
66
|
-
|
67
|
-
// 矩形の衝突判定
|
68
|
-
if (((box_a.getPosition().x + box_a.getSize().x > box_b.getPosition().x) && (box_a.getPosition().x < box_b.getPosition().x + box_b.getSize().x))
|
69
|
-
&& ((box_a.getPosition().y + box_a.getSize().y > box_b.getPosition().y) && (box_a.getPosition().y < box_b.getPosition().y + box_b.getSize().y)))
|
70
|
-
{
|
71
|
-
return true;
|
72
|
-
}
|
73
|
-
else
|
74
|
-
{
|
75
|
-
return false;
|
76
|
-
}
|
77
|
-
|
78
|
-
glm::ivec2 move(maxSpeed,maxSpeed);
|
79
|
-
box_a.setPosition(box_a.getPosition() + move);
|
80
|
-
box_b.setPosition(box_b.getPosition() + move);
|
81
|
-
|
82
|
-
|
83
|
-
}
|
84
|
-
}
|
85
23
|
else
|
86
24
|
{
|
87
|
-
// 矩形の衝突判定
|
88
|
-
if (((a.getPosition().x + a.getSize().x > b.getPosition().x) && (a.getPosition().x < b.getPosition().x + b.getSize().x))
|
89
|
-
&& ((a.getPosition().y + a.getSize().y > b.getPosition().y) && (a.getPosition().y < b.getPosition().y + b.getSize().y)))
|
90
|
-
{
|
91
|
-
return true;
|
92
|
-
}
|
93
|
-
else
|
94
|
-
{
|
95
|
-
|
25
|
+
return false;
|
96
|
-
}
|
97
|
-
|
98
26
|
}
|
99
|
-
|
100
27
|
}
|
101
28
|
else
|
102
29
|
{
|
103
30
|
return false;
|
104
31
|
}
|
105
32
|
}
|
106
|
-
// ################################################################################################################
|
107
33
|
|
108
34
|
```
|