回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,59 +1,60 @@
|
|
1
|
-
「そういうことじゃない!」ってのは承知しています^^;
|
2
|
-
ライブラリを使うとどのくらい簡単になるか試しました。
|
3
|
-
|
4
|
-
```Processing
|
5
|
-
import fisica.*;
|
6
|
-
|
7
|
-
FWorld world;
|
8
|
-
|
9
|
-
void setup() {
|
10
|
-
size(500, 500);
|
11
|
-
colorMode(HSB);
|
12
|
-
|
13
|
-
Fisica.init(this);
|
14
|
-
world = new FWorld();
|
15
|
-
world.setEdges();
|
16
|
-
world.setGravity(0, 0);
|
17
|
-
world.setGrabbable(false);
|
18
|
-
|
19
|
-
for (int i = 0; i < 3; i++) {
|
20
|
-
FBody ball = createBall();
|
21
|
-
ball.setPosition(random(50, width - 50), random(50, height - 50));
|
22
|
-
world.add(ball);
|
23
|
-
}
|
24
|
-
}
|
25
|
-
|
26
|
-
void draw() {
|
27
|
-
background(255);
|
28
|
-
world.step();
|
29
|
-
world.draw();
|
30
|
-
}
|
31
|
-
|
32
|
-
void contactStarted(FContact contact) {
|
33
|
-
FBody b1 = contact.getBody1();
|
34
|
-
FBody b2 = contact.getBody2();
|
35
|
-
if (b1.isStatic() || b2.isStatic()) return;
|
36
|
-
if (b1.getJoints().size() != 0 && b2.getJoints().size() != 0) return;
|
37
|
-
|
38
|
-
FRevoluteJoint joint = new FRevoluteJoint(b1, b2);
|
39
|
-
joint.setDrawable(false);
|
40
|
-
world.add(joint);
|
41
|
-
}
|
42
|
-
|
43
|
-
FBody createBall() {
|
44
|
-
FCircle ball = new FCircle(random(40, 60));
|
45
|
-
ball.setDamping(0);
|
46
|
-
ball.setRestitution(1);
|
47
|
-
ball.setVelocity(random(-300, 300), random(-300, 300));
|
48
|
-
ball.setFillColor(color(random(256), 128, 255));
|
49
|
-
ball.setNoStroke();
|
50
|
-
return ball;
|
51
|
-
}
|
52
|
-
|
53
|
-
// ちょい寂しいのでおまけ
|
54
|
-
void mousePressed() {
|
55
|
-
FBody ball = createBall();
|
56
|
-
ball.setPosition(mouseX, mouseY);
|
57
|
-
world.add(ball);
|
58
|
-
}
|
59
|
-
```
|
1
|
+
「そういうことじゃない!」ってのは承知しています^^;
|
2
|
+
ライブラリを使うとどのくらい簡単になるか試しました。
|
3
|
+
|
4
|
+
```Processing
|
5
|
+
import fisica.*;
|
6
|
+
|
7
|
+
FWorld world;
|
8
|
+
|
9
|
+
void setup() {
|
10
|
+
size(500, 500);
|
11
|
+
colorMode(HSB);
|
12
|
+
|
13
|
+
Fisica.init(this);
|
14
|
+
world = new FWorld();
|
15
|
+
world.setEdges();
|
16
|
+
world.setGravity(0, 0);
|
17
|
+
world.setGrabbable(false);
|
18
|
+
|
19
|
+
for (int i = 0; i < 3; i++) {
|
20
|
+
FBody ball = createBall();
|
21
|
+
ball.setPosition(random(50, width - 50), random(50, height - 50));
|
22
|
+
world.add(ball);
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
void draw() {
|
27
|
+
background(255);
|
28
|
+
world.step();
|
29
|
+
world.draw();
|
30
|
+
}
|
31
|
+
|
32
|
+
void contactStarted(FContact contact) {
|
33
|
+
FBody b1 = contact.getBody1();
|
34
|
+
FBody b2 = contact.getBody2();
|
35
|
+
if (b1.isStatic() || b2.isStatic()) return;
|
36
|
+
if (b1.getJoints().size() != 0 && b2.getJoints().size() != 0) return;
|
37
|
+
|
38
|
+
FRevoluteJoint joint = new FRevoluteJoint(b1, b2);
|
39
|
+
joint.setDrawable(false);
|
40
|
+
world.add(joint);
|
41
|
+
}
|
42
|
+
|
43
|
+
FBody createBall() {
|
44
|
+
FCircle ball = new FCircle(random(40, 60));
|
45
|
+
ball.setDamping(0);
|
46
|
+
ball.setRestitution(1);
|
47
|
+
ball.setVelocity(random(-300, 300), random(-300, 300));
|
48
|
+
ball.setFillColor(color(random(256), 128, 255));
|
49
|
+
ball.setNoStroke();
|
50
|
+
return ball;
|
51
|
+
}
|
52
|
+
|
53
|
+
// ちょい寂しいのでおまけ
|
54
|
+
void mousePressed() {
|
55
|
+
FBody ball = createBall();
|
56
|
+
ball.setPosition(mouseX, mouseY);
|
57
|
+
world.add(ball);
|
58
|
+
}
|
59
|
+
```
|
60
|
+
[fisica](http://www.ricardmarxer.com/fisica/)
|