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

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

Introduction

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

Prototype

public float getX() 

Source Link

Usage

From source file:base.Engine.java

License:Open Source License

public static Set<TreeSet<PatternInstance>> findCollisions(List<PatternInstance> instanceList,
        int caseThreshold) {
    Set<TreeSet<PatternInstance>> returnSet = new TreeSet<>(new TreeComparator());

    if (instanceList.size() < 2)
        return returnSet; // Our base case

    if (instanceList.size() < caseThreshold) {
        PatternInstance currentInstance = null;
        for (ListIterator<PatternInstance> i = instanceList.listIterator(); i.hasNext();) {
            currentInstance = i.next();/*from   ww  w  . ja  v  a 2s . c om*/
            PatternInstance otherInstance = null;
            for (ListIterator<PatternInstance> j = instanceList.listIterator(i.nextIndex()); j.hasNext();) {
                otherInstance = j.next();
                if (currentInstance != otherInstance
                        && currentInstance.getRectangle().overlaps(otherInstance.getRectangle())) {
                    returnSet = addPair(returnSet, currentInstance, otherInstance);
                }
            }
        }
        return returnSet; // Our other base case
    }

    Rectangle getRect = instanceList.get(0).getRectangle();
    // Create a super-rectangle that encompasses all the objects
    PatternInstance currentInstance = null;
    for (ListIterator<PatternInstance> i = instanceList.listIterator(1); i.hasNext();) {
        currentInstance = i.next();
        getRect = getRect.merge(currentInstance.getRectangle());
    }

    List<PatternInstance> listQ1 = new LinkedList<>();
    List<PatternInstance> listQ2 = new LinkedList<>();
    List<PatternInstance> listQ3 = new LinkedList<>();
    List<PatternInstance> listQ4 = new LinkedList<>();

    Rectangle rectQ1 = new Rectangle(getRect.getX(), getRect.getY(), getRect.getWidth() / 2 + 1,
            getRect.getHeight() / 2 + 1);
    Rectangle rectQ2 = new Rectangle(getRect.getX(), getRect.getY() + getRect.getHeight() / 2 + 1,
            getRect.getWidth() / 2 + 1, getRect.getHeight() / 2 + 1);
    Rectangle rectQ3 = new Rectangle(getRect.getX() + getRect.getWidth() / 2 + 1,
            getRect.getY() + getRect.getHeight() / 2 + 1, getRect.getWidth() / 2 + 1,
            getRect.getHeight() / 2 + 1);
    Rectangle rectQ4 = new Rectangle(getRect.getX() + getRect.getWidth() / 2 + 1, getRect.getY(),
            getRect.getWidth() / 2 + 1, getRect.getHeight() / 2 + 1);

    for (ListIterator<PatternInstance> iter = instanceList.listIterator(); iter.hasNext();) {
        currentInstance = iter.next();
        if (currentInstance.getRectangle().overlaps(rectQ1))
            listQ1.add(currentInstance);
        if (currentInstance.getRectangle().overlaps(rectQ2))
            listQ2.add(currentInstance);
        if (currentInstance.getRectangle().overlaps(rectQ3))
            listQ3.add(currentInstance);
        if (currentInstance.getRectangle().overlaps(rectQ4))
            listQ4.add(currentInstance);
    }

    for (TreeSet<PatternInstance> t1 : findCollisions(listQ1, caseThreshold * 2))
        resolveTree(returnSet, t1);
    for (TreeSet<PatternInstance> t2 : findCollisions(listQ2, caseThreshold * 2))
        resolveTree(returnSet, t2);
    for (TreeSet<PatternInstance> t3 : findCollisions(listQ3, caseThreshold * 2))
        resolveTree(returnSet, t3);
    for (TreeSet<PatternInstance> t4 : findCollisions(listQ4, caseThreshold * 2))
        resolveTree(returnSet, t4);

    return returnSet;

}

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}.
 *//*from ww w  .j a v  a  2  s . co 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.coldpixel.mario.Sprites.InteractiveTileObject.java

public InteractiveTileObject(World world, TiledMap map, Rectangle bounds) {
    this.world = world;
    this.map = map;
    this.bounds = bounds;

    BodyDef bdef = new BodyDef();
    FixtureDef fdef = new FixtureDef();
    PolygonShape shape = new PolygonShape();

    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set((bounds.getX() + bounds.getWidth() / 2) / MarioBros.PPM,
            (bounds.getY() + bounds.getHeight() / 2) / MarioBros.PPM);

    body = world.createBody(bdef);//from w  ww. j a  v  a 2s  .c  om

    shape.setAsBox(bounds.getWidth() / 2 / MarioBros.PPM, bounds.getHeight() / 2 / MarioBros.PPM);
    fdef.shape = shape;
    body.createFixture(fdef);

}

From source file:ch.coldpixel.mario.Tools.B2WorldCreator.java

public B2WorldCreator(World world, TiledMap map) {
    //Create body and fixture variables
    BodyDef bdef = new BodyDef();
    PolygonShape shape = new PolygonShape();
    FixtureDef fdef = new FixtureDef();
    Body body;/* w  w  w.  j a v a 2  s  . c  o  m*/

    //create ground bodies/fixture
    for (MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)) { //get(2) = in TILED from below the 2nd object(starts with 0, so it gets ground)
        Rectangle rect = ((RectangleMapObject) object).getRectangle(); //Get Rectangle Object itself

        bdef.type = BodyDef.BodyType.StaticBody;//3 different bodys, static=selfexplaining, dynamic=typical player, kinematic=not effected by forces like gravity, but can manipulated with velocity, ex: moving plattforms
        bdef.position.set((rect.getX() + rect.getWidth() / 2) / MarioBros.PPM,
                (rect.getY() + rect.getHeight() / 2) / MarioBros.PPM);

        body = world.createBody(bdef);//add body to our world

        shape.setAsBox(rect.getWidth() / 2 / MarioBros.PPM, rect.getHeight() / 2 / MarioBros.PPM);
        fdef.shape = shape;
        body.createFixture(fdef);
    }

    //create pipe bodies/fixture
    for (MapObject object : map.getLayers().get(3).getObjects().getByType(RectangleMapObject.class)) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        bdef.type = BodyDef.BodyType.StaticBody;
        bdef.position.set((rect.getX() + rect.getWidth() / 2) / MarioBros.PPM,
                (rect.getY() + rect.getHeight() / 2) / MarioBros.PPM);

        body = world.createBody(bdef);

        shape.setAsBox(rect.getWidth() / 2 / MarioBros.PPM, rect.getHeight() / 2 / MarioBros.PPM);
        fdef.shape = shape;
        body.createFixture(fdef);
    }

    //create brick bodies/fixture
    for (MapObject object : map.getLayers().get(5).getObjects().getByType(RectangleMapObject.class)) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        new Brick(world, map, rect);

    }

    //create coin bodies/fixtures
    for (MapObject object : map.getLayers().get(4).getObjects().getByType(RectangleMapObject.class)) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        new Coin(world, map, rect);

    }

}

From source file:com.agateau.pixelwheels.utils.Box2DUtils.java

License:Open Source License

public static Body createStaticBodyForMapObject(World world, MapObject object) {
    final float u = Constants.UNIT_FOR_PIXEL;
    float rotation = object.getProperties().get("rotation", 0f, Float.class);

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyDef.angle = -rotation * MathUtils.degreesToRadians;

    if (object instanceof RectangleMapObject) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        /*/*from ww w. j  a  v a2s .c  om*/
          A          D
           x--------x
           |        |
           x--------x
          B          C
         */
        float[] vertices = new float[8];
        // A
        vertices[0] = 0;
        vertices[1] = 0;
        // B
        vertices[2] = 0;
        vertices[3] = -rect.getHeight();
        // C
        vertices[4] = rect.getWidth();
        vertices[5] = -rect.getHeight();
        // D
        vertices[6] = rect.getWidth();
        vertices[7] = 0;
        scaleVertices(vertices, u);

        bodyDef.position.set(u * rect.getX(), u * (rect.getY() + rect.getHeight()));
        Body body = world.createBody(bodyDef);

        PolygonShape shape = new PolygonShape();
        shape.set(vertices);

        body.createFixture(shape, 1);
        return body;
    } else if (object instanceof PolygonMapObject) {
        Polygon polygon = ((PolygonMapObject) object).getPolygon();
        float[] vertices = polygon.getVertices().clone();
        scaleVertices(vertices, u);

        bodyDef.position.set(polygon.getX() * u, polygon.getY() * u);
        Body body = world.createBody(bodyDef);

        PolygonShape shape = new PolygonShape();
        shape.set(vertices);

        body.createFixture(shape, 1);
        return body;
    } else if (object instanceof EllipseMapObject) {
        Ellipse ellipse = ((EllipseMapObject) object).getEllipse();
        float radius = ellipse.width * u / 2;
        float x = ellipse.x * u + radius;
        float y = ellipse.y * u + radius;

        bodyDef.position.set(x, y);
        Body body = world.createBody(bodyDef);

        CircleShape shape = new CircleShape();
        shape.setRadius(radius);

        body.createFixture(shape, 1);
        return body;
    }
    throw new RuntimeException("Unsupported MapObject type: " + object);
}

From source file:com.bladecoder.engine.actions.SayAction.java

License:Apache License

@Override
public boolean run(VerbRunner cb) {
    float x, y;/*from ww  w. ja  v a2s .c o m*/
    Color color = null;

    setVerbCb(cb);
    InteractiveActor a = (InteractiveActor) World.getInstance().getCurrentScene().getActor(actor, false);

    if (soundId != null)
        a.playSound(soundId);

    if (text != null) {
        if (type != Text.Type.TALK) {
            x = y = TextManager.POS_SUBTITLE;
        } else {

            Rectangle boundingRectangle = a.getBBox().getBoundingRectangle();

            x = boundingRectangle.getX() + boundingRectangle.getWidth() / 2;
            y = boundingRectangle.getY() + boundingRectangle.getHeight();

            color = ((CharacterActor) a).getTextColor();

            restoreStandPose((CharacterActor) a);
            startTalkAnim((CharacterActor) a);
        }

        World.getInstance().getTextManager().addText(text, x, y, queue, type, color, null, this);
    }

    return getWait();
}

From source file:com.bladecoder.engine.actions.SayDialogAction.java

License:Apache License

@Override
public boolean run(VerbRunner cb) {
    World w = World.getInstance();//from  w w  w. j av  a  2  s  .  c  o  m

    if (w.getCurrentDialog() == null || w.getCurrentDialog().getCurrentOption() == null) {
        EngineLogger.debug("SayDialogAction WARNING: Current dialog doesn't found.");

        return false;
    }

    setVerbCb(cb);
    DialogOption o = w.getCurrentDialog().getCurrentOption();
    String playerText = o.getText();

    responseText = o.getResponseText();
    characterName = w.getCurrentDialog().getActor();

    characterTurn = true;
    previousAnim = null;

    // If the player or the character is talking restore to 'stand' pose
    restoreStandPose(w.getCurrentScene().getPlayer());

    if (w.getCurrentScene().getActor(characterName, false) instanceof SpriteActor)
        restoreStandPose((CharacterActor) w.getCurrentScene().getActor(characterName, false));

    if (playerText != null) {
        CharacterActor player = w.getCurrentScene().getPlayer();

        Rectangle boundingRectangle = player.getBBox().getBoundingRectangle();
        float x = boundingRectangle.getX() + boundingRectangle.getWidth() / 2;
        float y = boundingRectangle.getY() + boundingRectangle.getHeight();

        w.getTextManager().addText(playerText, x, y, false, Text.Type.TALK, player.getTextColor(), null, this);

        startTalkAnim(player);

    } else {
        resume();
    }

    return getWait();
}

From source file:com.bladecoder.engine.actions.SayDialogAction.java

License:Apache License

@Override
public void resume() {

    World w = World.getInstance();/*  w ww  .java  2s . com*/
    BaseActor actor = w.getCurrentScene().getActor(characterName, false);

    if (characterTurn) {
        characterTurn = false;

        if (previousAnim != null) {
            SpriteActor player = World.getInstance().getCurrentScene().getPlayer();
            player.startAnimation(previousAnim, null);
        }

        if (responseText != null) {
            Rectangle boundingRectangle = actor.getBBox().getBoundingRectangle();
            float x = boundingRectangle.getX() + boundingRectangle.getWidth() / 2;
            float y = boundingRectangle.getY() + boundingRectangle.getHeight();

            World.getInstance().getTextManager().addText(responseText, x, y, false, Text.Type.TALK,
                    ((CharacterActor) actor).getTextColor(), null, this);

            if (actor instanceof CharacterActor) {
                startTalkAnim((CharacterActor) actor);
            }
        } else {
            previousAnim = null;
            super.resume();
        }
    } else {
        if (actor instanceof SpriteActor && previousAnim != null) {
            ((SpriteActor) actor).startAnimation(previousAnim, null);
        }
        super.resume();
    }
}

From source file:com.bladecoder.engine.ui.defaults.DefaultSceneScreen.java

License:Apache License

private void drawDebugText(SpriteBatch batch) {
    World w = World.getInstance();/*from   w ww .  ja v a2s  . c  o  m*/

    w.getSceneCamera().getInputUnProject(viewport, unprojectTmp);

    Color color;

    sbTmp.setLength(0);

    if (EngineLogger.lastError != null) {
        sbTmp.append(EngineLogger.lastError);

        color = Color.RED;
    } else {

        sbTmp.append("( ");
        sbTmp.append((int) unprojectTmp.x);
        sbTmp.append(", ");
        sbTmp.append((int) unprojectTmp.y);
        sbTmp.append(") FPS:");
        sbTmp.append(Gdx.graphics.getFramesPerSecond());
        // sbTmp.append(" Density:");
        // sbTmp.append(Gdx.graphics.getDensity());
        // sbTmp.append(" UI Multiplier:");
        // sbTmp.append(DPIUtils.getSizeMultiplier());
        sbTmp.append(" UI STATE: ");
        sbTmp.append(state.toString());
        sbTmp.append(' ');

        long millis = w.getTimeOfGame();
        long second = (millis / 1000) % 60;
        long minute = (millis / (1000 * 60)) % 60;
        long hour = (millis / (1000 * 60 * 60));

        String time = String.format("%02d:%02d:%02d", hour, minute, second);

        sbTmp.append(time);

        //         if (w.getCurrentScene().getPlayer() != null) {
        //            sbTmp.append(" Depth Scl: ");
        //            sbTmp.append(w.getCurrentScene().getFakeDepthScale(unprojectTmp.y));
        //         }

        color = Color.WHITE;
    }

    String strDebug = sbTmp.toString();

    textLayout.setText(ui.getSkin().getFont("debug"), strDebug, color, viewport.getScreenWidth(), Align.left,
            true);
    RectangleRenderer.draw(batch, 0, viewport.getScreenHeight() - textLayout.height - 10, textLayout.width,
            textLayout.height + 10, Color.BLACK);
    ui.getSkin().getFont("debug").draw(batch, textLayout, 0, viewport.getScreenHeight() - 5);

    // Draw actor states when debug
    if (EngineLogger.getDebugLevel() == EngineLogger.DEBUG1) {

        for (BaseActor a : w.getCurrentScene().getActors().values()) {

            if (a instanceof AnchorActor)
                continue;

            Rectangle r = a.getBBox().getBoundingRectangle();
            sbTmp.setLength(0);
            sbTmp.append(a.getId());
            if (a instanceof InteractiveActor && ((InteractiveActor) a).getState() != null)
                sbTmp.append(".").append(((InteractiveActor) a).getState());

            unprojectTmp.set(r.getX(), r.getY(), 0);
            w.getSceneCamera().scene2screen(viewport, unprojectTmp);
            ui.getSkin().getFont("debug").draw(batch, sbTmp.toString(), unprojectTmp.x, unprojectTmp.y);
        }

    }
}

From source file:com.bladecoder.engine.ui.defaults.DefaultSceneScreen.java

License:Apache License

private void drawHotspots(SpriteBatch batch) {
    final World world = World.getInstance();
    for (BaseActor a : world.getCurrentScene().getActors().values()) {
        if (!(a instanceof InteractiveActor) || !a.isVisible() || a == world.getCurrentScene().getPlayer())
            continue;

        InteractiveActor ia = (InteractiveActor) a;

        if (!ia.canInteract())
            continue;

        Polygon p = a.getBBox();/*from w ww  .  j av  a2 s  .  c om*/

        if (p == null) {
            EngineLogger.error("ERROR DRAWING HOTSPOT FOR: " + a.getId());
        }

        Rectangle r = a.getBBox().getBoundingRectangle();

        unprojectTmp.set(r.getX() + r.getWidth() / 2, r.getY() + r.getHeight() / 2, 0);
        world.getSceneCamera().scene2screen(viewport, unprojectTmp);

        if (!showDesc || ia.getDesc() == null) {

            float size = DPIUtils.ICON_SIZE * DPIUtils.getSizeMultiplier();

            if (ia.getVerb("leave") != null) {
                TextureRegionDrawable drawable = (TextureRegionDrawable) getUI().getSkin().getDrawable("leave");

                // drawable.draw(batch, unprojectTmp.x - size / 2,
                // unprojectTmp.y - size / 2, size, size);

                drawable.draw(batch, unprojectTmp.x - size / 2, unprojectTmp.y - size / 2, size / 2, size / 2,
                        size, size, 1.0f, 1.0f, calcLeaveArrowRotation(ia));
            } else {
                Drawable drawable = ((TextureRegionDrawable) getUI().getSkin().getDrawable("hotspot"))
                        .tint(Color.RED);

                drawable.draw(batch, unprojectTmp.x - size / 2, unprojectTmp.y - size / 2, size, size);
            }
        } else {
            BitmapFont font = getUI().getSkin().getFont("desc");
            String desc = ia.getDesc();
            if (desc.charAt(0) == I18N.PREFIX)
                desc = I18N.getString(desc.substring(1));

            textLayout.setText(font, desc);

            float textX = unprojectTmp.x - textLayout.width / 2;
            float textY = unprojectTmp.y + textLayout.height;

            RectangleRenderer.draw(batch, textX - 8, textY - textLayout.height - 8, textLayout.width + 16,
                    textLayout.height + 16, Color.BLACK);
            font.draw(batch, textLayout, textX, textY);
        }
    }
}