Esempio n. 1
0
 @Override
 public void capsule(float radius, float height, int divisions) {
   if (height < 2f * radius)
     throw new GdxRuntimeException("Height must be at least twice the radius");
   final float d = 2f * radius;
   cylinder(d, height - d, d, divisions, 0, 360, false);
   sphere(
       matTmp1.setToTranslation(0, .5f * (height - d), 0),
       d,
       d,
       d,
       divisions,
       divisions,
       0,
       360,
       0,
       90);
   sphere(
       matTmp1.setToTranslation(0, -.5f * (height - d), 0),
       d,
       d,
       d,
       divisions,
       divisions,
       0,
       360,
       90,
       180);
 }
 public void updateModelMatrix() {
   modelMatrix.setToTranslation(x, y, z);
   modelMatrix.rotate(1, 0, 0, rotationX);
   modelMatrix.rotate(0, 0, 1, rotationZ);
   modelMatrix.rotate(0, 1, 0, rotationY);
   partMesh.calculateBoundingBox(box);
 }
  public void construct(ModelConstructTool modelBuilder, String... partName) {
    modelBuilder.node(partName[0]);
    MeshPartBuilder meshBuilder =
        modelBuilder.part(
            "gilse",
            GL20.GL_TRIANGLES,
            Usage.Position | Usage.Normal,
            new Material(ColorAttribute.createDiffuse(Color.ORANGE)));
    Matrix4 tmGls = new Matrix4();
    tmGls.setToRotation(0f, 0f, 10f, -90f);
    meshBuilder.setVertexTransform(tmGls);
    meshBuilder.cylinder(7f, 48f, 7f, 10);

    meshBuilder =
        modelBuilder.part(
            "cone",
            GL20.GL_TRIANGLES,
            Usage.Position | Usage.Normal,
            new Material(ColorAttribute.createDiffuse(Color.ORANGE)));
    Matrix4 tmCone = new Matrix4();
    tmCone.setToRotation(0f, 0f, 10f, -90f);
    tmCone.trn(30f, 0f, 0f);
    ;
    meshBuilder.setVertexTransform(tmCone);
    meshBuilder.cone(7, 12, 7, 10);
  }
Esempio n. 4
0
  public void render(Screen screen, Camera camera) {
    matrix.idt();
    matrix.setToTranslation(camera.x, camera.y, 0);
    screen.spriteBatch.setTransformMatrix(matrix);
    screen.spriteBatch.begin();
    // g.translate(-camera.x, -camera.y);

    int xo = 0;
    int yo = 0;
    for (int x = xo; x <= xo + camera.width / 10; x++) {
      for (int y = yo; y <= yo + camera.height / 10; y++) {
        if (x >= 0 && y >= 0 && x < width && y < height) {
          int ximg = 0;
          int yimg = 0;
          byte w = walls[x + y * width];
          if (w == 0) yimg = 1;
          if (w == 1) ximg = 0;
          if (w == 2) ximg = 2;
          if (w == 3) ximg = 1;
          if (w == 9) ximg = 7;
          if (w == 8) {
            ximg = 4;
            yimg = 1;
          }
          if (w == 5) {
            ximg = 1;
            yimg = 1;
          }
          if (w == 6) {
            ximg = (tick / 4 + x * 2) & 3;
            yimg = 2;
          }
          if (w == 7) {
            ximg = (-tick / 4 + x * 2) & 3;
            yimg = 3;
          }
          if (w == 4) {
            if (walls[x + (y - 1) * width] == 1) {
              yimg++;
            }
            ximg = 3;
          }

          if (w == 0) continue;

          screen.draw(Art.walls[ximg][yimg], x * 10, y * 10);
        }
      }
    }
    for (int i = entities.size() - 1; i >= 0; i--) {
      Entity e = entities.get(i);
      e.render(screen, camera);
    }

    screen.spriteBatch.end();
  }
Esempio n. 5
0
 private void renderSource(DebugRenderContext context, AudioSourceComponent listenerComponent) {
   GenericBatch batch = context.batch;
   Camera camera = context.camera;
   listenerComponent.getTransform(transform);
   transform.getTranslation(position);
   transform.setToLookAt(position, camera.position, up);
   Matrix4.inv(transform.val);
   batch.set2dTransform(transform);
   batch.render(listenerSprite);
 }
 public boolean contains(Vector3 vec) {
   Vector3 temp = new Vector3(vec);
   Matrix4 tempMat = new Matrix4(modelMatrix);
   tempMat.inv();
   temp.mul(tempMat);
   if (box.contains(temp)) {
     return true;
   }
   return false;
 }
Esempio n. 7
0
  public Matrix4 getMatrix(Element matrix) {
    Matrix4 m = new Matrix4();

    // read elements into data[]
    String[] tokens = matrix.getText().split("\\s+");
    for (int i = 0; i < tokens.length; i++) {
      m.val[i] = Float.parseFloat(tokens[i]);
    }
    m.tra();
    return m;
  }
Esempio n. 8
0
 /**
  * Starts a new batch of shapes. All shapes within the batch have to have the type specified. E.g.
  * if {@link ShapeType#Point} is specified, only call #point().
  *
  * <p>The call to this method must be paired with a call to {@link #end()}.
  *
  * <p>In case OpenGL ES 1.x is used, the projection and modelview matrix will be modified.
  *
  * @param type the {@link ShapeType}.
  */
 public void begin(ShapeType type) {
   if (currType != null)
     throw new GdxRuntimeException("Call end() before beginning a new shape batch");
   currType = type;
   if (matrixDirty) {
     combined.set(projView);
     Matrix4.mul(combined.val, transform.val);
     matrixDirty = false;
   }
   renderer.begin(combined, currType.getGlType());
 }
Esempio n. 9
0
  private void renderBox(Body body, float halfWidth, float halfHeight) {
    // get the bodies center and angle in world coordinates
    Vector2 pos = body.getWorldCenter();
    float angle = body.getAngle();

    // set the translation and rotation matrix
    transform.setToTranslation(pos.x, pos.y, 0);
    transform.rotate(0, 0, 1, (float) Math.toDegrees(angle));

    // render the box
    renderer.begin(ShapeType.Line);
    renderer.setTransformMatrix(transform);
    renderer.setColor(1, 1, 1, 1);
    renderer.rect(-halfWidth, -halfHeight, halfWidth * 2, halfHeight * 2);
    renderer.end();
  }
Esempio n. 10
0
 /**
  * Transforms the SpriteBatch to this group's coordinate system. Note this causes the batch to be
  * flushed.
  */
 protected void applyTransform(SpriteBatch batch) {
   updateTransform();
   batch.end();
   oldBatchTransform.set(batch.getTransformMatrix());
   batch.setTransformMatrix(batchTransform);
   batch.begin();
 }
Esempio n. 11
0
 public ShapeRenderer(int maxVertices) {
   if (Gdx.graphics.isGL20Available())
     renderer = new ImmediateModeRenderer20(maxVertices, false, true, 0);
   else renderer = new ImmediateModeRenderer10(maxVertices);
   projView.setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
   matrixDirty = true;
 }
Esempio n. 12
0
  private void updateTransform() {
    Matrix3 temp = worldTransform;

    float originX = getOriginX();
    float originY = getOriginY();
    float rotation = getRotation();
    float scaleX = getScaleX();
    float scaleY = getScaleY();

    if (originX != 0 || originY != 0) localTransform.setToTranslation(originX, originY);
    else localTransform.idt();
    if (rotation != 0) localTransform.mul(temp.setToRotation(rotation));
    if (scaleX != 1 || scaleY != 1) localTransform.mul(temp.setToScaling(scaleX, scaleY));
    if (originX != 0 || originY != 0) localTransform.mul(temp.setToTranslation(-originX, -originY));
    localTransform.trn(getX(), getY());

    // Find the first parent that transforms.
    Group parentGroup = getParent();
    while (parentGroup != null) {
      if (parentGroup.transform) break;
      parentGroup = parentGroup.getParent();
    }

    if (parentGroup != null) {
      worldTransform.set(parentGroup.worldTransform);
      worldTransform.mul(localTransform);
    } else {
      worldTransform.set(localTransform);
    }

    batchTransform.set(worldTransform);
  }
Esempio n. 13
0
 /** sets batch to game units to draw and then back to screen */
 @Override
 public void draw(SpriteBatch batch, float parentAlpha) {
   tmpMatrix4.set(batch.getProjectionMatrix());
   batch.setProjectionMatrix(cam.combined);
   super.draw(batch, parentAlpha);
   batch.setProjectionMatrix(tmpMatrix4);
 }
Esempio n. 14
0
 @Override
 public void set(
     BaseShader shader,
     int inputID,
     Renderable renderable,
     Attributes combinedAttributes) {
   shader.set(inputID, temp.set(shader.camera.combined).mul(renderable.worldTransform));
 }
 @Override
 protected void onUpdate(GameObject object, Vector3 offset, float delta) {
   if (effect == null) {
     effect = particleManager.create(object.getPosition(), type);
   }
   trans.set(offset.add(object.getPosition()), object.getOrientation());
   effect.setTransform(trans);
 }
Esempio n. 16
0
  public void render(float delta) {
    if (cameraCache != viewport.getCamera())
      cameraCache = (OrthographicCamera) viewport.getCamera();

    viewport.getCamera().position.add(speed.x * delta, speed.y * delta, 0f);
    for (int i = 0; i < layers.length; i++) {

      cameraProjectionCache.set(viewport.getCamera().projection);
      cameraProjectionCache.scl(cameraCache.zoom);

      batch.setProjectionMatrix(cameraProjectionCache);
      batch.begin();
      float currentX =
          -viewport.getCamera().position.x
              * layers[i].parallaxRatio.x
              % (layers[i].region.getRegionWidth() + layers[i].padding.x);

      if (speed.x < 0) currentX += -(layers[i].region.getRegionWidth() + layers[i].padding.x);
      do {
        /* Y REPEATING DISABLED:
        float currentY = -viewport.getCamera().position.y * layers[i].parallaxRatio.y % (layers[i].region.getRegionHeight() + layers[i].padding.y);

        if (speed.y < 0)
            currentY += -(layers[i].region.getRegionHeight() + layers[i].padding.y);

        do {
            batch.draw(layers[i].region,
                    -viewport.getCamera().viewportWidth / 2 + currentX + layers[i].startPosition.x,
                    -viewport.getCamera().viewportHeight / 2 + currentY + layers[i].startPosition.y);

            currentY += (layers[i].region.getRegionHeight() + layers[i].padding.y);
        } while (currentY < viewport.getCamera().viewportHeight);*/

        batch.draw(
            layers[i].region,
            -viewport.getCamera().viewportWidth / 2 + currentX + layers[i].startPosition.x,
            -viewport.getCamera().viewportHeight / 2 + layers[i].startPosition.y);

        currentX += (layers[i].region.getRegionWidth() + layers[i].padding.x);
      } while (currentX < viewport.getCamera().viewportWidth);
      batch.end();
    }
  }
Esempio n. 17
0
  public void update(float delta) {
    // tmp.set(acceleration);
    // tmp.scl(delta);
    // velocity.add(tmp);
    // velocity = velocity.scl(0.95f);
    tmp.set(velocity);
    tmp.scl(delta);
    position.add(tmp);

    transform.setTranslation(position);
    collisionObject.setWorldTransform(transform);
  }
Esempio n. 18
0
  @Override
  public void create() {
    font = new BitmapFont();
    batch = new SpriteBatch();

    Matrix4 m1 = new Matrix4();
    Matrix4 m2 = new Matrix4();
    float[] a1 = new float[16];
    float[] a2 = new float[16];
    float[] a3 = new float[16];
    Matrix.setIdentityM(a1, 0);
    Matrix.setIdentityM(a2, 0);
    Matrix.setIdentityM(a3, 0);

    long startTime = System.nanoTime();
    int ops = 0;
    while (System.nanoTime() - startTime < 5000000000l) {
      Matrix.multiplyMM(a1, 0, a2, 0, a3, 0);
      ops++;
    }
    results = "Matrix ops: " + ops + "\n";

    // warm up
    startTime = System.nanoTime();
    ops = 0;
    while (System.nanoTime() - startTime < 2000000000l) {
      m1.mul(m2);
      ops++;
    }

    startTime = System.nanoTime();
    ops = 0;
    while (System.nanoTime() - startTime < 5000000000l) {
      m1.mul(m2);
      ops++;
    }
    results += "Matrix4 ops: " + ops + "\n";
  }
Esempio n. 19
0
  SkeletonJoint getJoint(Element jointNode, boolean isRootNode) {
    SkeletonJoint joint = new SkeletonJoint();
    joint.name = jointNode.getAttribute("id");
    if (!isRootNode) {
      Element matrixElement = jointNode.getChildByName("matrix");
      if (matrixElement != null) {
        Matrix4 m = getMatrix(matrixElement);
        m.getTranslation(joint.position);
        m.getRotation(joint.rotation);
        // TODO: get scale from matrix
      }
    }

    Array<Element> nodes = jointNode.getChildrenByName("node");
    for (int i = 0; i < nodes.size; i++) {
      SkeletonJoint child = getJoint(nodes.get(i), false);
      if (!isRootNode) {
        child.parent = joint;
      }
      joint.children.add(child);
    }

    return joint;
  }
Esempio n. 20
0
  public void scale(float x, float y, float z, boolean updateLocal) {
    activate();
    // Set unit scale
    Matrix4 t = modelInstance.transform;
    Matrix4 mat_scale = new Matrix4();
    Vector3 s = new Vector3();
    t.getScale(s);
    mat_scale.scl(1 / s.x, 1 / s.y, 1 / s.z);
    t.mul(mat_scale);

    // Set target scale
    mat_scale.idt();
    mat_scale.scl(x, y, z);
    t.mul(mat_scale);

    // Relevant bullet body update
    CollisionShape cs = body.getCollisionShape();
    cs.setLocalScaling(new Vector3f(x, y, z));
    if (body.isInWorld() && body.isStaticOrKinematicObject()) scene.world.updateSingleAabb(body);

    // Child propagation
    Vector3f ps = scale();
    Matrix4f pt = transform();
    Matrix4f ct = new Matrix4f();
    Matrix4f ms = new Matrix4f();
    ms.setIdentity();
    ms.m00 = ps.x;
    ms.m11 = ps.y;
    ms.m22 = ps.z;
    pt.mul(ms);

    for (GameObject c : children) {
      c.scale(scale().mul(c.localScale), false);
      ct.mul(pt, c.localTransform);
      c.transform(ct, false);
    }

    if (parent != null && updateLocal) {
      updateLocalScale();
    }
  }
Esempio n. 21
0
  @Override
  public void draw(float delta) {
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    viewMatrix.setToOrtho2D(0, 0, 480, 320);
    spriteBatch.setProjectionMatrix(viewMatrix);
    spriteBatch.setTransformMatrix(transformMatrix);
    spriteBatch.begin();
    spriteBatch.disableBlending();
    spriteBatch.setColor(Color.WHITE);
    spriteBatch.draw(background, 0, 0, 480, 320, 0, 0, 512, 512, false, false);
    spriteBatch.enableBlending();
    spriteBatch.draw(logo, 0, 320 - 128, 480, 128, 0, 256, 512, 256, false, false);
    String text = "It is the end my friend.\nTouch to continue!";
    TextBounds bounds = font.getMultiLineBounds(text);
    spriteBatch.setBlendFunction(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
    font.drawMultiLine(spriteBatch, text, 0, 160 + bounds.height / 2, 480, HAlignment.CENTER);
    spriteBatch.end();
  }
Esempio n. 22
0
  public Projectile(
      Vector3 startingPosition, Vector3 direction, float msSpeed, btCollisionWorld world) {
    btCollisionShape sphere = new btSphereShape(0.5f);
    collisionObject = new btCollisionObject();
    collisionObject.setCollisionShape(sphere);
    collisionObject.userData = this;
    world.addCollisionObject(collisionObject);

    position = new Vector3(startingPosition);
    /*
    acceleration = new Vector3(direction);
    acceleration.nor().scl(msSpeed);*/

    velocity = new Vector3(direction);
    velocity.nor().scl(msSpeed);
    transform = new Matrix4();
    transform.setTranslation(position);
    collisionObject.setWorldTransform(transform);
  }
Esempio n. 23
0
 private void setupMatrices() {
   if (!Gdx.graphics.isGL20Available()) {
     GL10 gl = Gdx.gl10;
     gl.glMatrixMode(GL10.GL_PROJECTION);
     gl.glLoadMatrixf(projectionMatrix.val, 0);
     gl.glMatrixMode(GL10.GL_MODELVIEW);
     gl.glLoadMatrixf(transformMatrix.val, 0);
   } else {
     combinedMatrix.set(projectionMatrix).mul(transformMatrix);
     if (customShader != null) {
       customShader.setUniformMatrix("u_proj", projectionMatrix);
       customShader.setUniformMatrix("u_trans", transformMatrix);
       customShader.setUniformMatrix("u_projTrans", combinedMatrix);
       customShader.setUniformi("u_texture", 0);
     } else {
       shader.setUniformMatrix("u_projectionViewMatrix", combinedMatrix);
       shader.setUniformi("u_texture", 0);
     }
   }
 }
Esempio n. 24
0
  /**
   * Constructs a new SpriteBatch. Sets the projection matrix to an orthographic projection with
   * y-axis point upwards, x-axis point to the right and the origin being in the bottom left corner
   * of the screen. The projection will be pixel perfect with respect to the screen resolution.
   *
   * <p>The size parameter specifies the maximum size of a single batch in number of sprites
   *
   * @param size the batch size in number of sprites
   * @param buffers the number of buffers to use. only makes sense with VBOs. This is an expert
   *     function.
   * @param defaultShader the default shader to use. This is not owned by the SpriteBatch and must
   *     be disposed separately.
   */
  public SpriteBatch(int size, int buffers, ShaderProgram defaultShader) {
    this.buffers = new Mesh[buffers];

    for (int i = 0; i < buffers; i++) {
      this.buffers[i] =
          new Mesh(
              VertexDataType.VertexArray,
              false,
              size * 4,
              size * 6,
              new VertexAttribute(Usage.Position, 2, ShaderProgram.POSITION_ATTRIBUTE),
              new VertexAttribute(Usage.ColorPacked, 4, ShaderProgram.COLOR_ATTRIBUTE),
              new VertexAttribute(
                  Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"));
    }

    projectionMatrix.setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    vertices = new float[size * Sprite.SPRITE_SIZE];

    int len = size * 6;
    short[] indices = new short[len];
    short j = 0;
    for (int i = 0; i < len; i += 6, j += 4) {
      indices[i + 0] = (short) (j + 0);
      indices[i + 1] = (short) (j + 1);
      indices[i + 2] = (short) (j + 2);
      indices[i + 3] = (short) (j + 2);
      indices[i + 4] = (short) (j + 3);
      indices[i + 5] = (short) (j + 0);
    }
    for (int i = 0; i < buffers; i++) {
      this.buffers[i].setIndices(indices);
    }
    mesh = this.buffers[0];

    if (Gdx.graphics.isGL20Available() && defaultShader == null) {
      shader = createDefaultShader();
      ownsShader = true;
    } else shader = defaultShader;
  }
Esempio n. 25
0
 @Override
 public void sphere(
     float width,
     float height,
     float depth,
     int divisionsU,
     int divisionsV,
     float angleUFrom,
     float angleUTo,
     float angleVFrom,
     float angleVTo) {
   sphere(
       matTmp1.idt(),
       width,
       height,
       depth,
       divisionsU,
       divisionsV,
       angleUFrom,
       angleUTo,
       angleVFrom,
       angleVTo);
 }
Esempio n. 26
0
 public static void getShapeParts(
     final Node node,
     final boolean applyTransform,
     final Array<ShapePart> out,
     final int offset,
     final Pool<ShapePart> pool) {
   final Matrix4 transform = applyTransform ? node.localTransform : idt;
   if (node.parts.size > 0) {
     ShapePart part = null;
     for (int i = offset, n = out.size; i < n; i++) {
       final ShapePart p = out.get(i);
       if (Arrays.equals(p.transform.val, transform.val)) {
         part = p;
         break;
       }
     }
     if (part == null) {
       part = pool.obtain();
       part.parts.clear();
       part.transform.set(transform);
       out.add(part);
     }
     for (int i = 0, n = node.parts.size; i < n; i++) part.parts.add(node.parts.get(i).meshPart);
   }
   if (node.hasChildren()) {
     final boolean transformed = applyTransform && !Arrays.equals(transform.val, idt.val);
     final int o = transformed ? out.size : offset;
     getShapeParts(node.getChildren(), out, o, pool);
     if (transformed) {
       for (int i = o, n = out.size; i < n; i++) {
         final ShapePart part = out.get(i);
         tmpM.set(part.transform);
         part.transform.set(transform).mul(tmpM);
       }
     }
   }
 }
Esempio n. 27
0
 /**
  * Sets the transform matrix to be used by this SpriteBatch. If this is called inside a {@link
  * #begin()}/{@link #end()} block. the current batch is flushed to the gpu.
  *
  * @param transform the transform matrix
  */
 public void setTransformMatrix(Matrix4 transform) {
   if (drawing) flush();
   transformMatrix.set(transform);
   if (drawing) setupMatrices();
 }
Esempio n. 28
0
 /**
  * Sets the projection matrix to be used by this SpriteBatch. If this is called inside a {@link
  * #begin()}/{@link #end()} block. the current batch is flushed to the gpu.
  *
  * @param projection the projection matrix
  */
 public void setProjectionMatrix(Matrix4 projection) {
   if (drawing) flush();
   projectionMatrix.set(projection);
   if (drawing) setupMatrices();
 }
Esempio n. 29
0
 @Override
 public void box(float x, float y, float z, float width, float height, float depth) {
   box(matTmp1.setToScaling(width, height, depth).trn(x, y, z));
 }
Esempio n. 30
0
 @Override
 public void box(float width, float height, float depth) {
   box(matTmp1.setToScaling(width, height, depth));
 }