Example usage for com.badlogic.gdx.math Polyline getTransformedVertices

List of usage examples for com.badlogic.gdx.math Polyline getTransformedVertices

Introduction

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

Prototype

public float[] getTransformedVertices() 

Source Link

Document

Returns vertices scaled, rotated, and offset by the polygon position.

Usage

From source file:com.agateau.pixelwheels.map.LapPositionTableIO.java

License:Open Source License

public static LapPositionTable load(TiledMap map) {
    MapLayer layer = map.getLayers().get("Sections");
    Assert.check(layer != null, "No 'Sections' layer found");
    MapObjects objects = layer.getObjects();

    Array<Line> lines = new Array<Line>();
    lines.ensureCapacity(objects.getCount());
    Set<String> names = new HashSet<String>();
    for (MapObject obj : objects) {
        String name = obj.getName();
        Assert.check(!name.isEmpty(), "Section line is missing a name");
        Assert.check(!names.contains(name), "Duplicate section line " + name);
        names.add(name);//from w  w w.  jav a 2 s  . c  om

        float order;
        try {
            order = Float.parseFloat(name);
        } catch (NumberFormatException e) {
            throw new RuntimeException("Invalid section name " + name);
        }
        Assert.check(obj instanceof PolylineMapObject,
                "'Sections' layer should only contain PolylineMapObjects");
        Polyline polyline = ((PolylineMapObject) obj).getPolyline();
        float[] vertices = polyline.getTransformedVertices();
        Assert.check(vertices.length == 4,
                "Polyline with name " + order + "should have 2 points, not " + (vertices.length / 2));
        Line line = new Line();
        line.x1 = vertices[0];
        line.y1 = vertices[1];
        line.x2 = vertices[2];
        line.y2 = vertices[3];
        line.order = order;
        lines.add(line);
    }
    lines.sort();

    LapPositionTable table = new LapPositionTable();
    for (int idx = 0; idx < lines.size; ++idx) {
        Line line1 = lines.get(idx);
        Line line2 = lines.get((idx + 1) % lines.size);
        float[] vertices = { line1.x1, line1.y1, line2.x1, line2.y1, line2.x2, line2.y2, line1.x2, line1.y2 };
        Polygon polygon = new Polygon(vertices);
        table.addSection(idx, polygon);
    }
    return table;
}

From source file:com.cafeitvn.myballgame.screen.Box2DMapObjectParser.java

License:Apache License

/**
 * creates a {@link Fixture} from a {@link MapObject}
 * @param mapObject the {@link MapObject} which to parse
 * @return the parsed {@link Fixture}//from   ww  w . ja  v a2  s.  com
 */
public Fixture createFixture(MapObject mapObject) {
    MapProperties properties = mapObject.getProperties();

    String type = properties.get("type", String.class);

    Body body = bodies.get(
            type.equals(aliases.object) ? mapObject.getName() : properties.get(aliases.body, String.class));

    if (!type.equals(aliases.fixture) && !type.equals(aliases.object))
        throw new IllegalArgumentException("type of " + mapObject + " is  \"" + type + "\" instead of \""
                + aliases.fixture + "\" or \"" + aliases.object + "\"");

    FixtureDef fixtureDef = new FixtureDef();
    Shape shape = null;

    if (mapObject instanceof RectangleMapObject) {
        shape = new PolygonShape();
        Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle();
        rectangle.x *= unitScale;
        rectangle.y *= unitScale;
        rectangle.width *= unitScale;
        rectangle.height *= unitScale;
        ((PolygonShape) shape).setAsBox(rectangle.width / 2, rectangle.height / 2,
                new Vector2(rectangle.x - body.getPosition().x + rectangle.width / 2,
                        rectangle.y - body.getPosition().y + rectangle.height / 2),
                body.getAngle());
    } else if (mapObject instanceof PolygonMapObject) {
        shape = new PolygonShape();
        Polygon polygon = ((PolygonMapObject) mapObject).getPolygon();
        polygon.setPosition(polygon.getX() * unitScale - body.getPosition().x,
                polygon.getY() * unitScale - body.getPosition().y);
        polygon.setScale(unitScale, unitScale);
        ((PolygonShape) shape).set(polygon.getTransformedVertices());
    } else if (mapObject instanceof PolylineMapObject) {
        shape = new ChainShape();
        Polyline polyline = ((PolylineMapObject) mapObject).getPolyline();
        polyline.setPosition(polyline.getX() * unitScale - body.getPosition().x,
                polyline.getY() * unitScale - body.getPosition().y);
        polyline.setScale(unitScale, unitScale);
        ((ChainShape) shape).createChain(polyline.getTransformedVertices());
    } else if (mapObject instanceof CircleMapObject) {
        shape = new CircleShape();
        Circle circle = ((CircleMapObject) mapObject).getCircle();
        circle.setPosition(circle.x * unitScale - body.getPosition().x,
                circle.y * unitScale - body.getPosition().y);
        circle.radius *= unitScale;
        ((CircleShape) shape).setPosition(new Vector2(circle.x, circle.y));
        ((CircleShape) shape).setRadius(circle.radius);
    } else if (mapObject instanceof EllipseMapObject) {
        Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse();

        if (ellipse.width == ellipse.height) {
            CircleMapObject circleMapObject = new CircleMapObject(ellipse.x, ellipse.y, ellipse.width / 2);
            circleMapObject.setName(mapObject.getName());
            circleMapObject.getProperties().putAll(mapObject.getProperties());
            circleMapObject.setColor(mapObject.getColor());
            circleMapObject.setVisible(mapObject.isVisible());
            circleMapObject.setOpacity(mapObject.getOpacity());
            return createFixture(circleMapObject);
        }

        IllegalArgumentException exception = new IllegalArgumentException(
                "Cannot parse " + mapObject.getName() + " because " + mapObject.getClass().getSimpleName()
                        + "s that are not circles are not supported");
        Gdx.app.error(getClass().getSimpleName(), exception.getMessage(), exception);
        throw exception;
    } else if (mapObject instanceof TextureMapObject) {
        IllegalArgumentException exception = new IllegalArgumentException("Cannot parse " + mapObject.getName()
                + " because " + mapObject.getClass().getSimpleName() + "s are not supported");
        Gdx.app.error(getClass().getSimpleName(), exception.getMessage(), exception);
        throw exception;
    } else
        assert false : mapObject + " is a not known subclass of " + MapObject.class.getName();

    fixtureDef.shape = shape;
    fixtureDef.density = (Float) getProperty(properties, aliases.density, fixtureDef.density, Float.class);
    fixtureDef.filter.categoryBits = (Short) getProperty(properties, aliases.categoryBits,
            fixtureDef.filter.categoryBits, Short.class);
    fixtureDef.filter.groupIndex = (Short) getProperty(properties, aliases.groupIndex,
            fixtureDef.filter.groupIndex, Short.class);
    fixtureDef.filter.maskBits = (Short) getProperty(properties, aliases.maskBits, fixtureDef.filter.maskBits,
            Short.class);
    fixtureDef.friction = (Float) getProperty(properties, aliases.friciton, fixtureDef.friction, Float.class);
    fixtureDef.isSensor = (Boolean) getProperty(properties, aliases.isSensor, fixtureDef.isSensor,
            Boolean.class);
    fixtureDef.restitution = (Float) getProperty(properties, aliases.restitution, fixtureDef.restitution,
            Float.class);

    Fixture fixture = body.createFixture(fixtureDef);

    shape.dispose();

    String name = mapObject.getName();
    if (fixtures.containsKey(name)) {
        int duplicate = 1;
        while (fixtures.containsKey(name + duplicate))
            duplicate++;
        name += duplicate;
    }

    fixtures.put(name, fixture);

    return fixture;
}

From source file:com.rubentxu.juegos.core.utils.dermetfan.box2d.Box2DMapObjectParser.java

License:Apache License

/**
 * creates a {@link Fixture} from a {@link MapObject}
 *
 * @param mapObject the {@link MapObject} to parse
 * @return the parsed {@link Fixture}/*ww  w .  j  av  a 2s  .  co m*/
 */
public Fixture createFixture(MapObject mapObject) {
    MapProperties properties = mapObject.getProperties();

    String type = properties.get("type", String.class);

    Body body = bodies.get(
            type.equals(aliases.object) ? mapObject.getName() : properties.get(aliases.body, String.class));

    if (!type.equals(aliases.fixture) && !type.equals(aliases.object))
        throw new IllegalArgumentException("type of " + mapObject + " is  \"" + type + "\" instead of \""
                + aliases.fixture + "\" or \"" + aliases.object + "\"");

    FixtureDef fixtureDef = new FixtureDef();
    Shape shape = null;

    if (mapObject instanceof RectangleMapObject) {
        shape = new PolygonShape();
        Rectangle rectangle = new Rectangle(((RectangleMapObject) mapObject).getRectangle());
        rectangle.x *= unitScale;
        rectangle.y *= unitScale;
        rectangle.width *= unitScale;
        rectangle.height *= unitScale;
        ((PolygonShape) shape).setAsBox(rectangle.width / 2, rectangle.height / 2,
                new Vector2(rectangle.x - body.getPosition().x + rectangle.width / 2,
                        rectangle.y - body.getPosition().y + rectangle.height / 2),
                body.getAngle());
    } else if (mapObject instanceof PolygonMapObject) {
        shape = new PolygonShape();
        Polygon polygon = ((PolygonMapObject) mapObject).getPolygon();
        polygon.setPosition(polygon.getX() * unitScale - body.getPosition().x,
                polygon.getY() * unitScale - body.getPosition().y);
        polygon.setScale(unitScale, unitScale);
        ((PolygonShape) shape).set(polygon.getTransformedVertices());
    } else if (mapObject instanceof PolylineMapObject) {
        shape = new ChainShape();
        Polyline polyline = ((PolylineMapObject) mapObject).getPolyline();
        polyline.setPosition(polyline.getX() * unitScale - body.getPosition().x,
                polyline.getY() * unitScale - body.getPosition().y);
        polyline.setScale(unitScale, unitScale);
        float[] vertices = polyline.getTransformedVertices();
        Vector2[] vectores = new Vector2[vertices.length / 2];
        for (int i = 0, j = 0; i < vertices.length; i += 2, j++) {
            vectores[j].x = vertices[i];
            vectores[j].y = vertices[i + 1];
        }
        ((ChainShape) shape).createChain(vectores);
    } else if (mapObject instanceof CircleMapObject) {
        shape = new CircleShape();
        Circle circle = ((CircleMapObject) mapObject).getCircle();
        circle.setPosition(circle.x * unitScale - body.getPosition().x,
                circle.y * unitScale - body.getPosition().y);
        circle.radius *= unitScale;
        ((CircleShape) shape).setPosition(new Vector2(circle.x, circle.y));
        ((CircleShape) shape).setRadius(circle.radius);
    } else if (mapObject instanceof EllipseMapObject) {
        Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse();

        /*
        b2ChainShape* chain = (b2ChainShape*)addr;
        b2Vec2* verticesOut = new b2Vec2[numVertices];
        for( int i = 0; i < numVertices; i++ )
        verticesOut[i] = b2Vec2(verts[i<<1], verts[(i<<1)+1]);
        chain->CreateChain( verticesOut, numVertices );
        delete verticesOut;
        */

        if (ellipse.width == ellipse.height) {
            CircleMapObject circleMapObject = new CircleMapObject(ellipse.x, ellipse.y, ellipse.width / 2);
            circleMapObject.setName(mapObject.getName());
            circleMapObject.getProperties().putAll(mapObject.getProperties());
            circleMapObject.setColor(mapObject.getColor());
            circleMapObject.setVisible(mapObject.isVisible());
            circleMapObject.setOpacity(mapObject.getOpacity());
            return createFixture(circleMapObject);
        }

        IllegalArgumentException exception = new IllegalArgumentException(
                "Cannot parse " + mapObject.getName() + " because  that are not circles are not supported");
        Gdx.app.error(getClass().getName(), exception.getMessage(), exception);
        throw exception;
    } else if (mapObject instanceof TextureMapObject) {
        IllegalArgumentException exception = new IllegalArgumentException(
                "Cannot parse " + mapObject.getName() + " because s are not supported");
        Gdx.app.error(getClass().getName(), exception.getMessage(), exception);
        throw exception;
    } else
        assert false : mapObject + " is a not known subclass of " + MapObject.class.getName();

    fixtureDef.shape = shape;
    fixtureDef.density = getProperty(properties, aliases.density, fixtureDef.density);
    fixtureDef.filter.categoryBits = getProperty(properties, aliases.categoryBits, GRUPO.STATIC.getCategory());
    fixtureDef.filter.groupIndex = getProperty(properties, aliases.groupIndex, fixtureDef.filter.groupIndex);
    fixtureDef.filter.maskBits = getProperty(properties, aliases.maskBits, Box2DPhysicsObject.MASK_STATIC);
    fixtureDef.friction = getProperty(properties, aliases.friciton, fixtureDef.friction);
    fixtureDef.isSensor = getProperty(properties, aliases.isSensor, fixtureDef.isSensor);
    fixtureDef.restitution = getProperty(properties, aliases.restitution, fixtureDef.restitution);

    Fixture fixture = body.createFixture(fixtureDef);
    fixture.setUserData(body.getUserData());
    shape.dispose();

    String name = mapObject.getName();
    if (fixtures.containsKey(name)) {
        int duplicate = 1;
        while (fixtures.containsKey(name + duplicate))
            duplicate++;
        name += duplicate;
    }

    fixtures.put(name, fixture);

    return fixture;
}

From source file:headmade.arttag.utils.MapUtils.java

License:Apache License

public static void loadMap(ArtTagScreen artTagScreen, String mapName) {
    if (artTagScreen.currentRoom != null) {
        unloadMap(artTagScreen);/*  w  w w.java2s  . co  m*/
    }

    if (null != Player.instance.warpDirection) {
        Gdx.app.log(TAG, "Loading map " + mapName);
        if (DIRECTION_LEFT.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexX == 0) {
                artTagScreen.currentRoomIndexX = ArtTagScreen.MAX_ROOM_SIZE - 1;
            } else {
                artTagScreen.currentRoomIndexX--;
            }
        } else if (DIRECTION_RIGHT.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexX == ArtTagScreen.MAX_ROOM_SIZE - 1) {
                artTagScreen.currentRoomIndexX = 0;
            } else {
                artTagScreen.currentRoomIndexX++;
            }
        } else if (DIRECTION_TOP.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexY == ArtTagScreen.MAX_ROOM_SIZE - 1) {
                artTagScreen.currentRoomIndexY = 0;
            } else {
                artTagScreen.currentRoomIndexY++;
            }
        } else if (DIRECTION_BOTTOM.equalsIgnoreCase(Player.instance.warpDirection)) {
            if (artTagScreen.currentRoomIndexY == 0) {
                artTagScreen.currentRoomIndexY = ArtTagScreen.MAX_ROOM_SIZE - 1;
            } else {
                artTagScreen.currentRoomIndexY--;
            }
        }

    }

    boolean isNewRoom = true;
    if (artTagScreen.rooms[artTagScreen.currentRoomIndexX][artTagScreen.currentRoomIndexY] == null) {
        artTagScreen.currentRoom = new Room(mapName);
        artTagScreen.rooms[artTagScreen.currentRoomIndexX][artTagScreen.currentRoomIndexY] = artTagScreen.currentRoom;
    } else {
        artTagScreen.currentRoom = artTagScreen.rooms[artTagScreen.currentRoomIndexX][artTagScreen.currentRoomIndexY];
        isNewRoom = false;
    }

    final Box2DMapObjectParser parser = new Box2DMapObjectParser(ArtTag.UNIT_SCALE);
    // final Box2DMapObjectParser.Listener.Adapter listener = new Box2DMapObjectParser.Listener.Adapter() {
    //
    // @Override
    // public void created(Fixture fixture, MapObject mapObject) {
    //
    // Gdx.app.log(TAG, "mapObject.getProperties()" + fixture.getFilterData().maskBits);
    // super.created(fixture, mapObject);
    // }
    //
    // };
    // parser.setListener(listener);

    final TiledMap map = artTagScreen.currentRoom.getMap();

    final String onGameOver = map.getProperties().get(PROP_ONGAMEOVER, String.class);
    final String hideJobDesc = map.getProperties().get(PROP_ONGAMEOVER, String.class);
    artTagScreen.onGameOver = onGameOver;
    artTagScreen.isHideJobDesc = hideJobDesc == null ? false : true;

    parser.load(artTagScreen.world, map);
    if (null == artTagScreen.mapRenderer) {
        // artTagScreen.mapRenderer = new OrthogonalTiledMapRenderer(artTagScreen.map, artTagScreen.getGame().getBatch());
        artTagScreen.mapRenderer = new OrthogonalTiledMapRenderer(map, parser.getUnitScale(),
                artTagScreen.getGame().getBatch());
    } else {
        artTagScreen.mapRenderer.setMap(map);
    }

    MapLayer layer = map.getLayers().get("objects");
    for (final MapObject mapObject : layer.getObjects()) {
        if (OBJ_ART.equals(mapObject.getName())) {
            if (isNewRoom) {
                // add Art only if this a new room. If this is an old room the art was created before.
                if (mapObject instanceof RectangleMapObject) {
                    createNewArt(artTagScreen, mapObject, ((RectangleMapObject) mapObject).getRectangle(),
                            parser.getUnitScale());
                } else {
                    Gdx.app.error(TAG, OBJ_ART + " has to be a Rectangle");
                }
            }
        } else if (OBJ_WARP.equals(mapObject.getName())) {
            final Body warp = createWarp(artTagScreen, ((RectangleMapObject) mapObject).getRectangle(),
                    parser.getUnitScale());
            final String direction = mapObject.getProperties().get(PROP_DIRECTION, String.class);
            final String room = mapObject.getProperties().get(PROP_ROOM, String.class);
            warp.setUserData(new WarpVo(direction, room));
        } else if (OBJ_EXIT.equals(mapObject.getName())) {
            createExit(artTagScreen, ((RectangleMapObject) mapObject).getRectangle(), parser.getUnitScale());
        } else if (OBJ_HINT.equals(mapObject.getName())) {
            final Body hintBody = createHint(artTagScreen, ((RectangleMapObject) mapObject).getRectangle(),
                    parser.getUnitScale());
            final String hint = mapObject.getProperties().get(PROP_TEXT, String.class);
            hintBody.setUserData(hint);
        } else if (OBJ_PLAYER.equals(mapObject.getName())) {
            final Ellipse e = ((EllipseMapObject) mapObject).getEllipse();
            if (null == Player.instance.body) {
                final String direction = mapObject.getProperties().get("direction", String.class);
                if (null != Player.instance.warpDirection) {
                    if (Player.instance.warpDirection.equalsIgnoreCase(direction)) {
                        Player.instance.createBody(artTagScreen, (e.x + e.width / 2f) * parser.getUnitScale(),
                                (e.y + e.height / 2f) * parser.getUnitScale());
                        Player.instance.warpDirection = null;
                    }
                } else if (direction == null) {
                    Player.instance.createBody(artTagScreen, (e.x + e.width / 2f) * parser.getUnitScale(),
                            (e.y + e.height / 2f) * parser.getUnitScale());
                }
            }
        }
    }

    if (!isNewRoom) {
        // create ArtSensors for old Art
        for (final Art art : artTagScreen.currentRoom.getArtList()) {
            createArtSensor(artTagScreen, art);
        }
    }

    { // guards
        final HashMap<String, Guard> guards = new HashMap<String, Guard>();
        final Array<MapObject> paths = new Array<MapObject>();
        layer = map.getLayers().get("guards");
        for (final MapObject mapObject : layer.getObjects()) {
            if (mapObject.getName() != null && mapObject.getName().contains(OBJ_GUARD)) {
                final Ellipse e = ((EllipseMapObject) mapObject).getEllipse();
                final Guard g = new Guard();
                g.createBody(artTagScreen, (e.x + e.width / 2f) * parser.getUnitScale(),
                        (e.y + e.height / 2f) * parser.getUnitScale());
                guards.put(mapObject.getName().trim(), g);
            } else if (mapObject.getName() != null && mapObject.getName().contains(OBJ_PATH)) {
                paths.add(mapObject);
            } else {
                Gdx.app.log(TAG, "WTF");
            }
        }
        for (final MapObject mapObject : paths) {
            final PolylineMapObject pl = (PolylineMapObject) mapObject;
            final String ownerName = pl.getProperties().get("owner", String.class);
            final Guard g = guards.get(ownerName.trim());
            Gdx.app.log(TAG, "guards " + guards.keySet());
            if (g != null) {
                final Polyline orgPolyline = pl.getPolyline();
                final Polyline p = new Polyline(orgPolyline.getVertices());
                p.setScale(parser.getUnitScale(), parser.getUnitScale());
                p.setPosition(orgPolyline.getX() * parser.getUnitScale(),
                        orgPolyline.getY() * parser.getUnitScale());
                final float[] vertices = p.getTransformedVertices();
                for (int i = 0; i < vertices.length; i += 2) {
                    g.path.add(new Vector2(vertices[i], vertices[i + 1]));
                }
                artTagScreen.guards.add(g);
            } else {
                Gdx.app.log(TAG, "No guard for path " + ownerName);
            }
        }

    }

    layer = map.getLayers().get("lights");
    for (final MapObject mapObject : layer.getObjects()) {
        if (mapObject.getProperties().get("type", String.class).contains(LIGTH_POINT)) {
            if (mapObject instanceof EllipseMapObject) {
                createPointLight(artTagScreen, (EllipseMapObject) mapObject, parser.getUnitScale());
            } else {
                Gdx.app.error(TAG, LIGTH_POINT + " light has to be a Circle not " + mapObject);
            }
        } else if (mapObject.getProperties().get("type", String.class).contains(LIGTH_CONE)) {
            if (mapObject instanceof PolygonMapObject) {
                createConeLight(artTagScreen, (PolygonMapObject) mapObject, parser.getUnitScale());
            } else {
                Gdx.app.error(TAG, LIGTH_CONE + " light has to be a Polygon");
            }
        }
    }

}

From source file:net.dermetfan.utils.libgdx.box2d.Box2DMapObjectParser.java

License:Apache License

/** creates a {@link Fixture} from a {@link MapObject}
 *  @param mapObject the {@link MapObject} to parse
 *  @param body the {@link Body} to create the {@link Fixture Fixtures} on
 *  @return the parsed {@link Fixture} */
public Fixture createFixture(MapObject mapObject, Body body) {
    MapProperties properties = mapObject.getProperties();

    Shape shape = null;/* w  ww. ja v  a2  s  .c  o m*/
    if (mapObject instanceof RectangleMapObject) {
        shape = new PolygonShape();
        Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle();
        float x = rectangle.x * unitScale, y = rectangle.y * unitScale, width = rectangle.width * unitScale,
                height = rectangle.height * unitScale;
        ((PolygonShape) shape).setAsBox(width / 2, height / 2,
                vec2.set(x - body.getPosition().x + width / 2, y - body.getPosition().y + height / 2),
                body.getAngle());
    } else if (mapObject instanceof PolygonMapObject) {
        shape = new PolygonShape();
        Polygon polygon = new Polygon(((PolygonMapObject) mapObject).getPolygon().getTransformedVertices());
        polygon.setScale(unitScale, unitScale);
        polygon.setPosition(-body.getPosition().x, -body.getPosition().y);
        ((PolygonShape) shape).set(polygon.getTransformedVertices());
    } else if (mapObject instanceof PolylineMapObject) {
        shape = new ChainShape();
        Polyline polyline = new Polyline(
                ((PolylineMapObject) mapObject).getPolyline().getTransformedVertices());
        polyline.setPosition(polyline.getX() * unitScale - body.getPosition().x,
                polyline.getY() * unitScale - body.getPosition().y);
        polyline.setScale(unitScale, unitScale);
        ((ChainShape) shape).createChain(polyline.getTransformedVertices());
    } else if (mapObject instanceof CircleMapObject) {
        shape = new CircleShape();
        Circle mapObjectCircle = ((CircleMapObject) mapObject).getCircle();
        Circle circle = new Circle(mapObjectCircle.x, mapObjectCircle.y, mapObjectCircle.radius);
        circle.setPosition(circle.x * unitScale - body.getPosition().x,
                circle.y * unitScale - body.getPosition().y);
        circle.radius *= unitScale;
        CircleShape circleShape = (CircleShape) shape;
        circleShape.setPosition(vec2.set(circle.x, circle.y));
        circleShape.setRadius(circle.radius);
    } else if (mapObject instanceof EllipseMapObject) {
        Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse();

        if (ellipse.width == ellipse.height) {
            CircleMapObject circleMapObject = new CircleMapObject(ellipse.x + ellipse.width / 2,
                    ellipse.y + ellipse.height / 2, ellipse.width / 2);
            circleMapObject.setName(mapObject.getName());
            circleMapObject.getProperties().putAll(mapObject.getProperties());
            circleMapObject.setColor(mapObject.getColor());
            circleMapObject.setVisible(mapObject.isVisible());
            circleMapObject.setOpacity(mapObject.getOpacity());
            return createFixture(circleMapObject, body);
        }

        throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because "
                + mapObject.getClass().getSimpleName() + "s that are not circles are not supported");
    } else if (mapObject instanceof TextureMapObject)
        throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because "
                + mapObject.getClass().getSimpleName() + "s are not supported");
    else
        assert false : mapObject + " is a not known subclass of " + MapObject.class.getName();

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    assignProperties(fixtureDef, heritage);
    assignProperties(fixtureDef, mapProperties);
    assignProperties(fixtureDef, layerProperties);
    assignProperties(fixtureDef, properties);

    Fixture fixture = body.createFixture(fixtureDef);
    fixture.setUserData(getProperty(layerProperties, aliases.userData, fixture.getUserData()));
    fixture.setUserData(getProperty(properties, aliases.userData, fixture.getUserData()));

    shape.dispose();

    fixtures.put(findAvailableName(mapObject.getName(), fixtures), fixture);

    return fixture;
}

From source file:releasethekraken.path.SeaCreaturePath.java

/**
 * Constructs a new SeaCreaturePath//w w w .ja  v  a2  s . c  om
 * @param id The path's ID, from the level editor
 * @param parent The path that leads to this path, or null if this is the first path
 * @param nextPaths A list of paths that can be gone on after this path.  The list is empty if there are none.
 * @param polyline The Polyline object that holds the path data
 */
public SeaCreaturePath(int id, SeaCreaturePath parent, Array<SeaCreaturePath> nextPaths, Polyline polyline) {
    this.id = id;
    this.parent = parent;
    this.nextPaths = nextPaths;

    final float scale = 1 / 16F; //Scale it down to match the world units

    //Scale the polyline to match the world units.
    //This has to be done manually because Polyline.setScale() doesn't seem to work.
    float[] vertices = polyline.getTransformedVertices();
    for (int i = 0; i < vertices.length; i++)
        vertices[i] *= scale;
    polyline = new Polyline(vertices);

    this.polyline = polyline;

    vertices = polyline.getVertices(); //Get an array of vertices, in x1, y1, x2, y2,... format
    Vector2[] points = new Vector2[vertices.length / 2]; //Make an array of Vector2 objects

    //Convert the vertices to Vector2 objects
    for (int i = 0; i < points.length; i++) {
        float x = vertices[i * 2];
        float y = vertices[(i * 2) + 1];
        points[i] = new Vector2(x, y);
    }

    //TODO: Fix this!
    //Add additional starting and ending points so that all of the original points are used, due to how catmull rom spline works
    Array<Vector2> pointsList = new Array<Vector2>(points);
    Vector2 firstPoint = points[0].cpy().add(points[0].cpy().sub(points[1]));
    pointsList.insert(0, firstPoint);
    Vector2 lastPoint = points[points.length - 1].cpy()
            .add(points[points.length - 1].cpy().sub(points[points.length - 2]));
    pointsList.insert(pointsList.size, lastPoint);

    //Make the smooth path with the points
    this.path = new CatmullRomSpline<Vector2>(pointsList.toArray(), false);

    Gdx.app.log("SCPath " + this.toString(true), "Points: " + Arrays.toString(points));
}