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

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

Introduction

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

Prototype

public float getHeight() 

Source Link

Usage

From source file:Tabox2D.java

License:Open Source License

private Tabody generateRegularPoly(String name, String type, float x, float y, float rad) {
    // Scale proportions:
    x /= meterSize;//from   w  ww  .j a  v  a 2 s . co m
    y /= meterSize;
    rad /= meterSize;

    PolygonShape polygonShape;
    BodyDef defPoly = new BodyDef();

    setType(defPoly, type);

    // Generate points:
    List<Vector2> pts = new ArrayList<Vector2>();
    Vector2 p0 = new Vector2(0, rad);

    float conv = MathUtils.degreesToRadians;
    float angleInDeg = polyInfo.get(name + "_angle");
    float cos = MathUtils.cos(conv * angleInDeg);
    float sin = MathUtils.sin(conv * angleInDeg);

    for (int i = 0; i < polyInfo.get(name); i++) {
        pts.add(new Vector2(p0.x, p0.y));
        p0.set(p0.x, p0.y);

        float newX = p0.x * cos - p0.y * sin;
        float newY = p0.x * sin + p0.y * cos;

        p0.x = newX;
        p0.y = newY;
    }

    // Get bounding box:

    float[] rawPoints = new float[pts.size() * 2];
    int pointIndex = 0;
    for (int i = 0; i < rawPoints.length - 1; i += 2) {
        rawPoints[i] = pts.get(pointIndex).x;
        rawPoints[i + 1] = pts.get(pointIndex).y;
        pointIndex++;
    }

    Polygon polyForBox = new Polygon();
    polyForBox.setVertices(rawPoints);

    Rectangle boundingRect = polyForBox.getBoundingRectangle();
    float boxX = boundingRect.x;
    float boxY = boundingRect.y;
    float boxW = boundingRect.getWidth();
    float boxH = boundingRect.getHeight();

    Vector2 aabbCenter = new Vector2(boxX + boxW / 2, boxY + boxH / 2);
    defPoly.position.set(x, y);

    Tabody regularPoly = new Tabody();
    regularPoly.body = world.createBody(defPoly);
    //regularPoly.body.setFixedRotation(true);
    polygonShape = new PolygonShape();

    //polygonShape.setAsBox(w / 2, h / 2);
    for (int i = 0; i < rawPoints.length - 1; i += 2) {
        rawPoints[i] -= aabbCenter.x;
        rawPoints[i + 1] -= aabbCenter.y;
    }
    //rawPoints[0] += 0.5;
    polygonShape.set(rawPoints);

    FixtureDef fixtureBox = new FixtureDef();
    fixtureBox.shape = polygonShape;
    fixtureBox.density = 1;
    fixtureBox.friction = 1;
    fixtureBox.restitution = 0;

    ////////////////////////////////////////
    regularPoly.w = boxW * meterSize;//radius * 2 * meterSize;
    regularPoly.h = boxH * meterSize;//radius * 2 * meterSize;
    regularPoly.fixture = fixtureBox;
    regularPoly.bodyType = "poly";
    ////////////////////////////////////////

    regularPoly.body.createFixture(fixtureBox);
    polygonShape.dispose();
    tabodies.add(regularPoly);
    return regularPoly;
}

From source file:at.therefactory.jewelthief.actors.enemies.Cloud.java

License:Open Source License

@Override
public void update(Rectangle enemyField) {
    if (!sprite.getBoundingRectangle().contains(enemyField)) {
        if (sprite.getBoundingRectangle().x + sprite.getBoundingRectangle().width >= enemyField.x
                + enemyField.width) {//from   w w  w. j  av  a 2 s .c  o  m
            movementInverter.x = -1;
        } else if (sprite.getBoundingRectangle().x <= enemyField.x) {
            movementInverter.x = 1;
        } else if (sprite.getBoundingRectangle().y <= enemyField.y) {
            position.y = (enemyField.y + enemyField.getHeight());
        }
    }
    position.x += speed * movementInverter.x;
    position.y -= speed;
    sprite.setPosition(position.x - sprite.getWidth() / 2, position.y - sprite.getHeight() / 2);
}

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();//w w  w . j  a  v  a2  s.  c o m
            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}.
 *///  ww  w  . jav  a2  s  .  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.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);/*w w w  . ja  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;/*from  www .  jav a2s. com*/

    //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   w w  w  . ja v  a  2s. 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.  j av a2  s .  co 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   ww  w.  j  ava2s  .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();/*from w w w .j  a  v  a 2  s .c o  m*/
    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();
    }
}