Repository navigation
Expand file tree
/
Copy pathball.pde
More file actions
499 lines (483 loc) · 14.7 KB
/
Copy pathball.pde
File metadata and controls
499 lines (483 loc) · 14.7 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
ArrayList<Shape> shapes;
Ball ball;
Paddle topPaddle; //上
Paddle bottomPaddle; //下
int maxShapes = 20;
float shapeGenerationChance = 0.01;
float paddleWidth;
float paddleHeight = 10;
float paddleSpeed = 8; //パドルの移動速度
int gameState = 0; //0: プレイ中, 1:ユーザー勝利 , 2: AI勝利(ユーザー敗北)
boolean leftPressed = false;
boolean rightPressed = false;
void setup() {
size(960, 540);
shapes = new ArrayList<Shape>();
ball = new Ball(width/2, height/2, 20);
paddleWidth = width/6;
topPaddle = new Paddle(paddleHeight * 2, paddleWidth, paddleHeight, paddleSpeed);
bottomPaddle = new Paddle(height - paddleHeight * 3, paddleWidth, paddleHeight, paddleSpeed);
gameState = 0;
}
void draw() {
background(240);
if (gameState == 0){
if (random(1) < shapeGenerationChance && shapes.size() < maxShapes){
generateRandomShape();
}
for (int i = shapes.size() - 1; i >= 0; i--) {
Shape shape = shapes.get(i);
shape.update();
shape.display();
if (shape.isOffScreen()) {
shapes.remove(i);
}
}
ball.update();
ball.checkWallCollision();
if (gameState == 0) {
topPaddle.updateAI(ball.position.x);
topPaddle.display();
bottomPaddle.updateUser(leftPressed, rightPressed);
bottomPaddle.display();
for (Shape shape : shapes) {
ball.checkShapeCollision(shape);
}
ball.checkPaddleCollision(topPaddle);
ball.checkPaddleCollision(bottomPaddle);
ball.display();
fill(0);
textSize(16);
text("Shape Count:" + shapes.size(), width - 100, 30);
}
}
if (gameState != 0) {
fill(0);
textSize(48);
textAlign(CENTER, CENTER);
if (gameState == 1) {
text("YOU WIN!", width/2, height/2);
} else if (gameState == 2) {
text("YOU LOSE!", width/2, height/2);
}
textSize(20);
text("Click to Restart", width/2, height/2 + 50);
}
}
void keyPressed() {
if (keyCode == LEFT) {
leftPressed = true;
} else if (keyCode == RIGHT) {
rightPressed = true;
}
}
void keyReleased() {
if (keyCode == LEFT) {
leftPressed = false;
} else if (keyCode == RIGHT) {
rightPressed = false;
}
}
void mousePressed(){
if (gameState != 0) {
shapes.clear();
ball = new Ball(width/2, height/2, 20);
topPaddle.x = width/2;
bottomPaddle.x = width/2;
gameState = 0;
textAlign(LEFT);
}
}
void generateRandomShape() {
int shapeType = int(random(3)); //生成する図形の種類 0: 円, 1: 四角形, 2: 三角形
float y = random(100, height - 100);
float maxSize = sqrt((width * height) / 10.0); // 生成される図形の大きさを制限
float moveSpeed = random(1, 3);
boolean moveRight = random(1) > 0.5; //移動方向もランダムに決定
float x = moveRight ? -maxSize : width + maxSize; //画面外から登場させる
switch (shapeType) {
case 0:
float radius = random(10, maxSize/2);
shapes.add(new Circle(x, y, radius, moveSpeed, moveRight));
break;
case 1:
float w = random(10, maxSize);
float h = random(10, maxSize);
if (w * h > maxSize * maxSize) {
float scale = sqrt((maxSize * maxSize) / (w * h));
w *= scale;
h *= scale;
}
shapes.add(new Rectangle(x, y, w, h, moveSpeed, moveRight));
break;
case 2:
float size = random(10, maxSize);
shapes.add(new Triangle(x, y, size, moveSpeed, moveRight));
}
}
// 図形の抽象クラス
abstract class Shape {
float x, y;
color col;
float moveSpeed;
boolean moveRight;
PVector velocity; //図形の速度ベクトル
Shape(float x, float y, float moveSpeed, boolean moveRight) {
this.x = x;
this.y = y;
this.col = color(0); //黒色
this.moveSpeed = moveSpeed;
this.moveRight = moveRight;
this.velocity = new PVector(moveRight ? moveSpeed : -moveSpeed, 0);
}
void update() {
// 水平方向にのみ移動
x += velocity.x;
}
abstract void display();
abstract boolean checkCollision(float px, float py, float radius);
abstract PVector getCollisionNormal(float px, float py);
abstract boolean isOffScreen(); //画面外判定
PVector getVelocity() {
return velocity.copy();
}
}
class Circle extends Shape {
float radius;
Circle(float x, float y, float radius, float moveSpeed, boolean moveRight) {
super(x, y, moveSpeed, moveRight);
this.radius = radius;
}
void display() {
fill(col);
noStroke();
ellipse(x, y, radius * 2, radius * 2);
}
boolean checkCollision(float px, float py, float ballRadius) {
float distance = dist(x, y, px, py);
return distance < radius + ballRadius;
}
PVector getCollisionNormal(float px, float py) {
PVector normal = new PVector(px - x, py - y);
normal.normalize();
return normal;
}
boolean isOffScreen() {
return (moveRight && x - radius > width) || (!moveRight && x + radius < 0);
}
}
class Rectangle extends Shape {
float w, h;
Rectangle(float x, float y, float w, float h, float moveSpeed, boolean moveRight) {
super(x, y, moveSpeed, moveRight);
this.w = w;
this.h = h;
}
void display() {
fill(col);
noStroke();
rectMode(CENTER);
rect(x, y, w, h);
}
boolean checkCollision(float px, float py, float ballRadius) {
float testX = px;
float testY = py;
//ボールの中心が四角形のどの辺に最も近いかを調べる
if (px < x - w/2) testX = x - w/2;
else if (px > x + w/2) testX = x + w/2;
if (py < y - h/2) testY = y - h/2;
else if (py > y + h/2) testY = y + h/2;
float distance = dist(px, py, testX, testY);
return distance < ballRadius;
}
PVector getCollisionNormal(float px, float py) {
float testX = px;
float testY = py;
if (px < x - w/2) testX = x - w/2;
else if (px > x + w/2) testX = x + w/2;
if (py < y - h/2) testY = y - h/2;
else if (py > y + h/2) testY = y + h/2;
// ボールが四角形の角に衝突する場合
if (testX != px && testY != py) {
PVector normal = new PVector(px - testX, py - testY);
normal.normalize();
return normal;
}
// ボールが四角形の辺に衝突する場合
else if (testX != px) {
return new PVector((px - testX > 0) ? 1 : -1, 0);
}
else if (testY != py) {
return new PVector(0, (py - testY > 0) ? 1 : -1);
}
// ボールが四角形の内部にある場合(通常は発生しないはず)
else {
// デフォルトの法線(上向き)
return new PVector(0, -1);
}
}
boolean isOffScreen() {
return (moveRight && x - w/2 > width) || (!moveRight && x + w/2 < 0);
}
}
class Triangle extends Shape {
float size;
PVector[] vertices;
Triangle(float x, float y, float size, float moveSpeed, boolean moveRight) {
super(x, y, moveSpeed, moveRight);
this.size = size;
updateVertices();
}
void update() {
super.update();
updateVertices();
}
// 頂点位置を更新
void updateVertices() {
vertices = new PVector[3];
vertices[0] = new PVector(x, y - size/2);
vertices[1] = new PVector(x - size/2, y + size/2);
vertices[2] = new PVector(x + size/2, y + size/2);
}
void display() {
fill(col);
noStroke();
triangle(
vertices[0].x, vertices[0].y,
vertices[1].x, vertices[1].y,
vertices[2].x, vertices[2].y
);
}
boolean checkCollision(float px, float py, float ballRadius) {
for (int i = 0; i < 3; i++) {
PVector v1 = vertices[i];
PVector v2 = vertices[(i+1) % 3];
PVector edge = PVector.sub(v2, v1);
PVector ballTov1 = new PVector(px - v1.x, py - v1.y);
float dot = ballTov1.dot(edge) / edge.magSq();
dot = constrain(dot, 0, 1);
PVector closest = new PVector(
v1.x + dot * edge.x,
v1.y + dot * edge.y
);
float distance = dist(px, py, closest.x, closest.y);
if (distance < ballRadius) {
return true;
}
}
boolean inside = pointInTriangle(px, py);
return inside;
}
boolean pointInTriangle(float px, float py) {
boolean b1 = sign(px, py, vertices[0].x, vertices[0].y, vertices[1].x, vertices[1].y) < 0;
boolean b2 = sign(px, py, vertices[1].x, vertices[1].y, vertices[2].x, vertices[2].y) < 0;
boolean b3 = sign(px, py, vertices[2].x, vertices[2].y, vertices[1].x, vertices[1].y) < 0;
return ((b1 == b2) && (b2 == b3));
}
float sign(float x1, float y1, float x2, float y2, float x3, float y3) {
return (x1 - x3) * (y2 - y3) - (x2 - x3) * (y1 - y3);
}
PVector getCollisionNormal(float px, float py) {
PVector normal = new PVector(0, 0);
float minDist = Float.MAX_VALUE;
PVector closestPoint = null;
for (int i = 0; i < 3; i++) {
PVector v1 = vertices[i];
PVector v2 = vertices[(i + 1) % 3];
PVector edge = PVector.sub(v2, v1);
PVector ballTov1 = new PVector(px - v1.x, py - v1.y);
float dot = ballTov1.dot(edge) / edge.magSq();
dot = constrain(dot, 0, 1);
PVector closest = new PVector(
v1.x + dot * edge.x,
v1.y + dot * edge.y
);
float distance = dist(px, py, closest.x, closest.y);
if (distance < minDist) {
minDist = distance;
closestPoint = closest;
}
}
if (closestPoint != null) {
normal = new PVector(px - closestPoint.x, py - closestPoint.y);
normal.normalize();
}
return normal;
}
boolean isOffScreen() {
return (moveRight && x - size/2 > width) || (!moveRight && x + size/2 < 0);
}
}
class Paddle {
float x, y;
float w, h;
float speed;
color col;
Paddle(float yPos, float width, float height, float speed) {
this.x = width/2;
this.y = yPos;
this.w = width;
this.h = height;
this.speed = speed;
this.col = color(80);
}
void display() {
fill(col);
noStroke();
rectMode(CENTER);
rect(x, y, w, 5);
}
void updateAI(float ballX) {
float targetX = ballX;
if (x < targetX) {
x += speed;
} else if (x > targetX) {
x -= speed;
}
keepInBounds();
}
void updateUser(boolean moveLeft, boolean moveRight) {
if (moveLeft) {
x -= speed;
} else if (moveRight) {
x += speed;
}
keepInBounds();
}
void keepInBounds() {
x = constrain(x, w/2, width - w/2);
}
boolean checkCollision(Ball ball) {
boolean xOverlap = ball.position.x > x - w/2 && ball.position.x < x + w/2;
boolean yOverlap = abs(ball.position.y - y) < ball.radius;
if (xOverlap && yOverlap) {
return true;
}
return false;
}
PVector getCollisionNormal(Ball ball) {
if (ball.position.y < y) {
return new PVector(0, -1);
} else {
return new PVector(0, 1);
}
}
}
class Ball {
PVector position;
PVector velocity;
float radius;
color col;
float restitution = 1.0; // 反発係数
boolean justCollided = false;
int collisionCooldown = 0;
float speed = 5; //ボールに設定する一定の速さ
Ball(float x, float y, float radius) {
position = new PVector(x, y);
// velocity = PVector.random2D();
velocity = new PVector(random(-1, 1), random(0.5, 1)); // Xはランダム、Yは下向き成分多め
// velocity.mult(5);
velocity.normalize();
velocity.mult(speed);
this.radius = radius;
this.col = color(50, 100, 200);
}
void update() {
if (gameState == 0){
position.add(velocity);
}
if(collisionCooldown > 0) {
collisionCooldown--;
if (collisionCooldown == 0) {
justCollided = false;
}
}
}
void display() {
fill(col);
noStroke();
ellipse(position.x, position.y, radius * 2, radius * 2);
}
void checkWallCollision() {
boolean collided = false;
if (position.x - radius < 0) {
velocity.x = abs(velocity.x);// 右向きに
position.x = radius;
collided = true;
} else if(position.x + radius > width) {
velocity.x = -abs(velocity.x); //左向きに
position.x = width - radius;
collided = true;
}
if (position.y - radius < 0) {
// velocity.y = abs(velocity.y); //下向きに
// position.y = radius;
// collided = true;
gameState = 1; //ユーザー勝利
return;
} else if (position.y + radius > height) {
// velocity.y = -abs(velocity.y); //上向きに
// position.y = height - radius;
// collided = true;
gameState = 2; //ユーザー敗北
return;
}
if (collided) {
velocity.normalize();
velocity.mult(speed);
justCollided = true;
collisionCooldown = 1; // 連続で起きないように数フレームのクールダウン期間を確保
}
}
//パドルとの衝突判定(クールダウン無視)
void checkPaddleCollision(Paddle paddle) {
if (paddle.checkCollision(this)) {
PVector normal = paddle.getCollisionNormal(this); //パドルからの法線を取得
float dotProduct = velocity.dot(normal);
if (dotProduct < 0) {
float j = -(1 + restitution) * dotProduct;
PVector impulse = PVector.mult(normal, j);
velocity.add(impulse);
velocity.normalize();
velocity.mult(speed);
//めり込み解消
float overlap = ball.radius - abs(position.y - paddle.y);
if (overlap > 0) {
position.add(PVector.mult(normal, overlap + 1));
}
}
}
}
void checkShapeCollision(Shape shape) {
if (justCollided) return; //直前に衝突したばかりならスキップ。こうすることで、ボールが縁をそってブレることを回避する
if (shape.checkCollision(position.x, position.y, radius)) {
PVector normal = shape.getCollisionNormal(position.x, position.y);
//動く物体との相対速度を考慮
PVector shapeVelocity = shape.getVelocity();
PVector relativeVelocity = PVector.sub(velocity, shapeVelocity);
//相対速度と法線の内積を計算
float dotProduct = relativeVelocity.dot(normal);
if (dotProduct < 0) {
//力積計算
float j = -(1 + restitution) * dotProduct;
//normalは衝突面に対する法線法線ベクトル。normalにスカラー量jをかけている
PVector impulse = PVector.mult(normal, j);
PVector velocityBeforeCollision = velocity.copy();
velocity.add(impulse);
// velocity.add(shapeVelocity);
velocity.normalize();
velocity.mult(speed);
float penetrationDepth = radius - PVector.dist(position, new PVector(
position.x - normal.x * radius,
position.y - normal.y * radius
));
if (penetrationDepth > 0){
//めり込んでいる場合は法線方向に押し戻す
position.add(PVector.mult(normal, penetrationDepth + 1));
}
justCollided = true;
collisionCooldown = 1;
}
}
}
}