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