jaroslav@1877
|
1 |
/**
|
jaroslav@1877
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@1877
|
3 |
* Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1877
|
4 |
*
|
jaroslav@1877
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@1877
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@1877
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@1877
|
8 |
*
|
jaroslav@1877
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@1877
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@1877
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@1877
|
12 |
* GNU General Public License for more details.
|
jaroslav@1877
|
13 |
*
|
jaroslav@1877
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@1877
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@1877
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@1877
|
17 |
*/
|
jaroslav@1877
|
18 |
package org.apidesign.benchmark.jbox2d;
|
jaroslav@1877
|
19 |
|
jaroslav@1877
|
20 |
/*
|
jaroslav@1877
|
21 |
* Copyright 2014 Alexey Andreev.
|
jaroslav@1877
|
22 |
*
|
jaroslav@1877
|
23 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
jaroslav@1877
|
24 |
* you may not use this file except in compliance with the License.
|
jaroslav@1877
|
25 |
* You may obtain a copy of the License at
|
jaroslav@1877
|
26 |
*
|
jaroslav@1877
|
27 |
* http://www.apache.org/licenses/LICENSE-2.0
|
jaroslav@1877
|
28 |
*
|
jaroslav@1877
|
29 |
* Unless required by applicable law or agreed to in writing, software
|
jaroslav@1877
|
30 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
jaroslav@1877
|
31 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
jaroslav@1877
|
32 |
* See the License for the specific language governing permissions and
|
jaroslav@1877
|
33 |
* limitations under the License.
|
jaroslav@1877
|
34 |
*/
|
jaroslav@1877
|
35 |
|
jaroslav@1877
|
36 |
import org.jbox2d.collision.shapes.CircleShape;
|
jaroslav@1877
|
37 |
import org.jbox2d.collision.shapes.PolygonShape;
|
jaroslav@1877
|
38 |
import org.jbox2d.common.Vec2;
|
jaroslav@1877
|
39 |
import org.jbox2d.dynamics.*;
|
jaroslav@1877
|
40 |
import org.jbox2d.dynamics.joints.RevoluteJointDef;
|
jaroslav@1877
|
41 |
|
jaroslav@1877
|
42 |
/** Taken from TeaVM.
|
jaroslav@1877
|
43 |
* <a href="https://github.com/konsoletyper/teavm/blob/3341df3668bbe3f0c3ed78b621e311361c88acfc/samples/benchmark/src/main/java/org/teavm/samples/benchmark/Scene.java">Sample</a>
|
jaroslav@1877
|
44 |
* is licensed under Apache license.
|
jaroslav@1877
|
45 |
*
|
jaroslav@1877
|
46 |
* @author Alexey Andreev
|
jaroslav@1877
|
47 |
*/
|
jaroslav@1877
|
48 |
public class Scene {
|
jaroslav@1877
|
49 |
private World world;
|
jaroslav@1877
|
50 |
private Body axis;
|
jaroslav@1877
|
51 |
private Body reel;
|
jaroslav@1877
|
52 |
private long lastCalculated;
|
jaroslav@1877
|
53 |
private long startTime;
|
jaroslav@1877
|
54 |
|
jaroslav@1877
|
55 |
public Scene() {
|
jaroslav@1877
|
56 |
world = new World(new Vec2(0, -9.8f));
|
jaroslav@1877
|
57 |
initAxis();
|
jaroslav@1877
|
58 |
initReel();
|
jaroslav@1877
|
59 |
joinReelToAxis();
|
jaroslav@1877
|
60 |
initBalls();
|
jaroslav@1877
|
61 |
lastCalculated = System.currentTimeMillis();
|
jaroslav@1877
|
62 |
startTime = lastCalculated;
|
jaroslav@1877
|
63 |
}
|
jaroslav@1877
|
64 |
|
jaroslav@1877
|
65 |
private void initAxis() {
|
jaroslav@1877
|
66 |
BodyDef axisDef = new BodyDef();
|
jaroslav@1877
|
67 |
axisDef.type = BodyType.STATIC;
|
jaroslav@1877
|
68 |
axisDef.position = new Vec2(3, 3);
|
jaroslav@1877
|
69 |
axis = world.createBody(axisDef);
|
jaroslav@1877
|
70 |
|
jaroslav@1877
|
71 |
CircleShape axisShape = new CircleShape();
|
jaroslav@1877
|
72 |
axisShape.setRadius(0.02f);
|
jaroslav@1877
|
73 |
axisShape.m_p.set(0, 0);
|
jaroslav@1877
|
74 |
|
jaroslav@1877
|
75 |
FixtureDef axisFixture = new FixtureDef();
|
jaroslav@1877
|
76 |
axisFixture.shape = axisShape;
|
jaroslav@1877
|
77 |
axis.createFixture(axisFixture);
|
jaroslav@1877
|
78 |
}
|
jaroslav@1877
|
79 |
|
jaroslav@1877
|
80 |
private void initReel() {
|
jaroslav@1877
|
81 |
BodyDef reelDef = new BodyDef();
|
jaroslav@1877
|
82 |
reelDef.type = BodyType.DYNAMIC;
|
jaroslav@1877
|
83 |
reelDef.position = new Vec2(3, 3);
|
jaroslav@1877
|
84 |
reel = world.createBody(reelDef);
|
jaroslav@1877
|
85 |
|
jaroslav@1877
|
86 |
FixtureDef fixture = new FixtureDef();
|
jaroslav@1877
|
87 |
fixture.friction = 0.5f;
|
jaroslav@1877
|
88 |
fixture.restitution = 0.4f;
|
jaroslav@1877
|
89 |
fixture.density = 1;
|
jaroslav@1877
|
90 |
|
jaroslav@1877
|
91 |
int parts = 30;
|
jaroslav@1877
|
92 |
for (int i = 0; i < parts; ++i) {
|
jaroslav@1877
|
93 |
PolygonShape shape = new PolygonShape();
|
jaroslav@1877
|
94 |
double angle1 = i / (double) parts * 2 * Math.PI;
|
jaroslav@1877
|
95 |
double x1 = 2.7 * Math.cos(angle1);
|
jaroslav@1877
|
96 |
double y1 = 2.7 * Math.sin(angle1);
|
jaroslav@1877
|
97 |
double angle2 = (i + 1) / (double) parts * 2 * Math.PI;
|
jaroslav@1877
|
98 |
double x2 = 2.7 * Math.cos(angle2);
|
jaroslav@1877
|
99 |
double y2 = 2.7 * Math.sin(angle2);
|
jaroslav@1877
|
100 |
double angle = (angle1 + angle2) / 2;
|
jaroslav@1877
|
101 |
double x = 0.01 * Math.cos(angle);
|
jaroslav@1877
|
102 |
double y = 0.01 * Math.sin(angle);
|
jaroslav@1877
|
103 |
|
jaroslav@1877
|
104 |
shape.set(new Vec2[] { new Vec2((float) x1, (float) y1), new Vec2((float) x2, (float) y2),
|
jaroslav@1877
|
105 |
new Vec2((float) (x2 - x), (float) (y2 - y)), new Vec2((float) (x1 - x), (float) (y1 - y)) }, 4);
|
jaroslav@1877
|
106 |
fixture.shape = shape;
|
jaroslav@1877
|
107 |
reel.createFixture(fixture);
|
jaroslav@1877
|
108 |
}
|
jaroslav@1877
|
109 |
}
|
jaroslav@1877
|
110 |
|
jaroslav@1877
|
111 |
private void initBalls() {
|
jaroslav@1877
|
112 |
float ballRadius = 0.15f;
|
jaroslav@1877
|
113 |
|
jaroslav@1877
|
114 |
BodyDef ballDef = new BodyDef();
|
jaroslav@1877
|
115 |
ballDef.type = BodyType.DYNAMIC;
|
jaroslav@1877
|
116 |
FixtureDef fixtureDef = new FixtureDef();
|
jaroslav@1877
|
117 |
fixtureDef.friction = 0.3f;
|
jaroslav@1877
|
118 |
fixtureDef.restitution = 0.3f;
|
jaroslav@1877
|
119 |
fixtureDef.density = 0.2f;
|
jaroslav@1877
|
120 |
CircleShape shape = new CircleShape();
|
jaroslav@1877
|
121 |
shape.m_radius = ballRadius;
|
jaroslav@1877
|
122 |
fixtureDef.shape = shape;
|
jaroslav@1877
|
123 |
|
jaroslav@1877
|
124 |
for (int i = 0; i < 5; ++i) {
|
jaroslav@1877
|
125 |
for (int j = 0; j < 5; ++j) {
|
jaroslav@1877
|
126 |
float x = (j + 0.5f) * (ballRadius * 2 + 0.01f);
|
jaroslav@1877
|
127 |
float y = (i + 0.5f) * (ballRadius * 2 + 0.01f);
|
jaroslav@1877
|
128 |
ballDef.position.x = 3 + x;
|
jaroslav@1877
|
129 |
ballDef.position.y = 3 + y;
|
jaroslav@1877
|
130 |
Body body = world.createBody(ballDef);
|
jaroslav@1877
|
131 |
body.createFixture(fixtureDef);
|
jaroslav@1877
|
132 |
|
jaroslav@1877
|
133 |
ballDef.position.x = 3 - x;
|
jaroslav@1877
|
134 |
ballDef.position.y = 3 + y;
|
jaroslav@1877
|
135 |
body = world.createBody(ballDef);
|
jaroslav@1877
|
136 |
body.createFixture(fixtureDef);
|
jaroslav@1877
|
137 |
|
jaroslav@1877
|
138 |
ballDef.position.x = 3 + x;
|
jaroslav@1877
|
139 |
ballDef.position.y = 3 - y;
|
jaroslav@1877
|
140 |
body = world.createBody(ballDef);
|
jaroslav@1877
|
141 |
body.createFixture(fixtureDef);
|
jaroslav@1877
|
142 |
|
jaroslav@1877
|
143 |
ballDef.position.x = 3 - x;
|
jaroslav@1877
|
144 |
ballDef.position.y = 3 - y;
|
jaroslav@1877
|
145 |
body = world.createBody(ballDef);
|
jaroslav@1877
|
146 |
body.createFixture(fixtureDef);
|
jaroslav@1877
|
147 |
}
|
jaroslav@1877
|
148 |
}
|
jaroslav@1877
|
149 |
}
|
jaroslav@1877
|
150 |
|
jaroslav@1877
|
151 |
private void joinReelToAxis() {
|
jaroslav@1877
|
152 |
RevoluteJointDef jointDef = new RevoluteJointDef();
|
jaroslav@1877
|
153 |
jointDef.bodyA = axis;
|
jaroslav@1877
|
154 |
jointDef.bodyB = reel;
|
jaroslav@1877
|
155 |
world.createJoint(jointDef);
|
jaroslav@1877
|
156 |
}
|
jaroslav@1877
|
157 |
|
jaroslav@1877
|
158 |
public void calculate() {
|
jaroslav@1877
|
159 |
long currentTime = System.currentTimeMillis();
|
jaroslav@1877
|
160 |
int timeToCalculate = (int) (currentTime - lastCalculated);
|
jaroslav@1877
|
161 |
long relativeTime = currentTime - startTime;
|
jaroslav@1877
|
162 |
while (timeToCalculate > 10) {
|
jaroslav@1877
|
163 |
int period = (int) ((relativeTime + 5000) / 10000);
|
jaroslav@1877
|
164 |
reel.applyTorque(period % 2 == 0 ? 8f : -8f);
|
jaroslav@1877
|
165 |
world.step(0.01f, 20, 40);
|
jaroslav@1877
|
166 |
lastCalculated += 10;
|
jaroslav@1877
|
167 |
timeToCalculate -= 10;
|
jaroslav@1877
|
168 |
}
|
jaroslav@1877
|
169 |
}
|
jaroslav@1877
|
170 |
|
jaroslav@1877
|
171 |
public int timeUntilNextStep() {
|
jaroslav@1877
|
172 |
return (int) Math.max(0, lastCalculated + 10 - System.currentTimeMillis());
|
jaroslav@1877
|
173 |
}
|
jaroslav@1877
|
174 |
|
jaroslav@1877
|
175 |
public World getWorld() {
|
jaroslav@1877
|
176 |
return world;
|
jaroslav@1877
|
177 |
}
|
jaroslav@1877
|
178 |
}
|