Example usage for com.badlogic.gdx.math Rectangle overlaps

List of usage examples for com.badlogic.gdx.math Rectangle overlaps

Introduction

In this page you can find the example usage for com.badlogic.gdx.math Rectangle overlaps.

Prototype

public boolean overlaps(Rectangle r) 

Source Link

Usage

From source file:MyGdxGame.java

License:Apache License

private void updateKoala(float deltaTime) {
    if (deltaTime == 0)
        return;//from w w w.j  a  v a2s.c  o  m
    koala.stateTime += deltaTime;

    // check input and apply to velocity & state
    if ((Gdx.input.isKeyPressed(Keys.SPACE) || isTouched(0.5f, 1)) && koala.grounded) {
        koala.velocity.y += Koala.JUMP_VELOCITY;
        koala.state = Koala.State.Jumping;
        koala.grounded = false;
    }

    if (Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A) || isTouched(0, 0.25f)) {
        koala.velocity.x = -Koala.MAX_VELOCITY;
        if (koala.grounded)
            koala.state = Koala.State.Walking;
        koala.facesRight = false;
    }

    if (Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D) || isTouched(0.25f, 0.5f)) {
        koala.velocity.x = Koala.MAX_VELOCITY;
        if (koala.grounded)
            koala.state = Koala.State.Walking;
        koala.facesRight = true;
    }

    // apply gravity if we are falling
    koala.velocity.add(0, GRAVITY);

    // clamp the velocity to the maximum, x-axis only
    if (Math.abs(koala.velocity.x) > Koala.MAX_VELOCITY) {
        koala.velocity.x = Math.signum(koala.velocity.x) * Koala.MAX_VELOCITY;
    }

    // clamp the velocity to 0 if it's < 1, and set the state to standign
    if (Math.abs(koala.velocity.x) < 1) {
        koala.velocity.x = 0;
        if (koala.grounded)
            koala.state = Koala.State.Standing;
    }

    // multiply by delta time so we know how far we go
    // in this frame
    koala.velocity.scl(deltaTime);

    // perform collision detection & response, on each axis, separately
    // if the koala is moving right, check the tiles to the right of it's
    // right bounding box edge, otherwise check the ones to the left
    Rectangle koalaRect = rectPool.obtain();
    koalaRect.set(koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT);
    int startX, startY, endX, endY;
    if (koala.velocity.x > 0) {
        startX = endX = (int) (koala.position.x + Koala.WIDTH + koala.velocity.x);
    } else {
        startX = endX = (int) (koala.position.x + koala.velocity.x);
    }
    startY = (int) (koala.position.y);
    endY = (int) (koala.position.y + Koala.HEIGHT);
    getTiles(startX, startY, endX, endY, tiles);
    koalaRect.x += koala.velocity.x;
    for (Rectangle tile : tiles) {
        if (koalaRect.overlaps(tile)) {
            koala.velocity.x = 0;
            break;
        }
    }
    koalaRect.x = koala.position.x;

    // if the koala is moving upwards, check the tiles to the top of it's
    // top bounding box edge, otherwise check the ones to the bottom
    if (koala.velocity.y > 0) {
        startY = endY = (int) (koala.position.y + Koala.HEIGHT + koala.velocity.y);
    } else {
        startY = endY = (int) (koala.position.y + koala.velocity.y);
    }
    startX = (int) (koala.position.x);
    endX = (int) (koala.position.x + Koala.WIDTH);
    getTiles(startX, startY, endX, endY, tiles);
    koalaRect.y += koala.velocity.y;
    for (Rectangle tile : tiles) {
        if (koalaRect.overlaps(tile)) {
            // we actually reset the koala y-position here
            // so it is just below/above the tile we collided with
            // this removes bouncing :)
            if (koala.velocity.y > 0) {
                koala.position.y = tile.y - Koala.HEIGHT;
                // we hit a block jumping upwards, let's destroy it!
                TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get("walls");
                layer.setCell((int) tile.x, (int) tile.y, null);
            } else {
                koala.position.y = tile.y + tile.height;
                // if we hit the ground, mark us as grounded so we can jump
                koala.grounded = true;
            }
            koala.velocity.y = 0;
            break;
        }
    }
    rectPool.free(koalaRect);

    // unscale the velocity by the inverse delta time and set
    // the latest position
    koala.position.add(koala.velocity);
    koala.velocity.scl(1 / deltaTime);

    // Apply damping to the velocity on the x-axis so we don't
    // walk infinitely once a key was pressed
    koala.velocity.x *= Koala.DAMPING;

}

From source file:ca.hiphiparray.amazingmaze.Player.java

License:Open Source License

/** Handle the player collecting fish. */
private void collectFish() {
    Rectangle thisBox = getBoundingRectangle();
    for (int i = 0; i < maze.fishBoxes.size; i++) {
        if (thisBox.overlaps(maze.fishBoxes.get(i))) {
            TiledMapTileLayer layer = (TiledMapTileLayer) maze.map.getLayers().get(MapFactory.ITEM_LAYER);
            int x = (int) maze.fishBoxes.get(i).x;
            int y = (int) maze.fishBoxes.get(i).y;
            FishColour colour = ((FishCell) layer.getCell(x, y)).getColour();
            layer.setCell(x, y, null);/*from  w ww  . j a v a 2 s  . c  o m*/
            maze.fishBoxes.removeIndex(i);

            switch (colour) {
            case BLUE:
                blueCollected++;
                break;
            case PURPLE:
                purpleCollected++;
                break;
            case GREEN:
                greenCollected++;
                break;
            case RED:
                redCollected++;
                break;
            case ORANGE:
                orangeCollected++;
                break;
            }
            break;
        }
    }
}

From source file:ca.hiphiparray.amazingmaze.Player.java

License:Open Source License

/** Handle the player collecting cheese. */
private void collectCheese() {
    Rectangle thisBox = getBoundingRectangle();
    for (int i = 0; i < maze.cheeseBoxes.size; i++) {
        if (thisBox.overlaps(maze.cheeseBoxes.get(i))) {
            TiledMapTileLayer layer = (TiledMapTileLayer) maze.map.getLayers().get(MapFactory.ITEM_LAYER);
            int x = (int) maze.cheeseBoxes.get(i).x;
            int y = (int) maze.cheeseBoxes.get(i).y;
            layer.setCell(x, y, null);//from   w w  w  . j  a  va2s.  c  o  m
            maze.cheeseBoxes.removeIndex(i);
            lives++;
            maze.updateLives(-1);

            break;
        }
    }
}

From source file:ca.hiphiparray.amazingmaze.Player.java

License:Open Source License

/** Handle the player dying. */
private void handleDeath() {
    Rectangle thisBox = getBoundingRectangle();
    for (Rectangle wire : maze.wireBoxes) {
        if (thisBox.overlaps(wire)) {
            if (lives <= 0) {
                dead = true;/*from   w w  w.j a  v a 2 s  .co m*/
                return;
            }
            if (!maze.help) {
                lives--;
            }
            maze.updateLives((int) ((getX() - MapFactory.START_DISTANCE + 1) / MapFactory.WIRE_DISTANCE));
            setPosition(0, maze.mapHeight / 2);
            break;
        }
    }
}

From source file:ca.hiphiparray.amazingmaze.Player.java

License:Open Source License

/**
 * Return where the player should be after handling object collision.
 *
 * @param deltaPos the change in position since the last position update.
 * @return the new position, as a {@link Point2D.Float}.
 *//* w w w  .ja  va 2s.c o m*/
private Point2D.Float doObjectCollision(Vector2 deltaPos) {
    float newX = getX() + deltaPos.x;
    float newY = getY() + deltaPos.y;
    Point2D.Float nextTilePos = new Point2D.Float(newX, newY);

    Rectangle nextBoundingBox = new Rectangle(nextTilePos.x, nextTilePos.y, PLAYER_SIZE, PLAYER_SIZE);
    float nextStartX = nextBoundingBox.getX();
    float nextEndX = nextStartX + nextBoundingBox.getWidth();
    float nextStartY = nextBoundingBox.getY();
    float nextEndY = nextStartY + nextBoundingBox.getHeight();
    for (Rectangle obstacle : maze.obstacleBoxes) {
        if (nextBoundingBox.overlaps(obstacle)) {
            float objectStartX = obstacle.getX();
            float objectEndX = objectStartX + obstacle.getWidth();
            float objectStartY = obstacle.getY();
            float objectEndY = objectStartY + obstacle.getHeight();

            if (deltaPos.x != 0) {
                if (nextStartX > objectStartX && nextStartX < objectEndX) { // Collided on right.
                    newX = objectEndX;
                } else if (nextEndX > objectStartX && nextEndX < objectEndX) { // Collided on left.
                    newX = objectStartX - PLAYER_SIZE;
                }
            } else if (deltaPos.y != 0) {
                if (nextStartY > objectStartY && nextStartY < objectEndY) { // Collided on bottom.
                    newY = objectEndY;
                } else if (nextEndY > objectStartY && nextEndY < objectEndY) { // Collided on top.
                    newY = objectStartY - PLAYER_SIZE;
                }
            }
        }
    }
    newX = Math.max(newX, 0);
    newX = Math.min(newX, maze.mapWidth - PLAYER_SIZE);
    nextTilePos.setLocation(newX, newY);
    return nextTilePos;
}

From source file:ch.p1gu.game.systeme.CollisionSuperLamaEnnemis.java

@Override
protected void processSystem() {

    Entity superLama = world.getManager(TagManager.class).getEntity("SuperLama");
    ImmutableBag<Entity> ennemis = world.getManager(GroupManager.class).getEntities("Ennemis");

    Position sPos = mapperPos.get(superLama);
    Dimension sDim = mapperDim.get(superLama);
    Rectangle sTir = new Rectangle(sPos.x, sPos.y, sDim.largeur, sDim.hauteur);

    for (int j = 0; j < ennemis.size(); j++) {
        Entity e = ennemis.get(j);
        Position ePos = mapperPos.get(e);
        Dimension eDim = mapperDim.get(e);

        Rectangle rEnn = new Rectangle(ePos.x, ePos.y, eDim.largeur, eDim.hauteur);
        if (rEnn.overlaps(sTir)) {
            Vie sVie = mapperVie.get(superLama);
            Vie eVie = mapperVie.get(e);
            sVie.vie--;/*from ww  w  . j  ava2 s. c  om*/
            eVie.vie = 0;
            GlobalGameVariable.multiplicateurScore = 1;
        }

    }

}

From source file:ch.p1gu.game.systeme.CollisionSuperLamaTirsEnnemis.java

@Override
protected void processSystem() {
    Entity superLama = world.getManager(TagManager.class).getEntity("SuperLama");
    ImmutableBag<Entity> tirsEnnemis = world.getManager(GroupManager.class).getEntities("TirsEnnemis");
    Position sPos = mapperPos.get(superLama);
    Dimension sDim = mapperDim.get(superLama);
    Rectangle sTir = new Rectangle(sPos.x, sPos.y, sDim.largeur, sDim.hauteur);

    for (int j = 0; j < tirsEnnemis.size(); j++) {
        Entity e = tirsEnnemis.get(j);
        Position ePos = mapperPos.get(e);
        Dimension eDim = mapperDim.get(e);

        Rectangle rEnn = new Rectangle(ePos.x, ePos.y, eDim.largeur, eDim.hauteur);
        if (rEnn.overlaps(sTir)) {
            Vie sVie = mapperVie.get(superLama);
            Vie eVie = mapperVie.get(e);
            sVie.vie--;//from ww  w  .ja v a  2  s.  c o  m
            eVie.vie = 0;
            GlobalGameVariable.multiplicateurScore = 1;
        }

    }

}

From source file:ch.p1gu.game.systeme.CollisionTirLamaAvecEnnemi.java

@Override
protected void processSystem() {
    ImmutableBag<Entity> tirsLama = world.getManager(GroupManager.class).getEntities("TirsLama");
    ImmutableBag<Entity> ennemis = world.getManager(GroupManager.class).getEntities("Ennemis");

    for (int i = 0; i < tirsLama.size(); i++) {
        Entity t = tirsLama.get(i);
        Position tPos = mapperPos.get(t);
        Dimension tDim = mapperDim.get(t);

        Rectangle rTir = new Rectangle(tPos.x, tPos.y, tDim.largeur, tDim.hauteur);

        for (int j = 0; j < ennemis.size(); j++) {
            Entity e = ennemis.get(j);
            Position ePos = mapperPos.get(e);
            Dimension eDim = mapperDim.get(e);

            Rectangle rEnn = new Rectangle(ePos.x, ePos.y, eDim.largeur, eDim.hauteur);
            if (rEnn.overlaps(rTir)) {
                Vie tVie = mapperVie.get(t);
                Vie eVie = mapperVie.get(e);
                tVie.vie--;/*from  w  w  w.j  ava2 s.c o m*/
                eVie.vie -= mapperTir.get(t).puissance;

                GlobalGameVariable.score += 10 * GlobalGameVariable.multiplicateurScore;
                GlobalGameVariable.multiplicateurScore += 1;
            }

        }

    }
}

From source file:com.axatrikx.solor.view.DummyLevelScreen.java

License:Apache License

@Override
public void render(float delta) {
    super.render(delta);

    // begin a new batch and draw the bucket and
    // all drops/*from   w w w.  j a va  2s .c o  m*/
    batch.begin();
    font.draw(batch, "Drops Collected: " + dropsGathered, 0, 480);
    batch.draw(bucketImage, bucket.x, bucket.y);
    for (Rectangle raindrop : raindrops) {
        batch.draw(dropImage, raindrop.x, raindrop.y);
    }
    batch.end();

    // process user input
    if (Gdx.input.isTouched()) {
        Vector3 touchPos = new Vector3();
        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
        camera.unproject(touchPos);
        bucket.x = touchPos.x - 64 / 2;
    }
    if (Gdx.input.isKeyPressed(Keys.LEFT))
        bucket.x -= 200 * Gdx.graphics.getDeltaTime();
    if (Gdx.input.isKeyPressed(Keys.RIGHT))
        bucket.x += 200 * Gdx.graphics.getDeltaTime();

    // make sure the bucket stays within the screen bounds
    if (bucket.x < 0)
        bucket.x = 0;
    if (bucket.x > 800 - 64)
        bucket.x = 800 - 64;

    // check if we need to create a new raindrop
    if (TimeUtils.nanoTime() - lastDropTime > 1000000000)
        spawnRaindrop();

    // move the raindrops, remove any that are beneath the bottom edge of
    // the screen or that hit the bucket. In the later case we play back
    // a sound effect as well.
    Iterator<Rectangle> iter = raindrops.iterator();
    while (iter.hasNext()) {
        Rectangle raindrop = iter.next();
        raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
        if (raindrop.y + 64 < 0)
            iter.remove();
        if (raindrop.overlaps(bucket)) {
            dropsGathered++;
            dropSound.play();
            iter.remove();
        }
    }
}

From source file:com.badlogic.gdx.tests.superkoalio.SuperKoalio.java

License:Apache License

private void updateKoala(float deltaTime) {
    if (deltaTime == 0)
        return;//from   ww w  .  j a v  a 2  s.  co  m
    koala.stateTime += deltaTime;

    // check input and apply to velocity & state
    if ((Gdx.input.isKeyPressed(Keys.SPACE) || isTouched(0.75f, 1)) && koala.grounded) {
        koala.velocity.y += Koala.JUMP_VELOCITY;
        koala.state = Koala.State.Jumping;
        koala.grounded = false;
    }

    if (Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A) || isTouched(0, 0.25f)) {
        koala.velocity.x = -Koala.MAX_VELOCITY;
        if (koala.grounded)
            koala.state = Koala.State.Walking;
        koala.facesRight = false;
    }

    if (Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D) || isTouched(0.25f, 0.5f)) {
        koala.velocity.x = Koala.MAX_VELOCITY;
        if (koala.grounded)
            koala.state = Koala.State.Walking;
        koala.facesRight = true;
    }

    // apply gravity if we are falling
    koala.velocity.add(0, GRAVITY);

    // clamp the velocity to the maximum, x-axis only
    if (Math.abs(koala.velocity.x) > Koala.MAX_VELOCITY) {
        koala.velocity.x = Math.signum(koala.velocity.x) * Koala.MAX_VELOCITY;
    }

    // clamp the velocity to 0 if it's < 1, and set the state to standign
    if (Math.abs(koala.velocity.x) < 1) {
        koala.velocity.x = 0;
        if (koala.grounded)
            koala.state = Koala.State.Standing;
    }

    // multiply by delta time so we know how far we go
    // in this frame
    koala.velocity.scl(deltaTime);

    // perform collision detection & response, on each axis, separately
    // if the koala is moving right, check the tiles to the right of it's
    // right bounding box edge, otherwise check the ones to the left
    Rectangle koalaRect = rectPool.obtain();
    koalaRect.set(koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT);
    int startX, startY, endX, endY;
    if (koala.velocity.x > 0) {
        startX = endX = (int) (koala.position.x + Koala.WIDTH + koala.velocity.x);
    } else {
        startX = endX = (int) (koala.position.x + koala.velocity.x);
    }
    startY = (int) (koala.position.y);
    endY = (int) (koala.position.y + Koala.HEIGHT);
    getTiles(startX, startY, endX, endY, tiles);
    koalaRect.x += koala.velocity.x;
    for (Rectangle tile : tiles) {
        if (koalaRect.overlaps(tile)) {
            koala.velocity.x = 0;
            break;
        }
    }
    koalaRect.x = koala.position.x;

    // if the koala is moving upwards, check the tiles to the top of it's
    // top bounding box edge, otherwise check the ones to the bottom
    if (koala.velocity.y > 0) {
        startY = endY = (int) (koala.position.y + Koala.HEIGHT + koala.velocity.y);
    } else {
        startY = endY = (int) (koala.position.y + koala.velocity.y);
    }
    startX = (int) (koala.position.x);
    endX = (int) (koala.position.x + Koala.WIDTH);
    getTiles(startX, startY, endX, endY, tiles);
    koalaRect.y += koala.velocity.y;
    for (Rectangle tile : tiles) {
        if (koalaRect.overlaps(tile)) {
            // we actually reset the koala y-position here
            // so it is just below/above the tile we collided with
            // this removes bouncing :)
            if (koala.velocity.y > 0) {
                koala.position.y = tile.y - Koala.HEIGHT;
                // we hit a block jumping upwards, let's destroy it!
                TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(1);
                layer.setCell((int) tile.x, (int) tile.y, null);
            } else {
                koala.position.y = tile.y + tile.height;
                // if we hit the ground, mark us as grounded so we can jump
                koala.grounded = true;
            }
            koala.velocity.y = 0;
            break;
        }
    }
    rectPool.free(koalaRect);

    // unscale the velocity by the inverse delta time and set
    // the latest position
    koala.position.add(koala.velocity);
    koala.velocity.scl(1 / deltaTime);

    // Apply damping to the velocity on the x-axis so we don't
    // walk infinitely once a key was pressed
    koala.velocity.x *= Koala.DAMPING;

}