Example usage for com.badlogic.gdx.utils FloatArray set

List of usage examples for com.badlogic.gdx.utils FloatArray set

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils FloatArray set.

Prototype

public void set(int index, float value) 

Source Link

Usage

From source file:com.stercore.code.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) {
    if ((mapObject = listener.createFixture(mapObject)) == null)
        return null;

    String orientation = findProperty(aliases.orientation, aliases.orthogonal, heritage, mapProperties,
            layerProperties, mapObject.getProperties());
    transform(mat4, orientation);/*www .  j  av  a2s  .c  om*/

    Shape shape = null;
    if (mapObject instanceof RectangleMapObject) {
        Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle();
        vec3.set(rectangle.x, rectangle.y, 0);
        vec3.mul(mat4);
        float x = vec3.x, y = vec3.y, width, height;
        if (!orientation.equals(aliases.staggered)) {
            vec3.set(rectangle.width, rectangle.height, 0).mul(mat4);
            width = vec3.x;
            height = vec3.y;
        } else {
            width = rectangle.width * unitScale;
            height = rectangle.height * unitScale;
        }
        ((PolygonShape) (shape = new PolygonShape())).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 || mapObject instanceof PolylineMapObject) {
        FloatArray vertices = Pools.obtain(FloatArray.class);
        vertices.clear();
        vertices.addAll(mapObject instanceof PolygonMapObject
                ? ((PolygonMapObject) mapObject).getPolygon().getTransformedVertices()
                : ((PolylineMapObject) mapObject).getPolyline().getTransformedVertices());
        for (int ix = 0, iy = 1; iy < vertices.size; ix += 2, iy += 2) {
            vec3.set(vertices.get(ix), vertices.get(iy), 0);
            vec3.mul(mat4);
            vertices.set(ix, vec3.x - body.getPosition().x);
            vertices.set(iy, vec3.y - body.getPosition().y);
        }
        if (mapObject instanceof PolygonMapObject)
            ((PolygonShape) (shape = new PolygonShape())).set(vertices.items, 0, vertices.size);
        else if (vertices.size == 4)
            ((EdgeShape) (shape = new EdgeShape())).set(vertices.get(0), vertices.get(1), vertices.get(2),
                    vertices.get(3));
        else {
            vertices.shrink();
            ((ChainShape) (shape = new ChainShape())).createChain(vertices.items);
        }
        Pools.free(vertices);
    } else if (mapObject instanceof CircleMapObject || mapObject instanceof EllipseMapObject) {
        if (mapObject instanceof CircleMapObject) {
            Circle circle = ((CircleMapObject) mapObject).getCircle();
            vec3.set(circle.x, circle.y, circle.radius);
        } else {
            Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse();
            if (ellipse.width != ellipse.height)
                throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because "
                        + ClassReflection.getSimpleName(mapObject.getClass())
                        + "s that are not circles are not supported");
            vec3.set(ellipse.x + ellipse.width / 2, ellipse.y + ellipse.height / 2, ellipse.width / 2);
        }
        vec3.mul(mat4);
        vec3.sub(body.getPosition().x, body.getPosition().y, 0);
        CircleShape circleShape = (CircleShape) (shape = new CircleShape());
        circleShape.setPosition(vec2.set(vec3.x, vec3.y));
        circleShape.setRadius(vec3.z);
    } else if (mapObject instanceof TextureMapObject)
        throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because "
                + ClassReflection.getSimpleName(mapObject.getClass()) + "s are not supported");
    else
        assert false : mapObject + " is a not known subclass of " + MapObject.class.getName();

    MapProperties properties = mapObject.getProperties();

    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(findProperty(aliases.userData, fixture.getUserData(), heritage, mapProperties,
            layerProperties, properties));

    shape.dispose();

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

    return fixture;
}

From source file:de.fhkoeln.game.utils.Box2DMapObjectParser.java

License:Apache License

/** creates a {@link com.badlogic.gdx.physics.box2d.Fixture} from a {@link com.badlogic.gdx.maps.MapObject}
 *  @param mapObject the {@link com.badlogic.gdx.maps.MapObject} to parse
 *  @param body the {@link com.badlogic.gdx.physics.box2d.Body} to create the {@link com.badlogic.gdx.physics.box2d.Fixture Fixtures} on
 *  @return the parsed {@link com.badlogic.gdx.physics.box2d.Fixture} */
public Fixture createFixture(MapObject mapObject, Body body) {
    if ((mapObject = listener.createFixture(mapObject)) == null)
        return null;

    String orientation = findProperty(aliases.orientation, aliases.orthogonal, heritage, mapProperties,
            layerProperties, mapObject.getProperties());
    transform(mat4, orientation);/*  w  ww.  jav  a2  s  .  c  o  m*/

    Shape shape = null;
    if (mapObject instanceof RectangleMapObject) {
        Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle();
        vec3.set(rectangle.x, rectangle.y, 0);
        vec3.mul(mat4);
        float x = vec3.x, y = vec3.y, width, height;
        if (!orientation.equals(aliases.staggered)) {
            vec3.set(rectangle.width, rectangle.height, 0).mul(mat4);
            width = vec3.x;
            height = vec3.y;
        } else {
            width = rectangle.width * unitScale;
            height = rectangle.height * unitScale;
        }
        ((PolygonShape) (shape = new PolygonShape())).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 || mapObject instanceof PolylineMapObject) {
        FloatArray vertices = Pools.obtain(FloatArray.class);
        vertices.clear();
        vertices.addAll(mapObject instanceof PolygonMapObject
                ? ((PolygonMapObject) mapObject).getPolygon().getTransformedVertices()
                : ((PolylineMapObject) mapObject).getPolyline().getTransformedVertices());
        for (int ix = 0, iy = 1; iy < vertices.size; ix += 2, iy += 2) {
            vec3.set(vertices.get(ix), vertices.get(iy), 0);
            vec3.mul(mat4);
            vertices.set(ix, vec3.x - body.getPosition().x);
            vertices.set(iy, vec3.y - body.getPosition().y);
        }
        if (mapObject instanceof PolygonMapObject)
            ((PolygonShape) (shape = new PolygonShape())).set(vertices.items, 0, vertices.size);
        else if (vertices.size == 4)
            ((EdgeShape) (shape = new EdgeShape())).set(vertices.get(0), vertices.get(1), vertices.get(2),
                    vertices.get(3));
        else {
            vertices.shrink();
            ((ChainShape) (shape = new ChainShape())).createChain(vertices.items);
        }
        Pools.free(vertices);
    } else if (mapObject instanceof CircleMapObject || mapObject instanceof EllipseMapObject) {
        if (mapObject instanceof CircleMapObject) {
            Circle circle = ((CircleMapObject) mapObject).getCircle();
            vec3.set(circle.x, circle.y, circle.radius);
        } else {
            Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse();
            if (ellipse.width != ellipse.height)
                throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because "
                        + mapObject.getClass().getSimpleName() + "s that are not circles are not supported");
            vec3.set(ellipse.x + ellipse.width / 2, ellipse.y + ellipse.height / 2, ellipse.width / 2);
        }
        vec3.mul(mat4);
        vec3.sub(body.getPosition().x, body.getPosition().y, 0);
        CircleShape circleShape = (CircleShape) (shape = new CircleShape());
        circleShape.setPosition(vec2.set(vec3.x, vec3.y));
        circleShape.setRadius(vec3.z);
    } 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();

    MapProperties properties = mapObject.getProperties();

    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(findProperty(aliases.userData, fixture.getUserData(), heritage, mapProperties,
            layerProperties, properties));

    shape.dispose();

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

    return fixture;
}