teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

タイトルと文章を編集しました。

2021/02/03 02:27

投稿

退会済みユーザー
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 Intersect_2D(Box_Collision_2D& a, Box_Collision_2D& b)
12
+ bool Rect_and_Rect_2D(Box_Collision_2D& a, Box_Collision_2D& b)
14
- {
13
+ {
15
- int maxSpeed = 20;
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
- int aa = a.getMove().length();
16
+ {
22
-
23
- int bb = b.getMove().length();
24
-
25
- printf("aa: %d\n", aa);
26
- //printf("bb: %d\n",bb);
17
+ // 矩形の衝突判定
27
-
28
-
29
-
30
- int speed = 0;
31
- if (aa > bb)
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
- speed = aa / maxSpeed;
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
- return false;
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
  ```