benchmarks/jbox2d/src/main/java/org/apidesign/benchmark/jbox2d/Scene.java
changeset 1877 91a30c1a8c4b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/benchmarks/jbox2d/src/main/java/org/apidesign/benchmark/jbox2d/Scene.java	Sat Jan 30 18:33:49 2016 +0100
     1.3 @@ -0,0 +1,178 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.benchmark.jbox2d;
    1.22 +
    1.23 +/*
    1.24 + *  Copyright 2014 Alexey Andreev.
    1.25 + *
    1.26 + *  Licensed under the Apache License, Version 2.0 (the "License");
    1.27 + *  you may not use this file except in compliance with the License.
    1.28 + *  You may obtain a copy of the License at
    1.29 + *
    1.30 + *       http://www.apache.org/licenses/LICENSE-2.0
    1.31 + *
    1.32 + *  Unless required by applicable law or agreed to in writing, software
    1.33 + *  distributed under the License is distributed on an "AS IS" BASIS,
    1.34 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.35 + *  See the License for the specific language governing permissions and
    1.36 + *  limitations under the License.
    1.37 + */
    1.38 +
    1.39 +import org.jbox2d.collision.shapes.CircleShape;
    1.40 +import org.jbox2d.collision.shapes.PolygonShape;
    1.41 +import org.jbox2d.common.Vec2;
    1.42 +import org.jbox2d.dynamics.*;
    1.43 +import org.jbox2d.dynamics.joints.RevoluteJointDef;
    1.44 +
    1.45 +/** Taken from TeaVM.
    1.46 + * <a href="https://github.com/konsoletyper/teavm/blob/3341df3668bbe3f0c3ed78b621e311361c88acfc/samples/benchmark/src/main/java/org/teavm/samples/benchmark/Scene.java">Sample</a>
    1.47 + * is licensed under Apache license.
    1.48 + *
    1.49 + * @author Alexey Andreev
    1.50 + */
    1.51 +public class Scene {
    1.52 +    private World world;
    1.53 +    private Body axis;
    1.54 +    private Body reel;
    1.55 +    private long lastCalculated;
    1.56 +    private long startTime;
    1.57 +
    1.58 +    public Scene() {
    1.59 +        world = new World(new Vec2(0, -9.8f));
    1.60 +        initAxis();
    1.61 +        initReel();
    1.62 +        joinReelToAxis();
    1.63 +        initBalls();
    1.64 +        lastCalculated = System.currentTimeMillis();
    1.65 +        startTime = lastCalculated;
    1.66 +    }
    1.67 +
    1.68 +    private void initAxis() {
    1.69 +        BodyDef axisDef = new BodyDef();
    1.70 +        axisDef.type = BodyType.STATIC;
    1.71 +        axisDef.position = new Vec2(3, 3);
    1.72 +        axis = world.createBody(axisDef);
    1.73 +
    1.74 +        CircleShape axisShape = new CircleShape();
    1.75 +        axisShape.setRadius(0.02f);
    1.76 +        axisShape.m_p.set(0, 0);
    1.77 +
    1.78 +        FixtureDef axisFixture = new FixtureDef();
    1.79 +        axisFixture.shape = axisShape;
    1.80 +        axis.createFixture(axisFixture);
    1.81 +    }
    1.82 +
    1.83 +    private void initReel() {
    1.84 +        BodyDef reelDef = new BodyDef();
    1.85 +        reelDef.type = BodyType.DYNAMIC;
    1.86 +        reelDef.position = new Vec2(3, 3);
    1.87 +        reel = world.createBody(reelDef);
    1.88 +
    1.89 +        FixtureDef fixture = new FixtureDef();
    1.90 +        fixture.friction = 0.5f;
    1.91 +        fixture.restitution = 0.4f;
    1.92 +        fixture.density = 1;
    1.93 +
    1.94 +        int parts = 30;
    1.95 +        for (int i = 0; i < parts; ++i) {
    1.96 +            PolygonShape shape = new PolygonShape();
    1.97 +            double angle1 = i / (double) parts * 2 * Math.PI;
    1.98 +            double x1 = 2.7 * Math.cos(angle1);
    1.99 +            double y1 = 2.7 * Math.sin(angle1);
   1.100 +            double angle2 = (i + 1) / (double) parts * 2 * Math.PI;
   1.101 +            double x2 = 2.7 * Math.cos(angle2);
   1.102 +            double y2 = 2.7 * Math.sin(angle2);
   1.103 +            double angle = (angle1 + angle2) / 2;
   1.104 +            double x = 0.01 * Math.cos(angle);
   1.105 +            double y = 0.01 * Math.sin(angle);
   1.106 +
   1.107 +            shape.set(new Vec2[] { new Vec2((float) x1, (float) y1), new Vec2((float) x2, (float) y2),
   1.108 +                    new Vec2((float) (x2 - x), (float) (y2 - y)), new Vec2((float) (x1 - x), (float) (y1 - y)) }, 4);
   1.109 +            fixture.shape = shape;
   1.110 +            reel.createFixture(fixture);
   1.111 +        }
   1.112 +    }
   1.113 +
   1.114 +    private void initBalls() {
   1.115 +        float ballRadius = 0.15f;
   1.116 +
   1.117 +        BodyDef ballDef = new BodyDef();
   1.118 +        ballDef.type = BodyType.DYNAMIC;
   1.119 +        FixtureDef fixtureDef = new FixtureDef();
   1.120 +        fixtureDef.friction = 0.3f;
   1.121 +        fixtureDef.restitution = 0.3f;
   1.122 +        fixtureDef.density = 0.2f;
   1.123 +        CircleShape shape = new CircleShape();
   1.124 +        shape.m_radius = ballRadius;
   1.125 +        fixtureDef.shape = shape;
   1.126 +
   1.127 +        for (int i = 0; i < 5; ++i) {
   1.128 +            for (int j = 0; j < 5; ++j) {
   1.129 +                float x = (j + 0.5f) * (ballRadius * 2 + 0.01f);
   1.130 +                float y = (i + 0.5f) * (ballRadius * 2 + 0.01f);
   1.131 +                ballDef.position.x = 3 + x;
   1.132 +                ballDef.position.y = 3 + y;
   1.133 +                Body body = world.createBody(ballDef);
   1.134 +                body.createFixture(fixtureDef);
   1.135 +
   1.136 +                ballDef.position.x = 3 - x;
   1.137 +                ballDef.position.y = 3 + y;
   1.138 +                body = world.createBody(ballDef);
   1.139 +                body.createFixture(fixtureDef);
   1.140 +
   1.141 +                ballDef.position.x = 3 + x;
   1.142 +                ballDef.position.y = 3 - y;
   1.143 +                body = world.createBody(ballDef);
   1.144 +                body.createFixture(fixtureDef);
   1.145 +
   1.146 +                ballDef.position.x = 3 - x;
   1.147 +                ballDef.position.y = 3 - y;
   1.148 +                body = world.createBody(ballDef);
   1.149 +                body.createFixture(fixtureDef);
   1.150 +            }
   1.151 +        }
   1.152 +    }
   1.153 +
   1.154 +    private void joinReelToAxis() {
   1.155 +        RevoluteJointDef jointDef = new RevoluteJointDef();
   1.156 +        jointDef.bodyA = axis;
   1.157 +        jointDef.bodyB = reel;
   1.158 +        world.createJoint(jointDef);
   1.159 +    }
   1.160 +
   1.161 +    public void calculate() {
   1.162 +        long currentTime = System.currentTimeMillis();
   1.163 +        int timeToCalculate = (int) (currentTime - lastCalculated);
   1.164 +        long relativeTime = currentTime - startTime;
   1.165 +        while (timeToCalculate > 10) {
   1.166 +            int period = (int) ((relativeTime + 5000) / 10000);
   1.167 +            reel.applyTorque(period % 2 == 0 ? 8f : -8f);
   1.168 +            world.step(0.01f, 20, 40);
   1.169 +            lastCalculated += 10;
   1.170 +            timeToCalculate -= 10;
   1.171 +        }
   1.172 +    }
   1.173 +
   1.174 +    public int timeUntilNextStep() {
   1.175 +        return (int) Math.max(0, lastCalculated + 10 - System.currentTimeMillis());
   1.176 +    }
   1.177 +
   1.178 +    public World getWorld() {
   1.179 +        return world;
   1.180 +    }
   1.181 +}