Example usage for com.badlogic.gdx.physics.box2d ChainShape getVertexCount

List of usage examples for com.badlogic.gdx.physics.box2d ChainShape getVertexCount

Introduction

In this page you can find the example usage for com.badlogic.gdx.physics.box2d ChainShape getVertexCount.

Prototype

public int getVertexCount() 

Source Link

Usage

From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Box2DUtils.java

License:Apache License

/** @param shape the Shape which vertices to get (for circles, the bounding box vertices will be returned)
 *  @return the vertices of the given Shape*/
private static Vector2[] vertices0(Shape shape) {
    Vector2[] vertices;/*from  w ww . j a  v  a  2s.co  m*/
    switch (shape.getType()) {
    case Polygon:
        PolygonShape polygonShape = (PolygonShape) shape;
        vertices = new Vector2[polygonShape.getVertexCount()];
        for (int i = 0; i < vertices.length; i++) {
            vertices[i] = new Vector2();
            polygonShape.getVertex(i, vertices[i]);
        }
        break;
    case Edge:
        EdgeShape edgeShape = (EdgeShape) shape;
        edgeShape.getVertex1(vec2_0);
        edgeShape.getVertex2(vec2_1);
        vertices = new Vector2[] { new Vector2(vec2_0), new Vector2(vec2_1) };
        break;
    case Chain:
        ChainShape chainShape = (ChainShape) shape;
        vertices = new Vector2[chainShape.getVertexCount()];
        for (int i = 0; i < vertices.length; i++) {
            vertices[i] = new Vector2();
            chainShape.getVertex(i, vertices[i]);
        }
        break;
    case Circle:
        CircleShape circleShape = (CircleShape) shape;
        Vector2 position = circleShape.getPosition();
        float radius = circleShape.getRadius();
        vertices = new Vector2[4];
        vertices[0] = new Vector2(position.x - radius, position.y + radius); // top left
        vertices[1] = new Vector2(position.x - radius, position.y - radius); // bottom left
        vertices[2] = new Vector2(position.x + radius, position.y - radius); // bottom right
        vertices[3] = new Vector2(position.x + radius, position.y + radius); // top right
        break;
    default:
        throw new IllegalArgumentException(
                "shapes of the type '" + shape.getType().name() + "' are not supported");
    }
    return vertices;
}

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

License:Apache License

/** {@link #vertices(Shape)} without caching */
private static Vector2[] vertices0(Shape shape, Vector2[] output) {
    switch (shape.getType()) {
    case Polygon:
        PolygonShape polygonShape = (PolygonShape) shape;

        if (output == null || output.length != polygonShape.getVertexCount())
            output = new Vector2[polygonShape.getVertexCount()];

        for (int i = 0; i < output.length; i++) {
            if (output[i] == null)
                output[i] = new Vector2();
            polygonShape.getVertex(i, output[i]);
        }/*from  ww  w . j  ava2 s.  c  o m*/
        break;
    case Edge:
        EdgeShape edgeShape = (EdgeShape) shape;

        edgeShape.getVertex1(vec2_0);
        edgeShape.getVertex2(vec2_1);

        if (output == null || output.length != 2)
            output = new Vector2[] { vec2_0, vec2_1 };
        else {
            if (output[0] == null)
                output[0] = new Vector2(vec2_0);
            else
                output[0].set(vec2_0);
            if (output[1] == null)
                output[1] = new Vector2(vec2_1);
            else
                output[1].set(vec2_1);
        }
        break;
    case Chain:
        ChainShape chainShape = (ChainShape) shape;

        if (output == null || output.length != chainShape.getVertexCount())
            output = new Vector2[chainShape.getVertexCount()];

        for (int i = 0; i < output.length; i++) {
            if (output[i] == null)
                output[i] = new Vector2();
            chainShape.getVertex(i, output[i]);
        }
        break;
    case Circle:
        CircleShape circleShape = (CircleShape) shape;

        if (output == null || output.length != 4)
            output = new Vector2[4];
        vec2_0.set(circleShape.getPosition().x - circleShape.getRadius(),
                circleShape.getPosition().y + circleShape.getRadius()); // top left
        output[0] = output[0] != null ? output[0].set(vec2_0) : new Vector2(vec2_0);
        vec2_0.set(circleShape.getPosition().x - circleShape.getRadius(),
                circleShape.getPosition().y - circleShape.getRadius()); // bottom left
        output[1] = output[1] != null ? output[1].set(vec2_0) : new Vector2(vec2_0);
        vec2_0.set(circleShape.getPosition().x + circleShape.getRadius(),
                circleShape.getPosition().y - circleShape.getRadius()); // bottom right
        output[2] = output[2] != null ? output[2].set(vec2_0) : new Vector2(vec2_0);
        vec2_0.set(circleShape.getPosition().x + circleShape.getRadius(),
                circleShape.getPosition().y + circleShape.getRadius()); // top right
        output[3] = output[3] != null ? output[3].set(vec2_0) : new Vector2(vec2_0);
        break;
    default:
        throw new IllegalArgumentException(
                "Shapes of the type '" + shape.getType().name() + "' are not supported");
    }
    return output;
}