Example #1
0
  @Override
  public void render() {
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    if (Gdx.input.isTouched()) {
      stage.screenToStageCoordinates(stageCoords.set(Gdx.input.getX(), Gdx.input.getY()));
      Actor actor = stage.hit(stageCoords.x, stageCoords.y, true);
      if (actor instanceof Image)
        ((Image) actor)
            .setColor(
                (float) Math.random(),
                (float) Math.random(),
                (float) Math.random(),
                0.5f + 0.5f * (float) Math.random());
    }

    Array<Actor> actors = stage.getActors();
    int len = actors.size;
    if (rotateSprites) {
      for (int i = 0; i < len; i++) actors.get(i).rotateBy(Gdx.graphics.getDeltaTime() * 10);
    }

    scale += vScale * Gdx.graphics.getDeltaTime();
    if (scale > 1) {
      scale = 1;
      vScale = -vScale;
    }
    if (scale < 0.5f) {
      scale = 0.5f;
      vScale = -vScale;
    }

    len = images.size();
    for (int i = 0; i < len; i++) {
      Image img = images.get(i);
      if (rotateSprites) img.rotateBy(-40 * Gdx.graphics.getDeltaTime());
      else img.setRotation(0);

      if (scaleSprites) {
        img.setScale(scale);
      } else {
        img.setScale(1);
      }
      img.invalidate();
    }

    stage.draw();

    renderer.begin(ShapeType.Point);
    renderer.setColor(1, 0, 0, 1);
    len = actors.size;
    for (int i = 0; i < len; i++) {
      Group group = (Group) actors.get(i);
      renderer.point(group.getX() + group.getOriginX(), group.getY() + group.getOriginY(), 0);
    }
    renderer.end();

    fps.setText(
        "fps: "
            + Gdx.graphics.getFramesPerSecond()
            + ", actors "
            + images.size()
            + ", groups "
            + actors.size);
    ui.draw();
  }
Example #2
0
  @Override
  public void render() {
    // first we update the world. For simplicity
    // we use the delta time provided by the Graphics
    // instance. Normally you'll want to fix the time
    // step.
    long start = TimeUtils.nanoTime();
    world.step(Gdx.graphics.getDeltaTime(), 8, 3);
    float updateTime = (TimeUtils.nanoTime() - start) / 1000000000.0f;

    // next we clear the color buffer and set the camera
    // matrices
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();

    // next we render the ground body
    renderBox(groundBody, 50, 1);

    // next we render each box via the SpriteBatch.
    // for this we have to set the projection matrix of the
    // spritebatch to the camera's combined matrix. This will
    // make the spritebatch work in world coordinates
    batch.getProjectionMatrix().set(camera.combined);
    batch.begin();
    for (int i = 0; i < boxes.size(); i++) {
      Body box = boxes.get(i);
      Vector2 position = box.getPosition(); // that's the box's center position
      float angle =
          MathUtils.radiansToDegrees * box.getAngle(); // the rotation angle around the center
      batch.draw(
          textureRegion,
          position.x - 1,
          position.y - 1, // the bottom left corner of the box, unrotated
          1f,
          1f, // the rotation center relative to the bottom left corner of the box
          2,
          2, // the width and height of the box
          1,
          1, // the scale on the x- and y-axis
          angle); // the rotation angle
    }
    batch.end();

    // next we use the debug renderer. Note that we
    // simply apply the camera again and then call
    // the renderer. the camera.apply() call is actually
    // not needed as the opengl matrices are already set
    // by the spritebatch which in turn uses the camera matrices :)
    debugRenderer.render(world, camera.combined);

    // finally we render all contact points
    renderer.setProjectionMatrix(camera.combined);
    renderer.begin(ShapeType.Point);
    renderer.setColor(0, 1, 0, 1);
    for (int i = 0; i < world.getContactCount(); i++) {
      Contact contact = world.getContactList().get(i);
      // we only render the contact if it actually touches
      if (contact.isTouching()) {
        // get the world manifold from which we get the
        // contact points. A manifold can have 0, 1 or 2
        // contact points.
        WorldManifold manifold = contact.getWorldManifold();
        int numContactPoints = manifold.getNumberOfContactPoints();
        for (int j = 0; j < numContactPoints; j++) {
          Vector2 point = manifold.getPoints()[j];
          renderer.point(point.x, point.y, 0);
        }
      }
    }
    renderer.end();

    // finally we render the time it took to update the world
    // for this we have to set the projection matrix again, so
    // we work in pixel coordinates
    batch
        .getProjectionMatrix()
        .setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    batch.begin();
    font.draw(
        batch, "fps: " + Gdx.graphics.getFramesPerSecond() + " update time: " + updateTime, 0, 20);
    batch.end();
  }