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

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

Introduction

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

Prototype

public Polygon() 

Source Link

Document

Constructs a new polygon with no vertices.

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   ww w  .  java  2s  . c om*/
    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:Tabox2D.java

License:Open Source License

/**
 * Creates a Polygons with the given points
 * @param type "dynamic" or "static"//from   w  ww .  ja v  a 2s  . c  o m
 * @param pts points for the polygon
 * @return A new Tabody instance
 */
public Tabody newPoly(String type, float[] pts) {
    // Scale proportions:
    for (int i = 0; i < pts.length; i++) {
        pts[i] /= meterSize;
    }

    PolygonShape polygonShape;
    BodyDef defPoly = new BodyDef();

    setType(defPoly, type);

    // Get bounding box:

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

    //polyForBox.translate(center.x, center.y);

    Rectangle boundingRect = boundingBoxOf(polyForBox.getVertices());
    Vector2 aabbCenter = new Vector2(boundingRect.x + boundingRect.width / 2,
            boundingRect.y + boundingRect.height / 2);
    defPoly.position.set(aabbCenter.x, aabbCenter.y);

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

    for (int i = 0; i < pts.length - 1; i += 2) {
        pts[i] -= aabbCenter.x;
        pts[i + 1] -= aabbCenter.y;
    }
    polygonShape.set(pts);

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

    ////////////////////////////////////////
    regularPoly.w = boundingRect.width * meterSize;//radius * 2 * meterSize;
    regularPoly.h = boundingRect.height * meterSize;//radius * 2 * meterSize;
    regularPoly.fixture = fixtureBox;
    regularPoly.bodyType = "poly";
    ////////////////////////////////////////

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

From source file:Tabox2D.java

License:Open Source License

/**
 * Returns the bounding box of the given polygon
 * @param ptsCombined The points as Vector2 list
 * @return A Rectangle object//from w  w  w  .  ja v a 2s  . co  m
 */
private Rectangle boundingBoxOf(List<Vector2> ptsCombined) {
    float[] rawPtsCombined = new float[ptsCombined.size() * 2];
    int ptsCombinedIndex = 0;
    for (int i = 0; i < rawPtsCombined.length - 1; i += 2) {
        rawPtsCombined[i] = ptsCombined.get(ptsCombinedIndex).x;
        rawPtsCombined[i + 1] = ptsCombined.get(ptsCombinedIndex).y;
        ptsCombinedIndex++;
    }

    Polygon polygon = new Polygon();
    polygon.setVertices(rawPtsCombined);
    Rectangle boundingRect = polygon.getBoundingRectangle();
    return boundingRect;
}

From source file:Tabox2D.java

License:Open Source License

/**
 * Returns the bounding box of the given polygon
 * @param ptsCombined The points as a float[] object
 * @return A Rectangle object//from  w  w  w.j a v a2s.  com
 */
private Rectangle boundingBoxOf(float[] ptsCombined) {
    Polygon polygon = new Polygon();
    polygon.setVertices(ptsCombined);
    Rectangle boundingRect = polygon.getBoundingRectangle();
    return boundingRect;
}

From source file:com.agateau.pixelwheels.vehicledef.VehicleIO.java

License:Open Source License

private static Shape2D loadShape(XmlReader.Element element, float vehicleWidth, float vehicleHeight) {
    String type = element.getName();
    if (type.equals("octogon")) {
        float width = element.getFloatAttribute("height");
        float height = element.getFloatAttribute("width");
        float x = element.getFloatAttribute("y", (vehicleWidth - width) / 2);
        float y = element.getFloatAttribute("x", (vehicleHeight - height) / 2);
        float corner = element.getFloatAttribute("corner", 0);
        Polygon polygon = new Polygon();
        polygon.setVertices(new float[] { width / 2 - corner, -height / 2, width / 2, -height / 2 + corner,
                width / 2, height / 2 - corner, width / 2 - corner, height / 2, -width / 2 + corner, height / 2,
                -width / 2, height / 2 - corner, -width / 2, -height / 2 + corner, -width / 2 + corner,
                -height / 2 });//from   ww  w .  j a v  a  2s. c o  m
        polygon.translate(x - (vehicleWidth - width) / 2, y - (vehicleHeight - height) / 2);
        return polygon;
    } else if (type.equals("trapezoid")) {
        float bottomHeight = element.getFloatAttribute("bottomWidth");
        float topHeight = element.getFloatAttribute("topWidth");
        float height = Math.max(bottomHeight, topHeight);
        float width = element.getFloatAttribute("height");
        float x = element.getFloatAttribute("y", (vehicleWidth - width) / 2);
        float y = element.getFloatAttribute("x", (vehicleHeight - height) / 2);
        Polygon polygon = new Polygon();
        polygon.setVertices(new float[] { width / 2, topHeight / 2, -width / 2, bottomHeight / 2, -width / 2,
                -bottomHeight / 2, width / 2, -topHeight / 2, });
        polygon.translate(x - (vehicleWidth - width) / 2, y - (vehicleHeight - height) / 2);
        return polygon;
    } else {
        throw new RuntimeException("Unknown shape type: " + element);
    }
}

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);//from  ww  w .  j a  va 2 s .  c  o  m
            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.engineeditor.model.ChapterDocument.java

License:Apache License

public BaseActor getEngineActor(Element e) {
    BaseActor a = null;/*from  www  . j  a va 2  s . c  o  m*/

    String type = getType(e);

    if (type.equals(XMLConstants.ATLAS_VALUE)) {
        a = new SpriteActor();
        ((SpriteActor) a).setRenderer(new AtlasRenderer());
    } else if (type.equals(XMLConstants.S3D_VALUE)) {
        a = new SpriteActor();
        Sprite3DRenderer r = new Sprite3DRenderer();
        ((SpriteActor) a).setRenderer(r);
        r.setSpriteSize(Param.parseVector2(e.getAttribute(XMLConstants.SPRITE_SIZE_ATTR)));

    } else if (type.equals(XMLConstants.SPINE_VALUE)) {
        a = new SpriteActor();
        SpineRenderer r = new SpineRenderer();
        r.enableEvents(false);
        ((SpriteActor) a).setRenderer(r);
    } else if (type.equals(XMLConstants.IMAGE_VALUE)) {
        a = new SpriteActor();
        ((SpriteActor) a).setRenderer(new ImageRenderer());
    } else if (type.equals(XMLConstants.NO_RENDERER_VALUE)) {
        a = new BaseActor();
    } else {
        EngineLogger.error(" Wrong actor Type defined in XML");
        return null;
    }

    String layer = e.getAttribute(XMLConstants.LAYER_ATTR);
    a.setLayer(layer);

    a.setId(getId(e));
    Polygon bbox = getBBox(e);
    a.setBbox(bbox);

    if (bbox == null) {
        bbox = new Polygon();
        a.setBbox(bbox);

        if (a instanceof SpriteActor)
            ((SpriteActor) a).setBboxFromRenderer(true);
    }

    Vector2 pos = getPos(e);
    if (pos != null)
        a.setPosition(pos.x, pos.y);

    a.setDesc(e.getAttribute(XMLConstants.DESC_ATTR));

    if (a instanceof SpriteActor) {
        ActorRenderer r = ((SpriteActor) a).getRenderer();

        NodeList faList = getAnimations(e);

        for (int i = 0; i < faList.getLength(); i++) {
            Element faElement = (Element) faList.item(i);

            AnimationDesc fa = getEngineFA(type, faElement);

            r.addAnimation(fa);
        }

        if (!e.getAttribute(XMLConstants.INIT_ANIMATION_ATTR).isEmpty()) {
            ((SpriteActor) a).getRenderer().setInitAnimation(e.getAttribute(XMLConstants.INIT_ANIMATION_ATTR));

        }

        if (!e.getAttribute(XMLConstants.SCALE_ATTR).isEmpty()) {
            ((SpriteActor) a).setScale(Float.parseFloat(e.getAttribute(XMLConstants.SCALE_ATTR)));
        }

        // PARSE DEPTH TYPE
        String depthType = e.getAttribute(XMLConstants.DEPTH_TYPE_ATTR);
        ((SpriteActor) a).setDepthType(DepthType.NONE);

        if (!depthType.isEmpty()) {
            if (depthType.equals(XMLConstants.VECTOR_ATTR))
                ((SpriteActor) a).setDepthType(DepthType.VECTOR);
        }
    }

    if (e.getAttribute(XMLConstants.OBSTACLE_ATTR).equals(XMLConstants.TRUE_VALUE))
        a.setWalkObstacle(true);

    if (!e.getAttribute(XMLConstants.ZINDEX_ATTR).isEmpty()) {
        a.setZIndex(Float.parseFloat(e.getAttribute(XMLConstants.ZINDEX_ATTR)));
    }

    return a;
}

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

License:Apache License

public void setBbox(Element e, Polygon p) {
    if (p == null) {
        p = new Polygon();

        float[] verts = new float[8];

        verts[0] = 0f;/*w w  w  .  j  a  v  a 2s .  co  m*/
        verts[1] = 0f;
        verts[2] = 0f;
        verts[3] = 200;
        verts[4] = 200;
        verts[5] = 200;
        verts[6] = 200;
        verts[7] = 0f;

        p.setVertices(verts);
    }

    e.setAttribute(XMLConstants.BBOX_ATTR, Param.toStringParam(p));

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

From source file:com.mobidevelop.maps.basic.objects.BasicPolygonMapObject.java

License:Apache License

public BasicPolygonMapObject(MapLayer layer) {
    super(layer);
    this.polygon = new Polygon();
}

From source file:com.mobidevelop.maps.basic.objects.BasicPolygonMapObject.java

License:Apache License

public BasicPolygonMapObject(MapLayer layer, float x, float y) {
    super(layer, x, y);
    this.polygon = new Polygon();
}