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

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

Introduction

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

Prototype

public float getX() 

Source Link

Document

Returns the x-coordinate of the polygon's position within the world.

Usage

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.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.loader.ChapterXMLLoader.java

License:Apache License

public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
        throws SAXException {

    if (currentVerb != null) { // INSIDE VERB

        if (!localName.equals(XMLConstants.ACTION_TAG)) {
            SAXParseException e2 = new SAXParseException("TAG not supported inside VERB: " + localName,
                    locator);/* w  w w. j av a 2s . c  om*/
            error(e2);
            throw e2;
        }

        parseAction(atts, actor != null ? actor.getId() : null);
    } else if (currentDialog != null) { // INSIDE DIALOG

        if (!localName.equals(XMLConstants.OPTION_TAG)) {
            SAXParseException e2 = new SAXParseException("Only 'option' tag allowed in dialogs", locator);
            error(e2);
            throw e2;
        }

        parseOption(atts);

    } else if (localName.equals(XMLConstants.ACTOR_TAG)) {
        parseActor(atts);
    } else if (localName.equals(XMLConstants.ANIMATION_TAG)) {
        parseAnimation(atts);
    } else if (localName.equals(XMLConstants.VERB_TAG)) {
        parseVerb(atts, actor != null ? ((InteractiveActor) actor).getVerbManager() : scene.getVerbManager());
    } else if (localName.equals(XMLConstants.DIALOG_TAG)) {
        String id = atts.getValue(XMLConstants.ID_ATTR);

        currentDialog = new Dialog();
        currentDialog.setId(id);
        currentDialog.setActor(actor.getId());

        ((CharacterActor) actor).addDialog(currentDialog);
    } else if (localName.equals(XMLConstants.SOUND_TAG)) {
        parseSound(atts, (InteractiveActor) actor);
    } else if (localName.equals(XMLConstants.CHAPTER_TAG)) {
        initScene = atts.getValue(XMLConstants.INIT_SCENE_ATTR);
    } else if (localName.equals(XMLConstants.WALK_ZONE_TAG)) {
        PolygonalNavGraph polygonalPathFinder = new PolygonalNavGraph();
        Polygon poly = new Polygon();

        Param.parsePolygon(poly, atts.getValue(XMLConstants.POLYGON_ATTR),
                atts.getValue(XMLConstants.POS_ATTR));
        poly.setScale(scale, scale);
        poly.setPosition(poly.getX() * scale, poly.getY() * scale);
        polygonalPathFinder.setWalkZone(poly);

        scene.setPolygonalNavGraph(polygonalPathFinder);
    } else if (localName.equals(XMLConstants.SCENE_TAG)) {
        parseScene(atts);
    } else if (localName.equals(XMLConstants.LAYER_TAG)) {
        parseLayer(atts);
    } else {
        // SAXParseException e = new SAXParseException("Wrong label '"
        // + localName + "' loading Scene.", locator);
        // error(e);
        // throw e;
        EngineLogger.error(
                "TAG not supported in Chapter document: " + localName + " LINE: " + locator.getLineNumber());
    }
}

From source file:com.bladecoder.engine.model.Scene.java

License:Apache License

public void drawBBoxLines(ShapeRenderer renderer) {
    // renderer.begin(ShapeType.Rectangle);
    renderer.begin(ShapeType.Line);

    for (BaseActor a : actors.values()) {
        Polygon p = a.getBBox();

        if (p == null) {
            EngineLogger.error("ERROR DRAWING BBOX FOR: " + a.getId());
        }/*from  w w  w  .jav a  2s .c  o  m*/

        if (a instanceof ObstacleActor) {
            renderer.setColor(OBSTACLE_COLOR);
            renderer.polygon(p.getTransformedVertices());
        } else if (a instanceof AnchorActor) {
            renderer.setColor(Scene.ANCHOR_COLOR);
            renderer.line(p.getX() - Scene.ANCHOR_RADIUS, p.getY(), p.getX() + Scene.ANCHOR_RADIUS, p.getY());
            renderer.line(p.getX(), p.getY() - Scene.ANCHOR_RADIUS, p.getX(), p.getY() + Scene.ANCHOR_RADIUS);
        } else {
            renderer.setColor(ACTOR_BBOX_COLOR);
            renderer.polygon(p.getTransformedVertices());
        }

        // Rectangle r = a.getBBox().getBoundingRectangle();
        // renderer.rect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
    }

    if (polygonalNavGraph != null) {
        renderer.setColor(WALKZONE_COLOR);
        renderer.polygon(polygonalNavGraph.getWalkZone().getTransformedVertices());

        // DRAW LINEs OF SIGHT
        renderer.setColor(Color.WHITE);
        ArrayList<NavNodePolygonal> nodes = polygonalNavGraph.getGraphNodes();
        for (NavNodePolygonal n : nodes) {
            for (NavNodePolygonal n2 : n.neighbors) {
                renderer.line(n.x, n.y, n2.x, n2.y);
            }
        }
    }

    renderer.end();
}

From source file:com.bladecoder.engine.util.PolygonUtils.java

License:Apache License

public static void addPoint(Polygon poly, float x, float y, int index) {
    float verts[] = poly.getVertices();

    x -= poly.getX();
    y -= poly.getY();//from  w w  w.  j  a  va2 s . c  o  m

    int length = verts.length;
    float destination[] = new float[length + 2];

    System.arraycopy(verts, 0, destination, 0, index);
    destination[index] = x;
    destination[index + 1] = y;
    System.arraycopy(verts, index, destination, index + 2, length - index);

    poly.setVertices(destination);
}

From source file:com.bladecoder.engineeditor.model.ChapterDocument.java

License:Apache License

public Element createWalkZone(Element scn, Polygon poly) {
    Element e = doc.createElement(XMLConstants.WALK_ZONE_TAG);
    e.setAttribute(XMLConstants.POLYGON_ATTR, Param.toStringParam(poly));
    e.setAttribute(XMLConstants.POS_ATTR, Param.toStringParam(new Vector2(poly.getX(), poly.getY())));

    scn.appendChild(e);// w  w  w . j ava 2s.c o  m

    modified = true;
    firePropertyChange(XMLConstants.WALK_ZONE_TAG, e);

    return e;
}

From source file:com.bladecoder.engineeditor.model.ChapterDocument.java

License:Apache License

public void setWalkZonePolygon(Element scn, Polygon poly) {
    Element e = getWalkZone(scn);

    if (e == null)
        e = createWalkZone(scn, poly);//from  w w  w. ja  va2s. com
    else {
        e.setAttribute(XMLConstants.POLYGON_ATTR, Param.toStringParam(poly));
        e.setAttribute(XMLConstants.POS_ATTR, Param.toStringParam(new Vector2(poly.getX(), poly.getY())));
    }

    modified = true;
    firePropertyChange(XMLConstants.WALK_ZONE_TAG, e);

}

From source file:com.bladecoder.engineeditor.model.ChapterDocument.java

License:Apache License

public Element createObstacle(Element scn, Polygon poly) {
    Element e = doc.createElement(XMLConstants.OBSTACLE_ATTR);
    e.setAttribute(XMLConstants.POLYGON_ATTR, Param.toStringParam(poly));
    e.setAttribute(XMLConstants.POS_ATTR, Param.toStringParam(new Vector2(poly.getX(), poly.getY())));

    getWalkZone(scn).appendChild(e);/*w ww.  j a  va 2  s .c o m*/

    modified = true;
    firePropertyChange(XMLConstants.OBSTACLE_ATTR, e);

    return e;
}

From source file:com.bladecoder.engineeditor.model.ChapterDocument.java

License:Apache License

public void setObstaclePolygon(Element scn, int i, Polygon poly) {
    Element e = getObstacle(scn, i);

    if (e == null)
        return;//from  w ww.  ja  v  a 2s  .c  om

    e.setAttribute(XMLConstants.POLYGON_ATTR, Param.toStringParam(poly));
    e.setAttribute(XMLConstants.POS_ATTR, Param.toStringParam(new Vector2(poly.getX(), poly.getY())));

    modified = true;
    firePropertyChange(XMLConstants.OBSTACLE_ATTR, e);
}

From source file:com.bladecoder.engineeditor.scneditor.CanvasDrawer.java

License:Apache License

public void drawBBoxActors(Scene scn) {
    drawer.setProjectionMatrix(camera.combined);
    drawer.setTransformMatrix(new Matrix4());
    drawer.begin(ShapeType.Line);

    for (BaseActor a : scn.getActors().values()) {

        Polygon p = a.getBBox();

        if (p == null) {
            EngineLogger.error("ERROR DRAWING BBOX FOR: " + a.getId());
        }//from   ww  w.  j av  a2  s  .  co  m

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

        if (a instanceof ObstacleActor) {
            drawer.setColor(Scene.OBSTACLE_COLOR);
            drawer.polygon(p.getTransformedVertices());
        } else if (a instanceof InteractiveActor) {
            InteractiveActor iActor = (InteractiveActor) a;

            if (!scn.getLayer(iActor.getLayer()).isVisible())
                continue;

            drawer.setColor(Scene.ACTOR_BBOX_COLOR);
            if (p.getTransformedVertices().length > 2)
                drawer.polygon(p.getTransformedVertices());
        } else if (a instanceof AnchorActor) {
            drawer.setColor(Scene.ANCHOR_COLOR);
            drawer.line(p.getX() - Scene.ANCHOR_RADIUS, p.getY(), p.getX() + Scene.ANCHOR_RADIUS, p.getY());
            drawer.line(p.getX(), p.getY() - Scene.ANCHOR_RADIUS, p.getX(), p.getY() + Scene.ANCHOR_RADIUS);
        }

        // drawer.rect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
    }

    drawer.end();
}

From source file:com.bladecoder.engineeditor.scneditor.ScnWidgetInputListener.java

License:Apache License

@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

    super.touchDown(event, x, y, pointer, button);
    //      EditorLogger.debug("Touch Down - X: " + x + " Y: " + y);

    Scene scn = scnWidget.getScene();//w w  w  . j  ava 2 s  .c o m
    if (scn == null)
        return false;

    Vector2 p = new Vector2(Gdx.input.getX(), Gdx.input.getY());
    scnWidget.screenToWorldCoords(p);
    org.set(p);

    if (button == Buttons.LEFT) {
        selActor = scnWidget.getSelectedActor();

        if (scn.getPolygonalNavGraph() != null && scnWidget.getShowWalkZone()) { // Check
            // WALKZONE

            // CHECK WALKZONE VERTEXS
            Polygon wzPoly = scn.getPolygonalNavGraph().getWalkZone();
            float verts[] = wzPoly.getTransformedVertices();

            for (int i = 0; i < verts.length; i += 2) {
                if (p.dst(verts[i], verts[i + 1]) < CanvasDrawer.CORNER_DIST) {
                    draggingMode = DraggingModes.DRAGGING_WALKZONE_POINT;
                    vertIndex = i;
                    return true;
                }
            }

            // CHECK FOR WALKZONE DRAGGING
            if (wzPoly.contains(p.x, p.y)) {
                draggingMode = DraggingModes.DRAGGING_WALKZONE;
                undoOrg.set(wzPoly.getX(), wzPoly.getY());
                return true;
            }

        }

        // SELACTOR VERTEXs DRAGGING
        if (selActor != null
                && (!(selActor instanceof SpriteActor) || !((SpriteActor) selActor).isBboxFromRenderer())
                && !(scnWidget.getSelectedActor() instanceof AnchorActor)) {

            Polygon bbox = selActor.getBBox();
            float verts[] = bbox.getTransformedVertices();
            for (int i = 0; i < verts.length; i += 2) {
                if (p.dst(verts[i], verts[i + 1]) < CanvasDrawer.CORNER_DIST) {
                    draggingMode = DraggingModes.DRAGGING_BBOX_POINT;
                    vertIndex = i;
                    return true;
                }
            }
        }

        BaseActor a = scn.getActorAt(p.x, p.y); // CHECK FOR ACTORS

        if (a != null && a != selActor) {
            selActor = a;
            BaseActor da = Ctx.project.getActor(selActor.getId());
            Ctx.project.setSelectedActor(da);
        }

        if (a != null) {
            draggingMode = DraggingModes.DRAGGING_ACTOR;
            undoOrg.set(selActor.getX(), selActor.getY());
            return true;
        }

        // CHECK FOR DRAGGING DEPTH MARKERS
        Vector2 depthVector = scnWidget.getScene().getDepthVector();
        if (depthVector != null) {
            p.set(0, depthVector.x);
            scnWidget.worldToScreenCoords(p);
            if (Vector2.dst(p.x - 40, p.y, x, y) < 50) {
                draggingMode = DraggingModes.DRAGGING_MARKER_0;
                return true;
            }

            p.set(0, depthVector.y);
            scnWidget.worldToScreenCoords(p);
            if (Vector2.dst(p.x - 40, p.y, x, y) < 50) {
                draggingMode = DraggingModes.DRAGGING_MARKER_100;
                return true;
            }
        }

    }

    return true;
}