質問編集履歴

3

提示画像を追加

2023/12/11 06:57

投稿

samidare_chan
samidare_chan

スコア17

test CHANGED
File without changes
test CHANGED
@@ -1,5 +1,7 @@
1
1
  ### 質問内容
2
2
  二つの物体body,body2がありますが、bodyにだけ速度を適用して移動させたいのですがなぜかbody2にも適用されてしまい片方だけ移動するということがでません。これはなぜでしょうか?
3
+
4
+ 提示画像のように画面の中央から始まって片方だけ右に移動するはずがなぜか両方とも右に移動してしまいます。
3
5
 
4
6
  ### 知りたいこと
5
7
  bodyにだけ速度を適用したい
@@ -15,6 +17,9 @@
15
17
  box2D: https://box2d.org/documentation/
16
18
  dxlib: https://dxlib.xsrv.jp/dxfunc.html
17
19
  SetLinearVelocity() : https://box2d.org/documentation/classb2_body.html#a832f3989a44f0d4782c80456832197ad
20
+
21
+ ### 提示画像
22
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-12-11/b0825893-1498-4410-96a5-789c2507663e.png)
18
23
  ### コンソールログ
19
24
  ```
20
25
  00000209A36FF540

2

文章を修正

2023/12/11 06:16

投稿

samidare_chan
samidare_chan

スコア17

test CHANGED
File without changes
test CHANGED
@@ -7,18 +7,54 @@
7
7
  ### 調べたこと、試したこと
8
8
  下記のサイトを参考にリファレンスを参照
9
9
  body2にも別の速度を適用して試しました。
10
- 座標を表示
10
+ 座標を表示して二つ移動しているつまり問題点が座標であることを確認
11
11
  chatGDTを活用
12
- SetLinearVelocity: https://box2d.org/documentation/classb2_body.html#a832f3989a44f0d4782c80456832197ad
12
+ 下記のコンソールログを表示してbody,body2が別のものを参照しているか確認、
13
+
13
14
  ### 利用しているライブラリ
14
15
  box2D: https://box2d.org/documentation/
15
16
  dxlib: https://dxlib.xsrv.jp/dxfunc.html
17
+ SetLinearVelocity() : https://box2d.org/documentation/classb2_body.html#a832f3989a44f0d4782c80456832197ad
18
+ ### コンソールログ
19
+ ```
20
+ 00000209A36FF540
21
+ 00000209A36FF600
22
+ Hit
23
+ 200.08
24
+ 400.086
25
+
26
+ 200.205
27
+ 400.212
28
+
29
+ 200.35
30
+ 400.359
31
+
32
+ 200.505
33
+ 400.515
34
+
35
+ 200.667
36
+ 400.677
37
+
38
+ 200.83
39
+ ```
16
40
 
17
41
  ### 該当のソースコード
18
42
  ```cpp
19
43
  #include <DxLib.h>
20
44
  #include <iostream>
21
45
  #include <box2D/Box2D.h>
46
+
47
+ class MyContactListener : public b2ContactListener {
48
+ void BeginContact(b2Contact* contact) {
49
+ // 二つの物体が接触したときの処理
50
+ b2Fixture* fixtureA = contact->GetFixtureA();
51
+ b2Fixture* fixtureB = contact->GetFixtureB();
52
+ // 物体Aと物体Bの情報を使って処理を行う
53
+
54
+ std::cout << "Hit" << std::endl;
55
+ }
56
+ };
57
+
22
58
 
23
59
  // プログラムは WinMain から始まります
24
60
  int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
@@ -39,8 +75,11 @@
39
75
  }
40
76
 
41
77
 
42
- b2Vec2 gravity(0.0f, 0.0f); // 重力ベクトル
78
+ b2Vec2 gravity(0.0f, 9.8f); // 重力ベクトル
43
79
  b2World world(gravity);
80
+
81
+ MyContactListener myContactListener;
82
+ world.SetContactListener(&myContactListener);
44
83
 
45
84
  // ボディの設定
46
85
  b2BodyDef bodyDef;
@@ -67,19 +106,22 @@
67
106
  body->CreateFixture(&dynamicBox, 1.0f); // 密度
68
107
  body2->CreateFixture(&dynamicBox2, 1.0f); // 密度
69
108
 
109
+ std::cout << body << std::endl;
110
+ std::cout << body2 << std::endl;
111
+
70
112
  while (CheckHitKey(KEY_INPUT_ESCAPE) == 0)
71
113
  {
72
114
  ClearDrawScreen();
73
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
115
+
74
- b2Vec2 velocity(50.0f, 0.0f); // X方向に5.0の速度
116
+ b2Vec2 velocity(10.0f, 0.0f); // X方向に5.0の速度
75
117
  b2Vec2 velocity2(0.0f, 0.0f); // X方向に5.0の速度
76
118
 
77
119
  // ボディに初速度を設定
78
120
  body->SetLinearVelocity(velocity);
79
- body2->SetLinearVelocity(velocity2);
121
+ //body2->SetLinearVelocity(velocity2);
80
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
122
+
81
- b2Vec2 force(1000000.0f, 0.0f);
123
+ // b2Vec2 force(1000000.0f, 0.0f);
82
- //body->ApplyForceToCenter(force, true);
124
+ // body->ApplyForceToCenter(force, true);
83
125
 
84
126
 
85
127
  // Box2Dのステップを進める
@@ -87,13 +129,13 @@
87
129
 
88
130
  b2Vec2 body1pos = body->GetPosition();
89
131
  b2Vec2 body2pos = body2->GetPosition();
90
-
132
+
91
133
  std::cout << body1pos.x << std::endl;
92
134
  std::cout << body2pos.x << std::endl;
93
135
  std::cout <<std::endl;
94
136
 
95
137
  // ボディの位置を取得
96
- DrawBox(body1pos.x,body1pos.y,body1pos.x + 100,body1pos.y + 100, GetColor(0, 255, 0),TRUE);
138
+ DrawBox(body1pos.x,body1pos.y,body1pos.x +100,body1pos.y + 100, GetColor(0, 255, 0),TRUE);
97
139
  DrawBox(body2pos.x,body2pos.y,body2pos.x + 100,body2pos.y + 100, GetColor(255, 0, 0),TRUE);
98
140
  // ボックスを描画位置に設定
99
141
 

1

文章を修正

2023/12/11 04:34

投稿

samidare_chan
samidare_chan

スコア17

test CHANGED
File without changes
test CHANGED
@@ -9,7 +9,7 @@
9
9
  body2にも別の速度を適用して試しました。
10
10
  座標を表示
11
11
  chatGDTを活用
12
-
12
+ SetLinearVelocity: https://box2d.org/documentation/classb2_body.html#a832f3989a44f0d4782c80456832197ad
13
13
  ### 利用しているライブラリ
14
14
  box2D: https://box2d.org/documentation/
15
15
  dxlib: https://dxlib.xsrv.jp/dxfunc.html