回答編集履歴

4

参考情報を追記

2018/05/03 10:17

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -20,6 +20,12 @@
20
20
 
21
21
  宗教上の理由などがある場合を除き、`float`型の精度に起因する余計なトラブルを持ち込みむため、避けたほうが無難です。
22
22
 
23
+ この精度についての問題について、日本語で体系的に纏まっている情報としては2008年に出版された本ですが、「ゲームプログラマになる前に覚えておきたい技術」が纏まっています。
24
+
25
+ 「ゲームプログラマになる前に覚えておきたい技術 float 中途半端な数による誤差」でキーワード検索してみてくださいな。
26
+
27
+
28
+
23
29
 
24
30
 
25
31
  サンプルコードです。ご参考まで。

3

コードを変更

2018/05/03 10:17

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  発射した弾の生存管理方法はいろいろな手法がありますが、
14
14
 
15
- 描画範囲外になったら生存フラグをgalseにして、描画時にリストから削除する形が楽かと。
15
+ 描画範囲外になったら生存フラグを`false`にして、描画時にリストから削除する形が楽かと。
16
16
 
17
17
 
18
18
 
@@ -28,9 +28,9 @@
28
28
 
29
29
  Bullet bullet;
30
30
 
31
- Boss boss;
31
+ private Boss boss = new Boss(100, 150);
32
-
32
+
33
- ArrayList<Bullet> danmaku;
33
+ private ArrayList<Bullet> danmaku = new ArrayList<Bullet>();
34
34
 
35
35
 
36
36
 
@@ -50,10 +50,6 @@
50
50
 
51
51
  bullet = new Bullet(width/2, height/2, 10, 0, 0 ); //クラスからインスタンスを生成
52
52
 
53
- boss = new Boss(100, 200);
54
-
55
- danmaku = new ArrayList<Bullet>();
56
-
57
53
 
58
54
 
59
55
  }
@@ -88,9 +84,11 @@
88
84
 
89
85
  class Boss {
90
86
 
91
- int tx, ty;
87
+ private int tx, ty;
88
+
92
-
89
+ private int dx, dy;
90
+
93
- long routine = 0;
91
+ private long routine = 0;
94
92
 
95
93
  Boss(int x, int y) {
96
94
 
@@ -98,13 +96,29 @@
98
96
 
99
97
  ty = y;
100
98
 
99
+ dx = 1;
100
+
101
+ dy = -5;
102
+
101
103
  }
102
104
 
103
105
  void move(){
104
106
 
105
- tx += 1;
107
+ tx += dx;
106
-
108
+
107
- ty += 1;
109
+ ty += dy;
110
+
111
+ if(tx < 0 || tx > width){
112
+
113
+ dx*= -1;
114
+
115
+ }
116
+
117
+ if(ty < 0 || ty > height){
118
+
119
+ dy*= -1;
120
+
121
+ }
108
122
 
109
123
  }
110
124
 
@@ -122,6 +136,8 @@
122
136
 
123
137
  danmaku.addAll(this.shot());
124
138
 
139
+ println("#shot!!" +routine + ":" + tx + "," + ty);
140
+
125
141
  }
126
142
 
127
143
  }
@@ -132,9 +148,9 @@
132
148
 
133
149
  for (int i = 0; i < 360; i+= 10) {
134
150
 
135
- float rad = radians(i);
151
+ double rad = radians(i);
136
-
152
+
137
- danmaku.add(new Bullet(this.tx, this.tx, 10, cos(rad), sin(rad)));
153
+ danmaku.add(new Bullet(this.tx, this.ty, 10, Math.cos(rad), Math.sin(rad)));
138
154
 
139
155
  }
140
156
 
@@ -152,11 +168,15 @@
152
168
 
153
169
 
154
170
 
171
+ private double tx, ty;
172
+
173
+ private final double tr;
174
+
155
- float tx, ty, tr, dx, dy;
175
+ private double dx, dy;
156
-
176
+
157
- boolean isalive = true;
177
+ private boolean is_alive = true;
158
-
178
+
159
- Bullet(float x, float y, float r, float temp_dx, float temp_dy ) {
179
+ Bullet(double x, double y, double r, double temp_dx, double temp_dy ) {
160
180
 
161
181
  tx = x;
162
182
 
@@ -172,7 +192,7 @@
172
192
 
173
193
  boolean isAlive(){
174
194
 
175
- return isalive;
195
+ return is_alive;
176
196
 
177
197
  }
178
198
 
@@ -184,7 +204,7 @@
184
204
 
185
205
  if(Math.min(tx, ty) < 0){
186
206
 
187
- isalive = false;
207
+ is_alive = false;
188
208
 
189
209
  //println("#" + tx + "," + ty);
190
210
 
@@ -194,9 +214,7 @@
194
214
 
195
215
  if(tx > width || ty > height){
196
216
 
197
- isalive = false;
217
+ is_alive = false;
198
-
199
- println("#" + tx + "," + ty);
200
218
 
201
219
  return ;
202
220
 
@@ -206,7 +224,7 @@
206
224
 
207
225
 
208
226
 
209
- ellipse(tx, ty, tr, tr);
227
+ ellipse((float)tx, (float)ty, (float)tr, (float)tr);
210
228
 
211
229
  }
212
230
 

2

4枠の範囲を追加

2018/05/02 21:36

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -32,6 +32,8 @@
32
32
 
33
33
  ArrayList<Bullet> danmaku;
34
34
 
35
+
36
+
35
37
  void setup() {
36
38
 
37
39
  size(320, 480);
@@ -86,11 +88,11 @@
86
88
 
87
89
  class Boss {
88
90
 
89
- float tx, ty;
91
+ int tx, ty;
90
92
 
91
93
  long routine = 0;
92
94
 
93
- Boss(float x, float y) {
95
+ Boss(int x, int y) {
94
96
 
95
97
  tx = x;
96
98
 
@@ -180,15 +182,25 @@
180
182
 
181
183
  ty += dy;
182
184
 
183
- if(tx < 0 || ty < 0){
185
+ if(Math.min(tx, ty) < 0){
184
186
 
185
187
  isalive = false;
186
188
 
189
+ //println("#" + tx + "," + ty);
190
+
187
191
  return ;
188
192
 
189
193
  }
190
194
 
191
-
195
+ if(tx > width || ty > height){
196
+
197
+ isalive = false;
198
+
199
+ println("#" + tx + "," + ty);
200
+
201
+ return ;
202
+
203
+ }
192
204
 
193
205
  stroke(255, 0, 0);
194
206
 

1

追記

2018/05/02 12:58

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  今回の場合は`Boss`クラスになるかと。
8
8
 
9
- `Boss`クラスは任意のタイミングで、弾幕を貼る。
9
+ `Boss`クラスは任意のタイミングで、弾幕を貼る。そのようなコードになります。
10
10
 
11
11
 
12
12
 
@@ -18,7 +18,7 @@
18
18
 
19
19
  あと変数の型を`double`型ではなく`float`型で宣言するのは、
20
20
 
21
- 宗教上の理由などがある場合を除き、`float`型の精度に起因する余計なトラブルを持ち込みやすいため、避けたほうが無難です。
21
+ 宗教上の理由などがある場合を除き、`float`型の精度に起因する余計なトラブルを持ち込みため、避けたほうが無難です。
22
22
 
23
23
 
24
24