-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquare.pde
More file actions
executable file
·137 lines (120 loc) · 3.34 KB
/
Copy pathSquare.pde
File metadata and controls
executable file
·137 lines (120 loc) · 3.34 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
/*
動かない四角(角の丸みの設定可)の物体
*/
class Square{
PVector p;
int sizeX;
int sizeY;
int r;
float e;
boolean onFlag;
int type;
Square(int x, int y, int sizeX, int sizeY, int r, int type){
p = new PVector(x,y);
this.sizeX = sizeX;
this.sizeY = sizeY;
this.r = r;
this.type = type;
if(type == 1)
e = 0.8;
if(type == 2)
e = 2.5;
if(type == 3)
e = 0;
}
void show(){
line(p.x +r, p.y , p.x +sizeX -r, p.y);
line(p.x +r, p.y +sizeY , p.x +sizeX -r, p.y + sizeY);
line(p.x , p.y +r, p.x , p.y +sizeY -r);
line(p.x + sizeX, p.y +r, p.x +sizeX , p.y +sizeY -r);
noFill();
arc(p.x +r, p.y +r, r*2, r*2, PI, PI+ HALF_PI);
arc(p.x +sizeX -r, p.y +r, r*2, r*2, PI+ HALF_PI, TWO_PI);
arc(p.x +sizeX -r, p.y +sizeY -r, r*2, r*2, 0, HALF_PI);
arc(p.x +r, p.y +sizeY -r, r*2, r*2, HALF_PI, PI);
textSize(20);
fill(0);
textAlign(CENTER, CENTER);
if(type == 2){
fill(255,0,0);
text("HOP", p.x +sizeX/2, p.y +sizeY/2);
}
if(type == 3){
fill(0,0,255);
text("GUM", p.x +sizeX/2, p.y +sizeY/2);
}
}
void collision( Ball ball, int ballNum){
float elas = e * ball.getElas();
PVector[] calcResult;
if(ballNum == 0)
onFlag = false;
if( ball.getVy() >= 0){
if(ball.getY() > p.y -ball.getR() && ball.getY() < p.y +sizeY
&& ball.getX() > p.x +r && ball.getX() < p.x +sizeX -r){
ball.setY( p.y -ball.getR() );
ball.setVy( -1 *elas *ball.getVy() );
if(ballNum == 0)
onFlag = true;
}
}
else{
if(ball.getY() > p.y && ball.getY() < p.y +sizeY +ball.getR()
&& ball.getX() > p.x +r && ball.getX() < p.x +sizeX -r){
ball.setY( p.y +sizeY +ball.getR() );
ball.setVy( -1 *elas *ball.getVy() );
}
}
if( ball.getVx() >= 0){
if(ball.getX() > p.x -ball.getR() && ball.getX() < p.x +sizeX
&& ball.getY() > p.y +r && ball.getY() < p.y +sizeY -r){
ball.setX( p.x -ball.getR() );
ball.setVx( -1 *elas *ball.getVx() );
}
}
else{
if(ball.getX() > p.x && ball.getX() < p.x +sizeX +ball.getR()
&& ball.getY() > p.y +r && ball.getY() < p.y +sizeY -r){
ball.setX( p.x +sizeX +ball.getR() );
ball.setVx( -1 *elas *ball.getVx() );
}
}
for(int j=0; j<2; j++){
for(int i=0; i<2; i++){
if( ball.twoCircleMatch(ball.getP(),
PVector.add(p, new PVector(r +i*(sizeX -2*r), r +j*(sizeY -2*r)) ) ,ball.getR(), r ) ){
//衝突後の速度と位置を取得
calcResult = ball.clacCircleCollision(
ball.getP(), PVector.add(p, new PVector(r +i*(sizeX -2*r), r +j*(sizeY -2*r))),
ball.getV(), new PVector(0,0), ball.getR(), r, 0, 1, ball.getElas(), elas
);
//結果をそれぞれ代入
ball.setV( calcResult[0] );
ball.setP( calcResult[2] );
}
}
}
}
//以下、ゲッター
PVector getP(){
return p;
}
int getX(){
return int(p.x);
}
int getY(){
return int(p.y);
}
int getSizeX(){
return sizeX;
}
int getSizeY(){
return sizeY;
}
int getR(){
return r;
}
boolean getOnFlag(){
return onFlag;
}
}