Example usage for com.badlogic.gdx.physics.box2d EdgeShape getVertex2

List of usage examples for com.badlogic.gdx.physics.box2d EdgeShape getVertex2

Introduction

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

Prototype

public void getVertex2(Vector2 vec) 

Source Link

Usage

From source file:com.blindtigergames.werescrewed.debug.SBox2DDebugRenderer.java

License:Apache License

private void drawShape(Fixture fixture, Transform transform, Color color) {
    if (fixture.getType() == Type.Circle) {
        CircleShape circle = (CircleShape) fixture.getShape();
        t.set(circle.getPosition());/*  w w  w  . j  a  v  a2  s. c o m*/
        transform.mul(t);
        drawSolidCircle(t, circle.getRadius(),
                axis.set(transform.vals[Transform.COS], transform.vals[Transform.SIN]), color);
    }

    if (fixture.getType() == Type.Edge) {
        EdgeShape edge = (EdgeShape) fixture.getShape();
        edge.getVertex1(vertices[0]);
        edge.getVertex2(vertices[1]);
        transform.mul(vertices[0]);
        transform.mul(vertices[1]);
        drawSolidPolygon(vertices, 2, color);
    }

    if (fixture.getType() == Type.Polygon) {
        PolygonShape chain = (PolygonShape) fixture.getShape();
        int vertexCount = chain.getVertexCount();
        for (int i = 0; i < vertexCount; i++) {
            chain.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
        }
        drawSolidPolygon(vertices, vertexCount, color);
    }

    if (fixture.getType() == Type.Chain) {
        ChainShape chain = (ChainShape) fixture.getShape();
        int vertexCount = chain.getVertexCount();
        for (int i = 0; i < vertexCount; i++) {
            chain.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
        }
        drawSolidPolygon(vertices, vertexCount, color);
    }
}

From source file:com.me.mygdxgame.Entities.MydebugRenderer.java

License:Apache License

private void drawShape(Fixture fixture, Transform transform, Color color) {
    if (fixture.getType() == Type.Circle) {
        CircleShape circle = (CircleShape) fixture.getShape();
        t.set(circle.getPosition());//from w w w .ja v  a  2  s  .  c o m

        transform.mul(t);
        t.x = t.x * 29f;
        t.y = t.y * 29f;
        drawSolidCircle(t, circle.getRadius() * 29f,
                axis.set(transform.vals[Transform.COS], transform.vals[Transform.SIN]), color);
        return;
    }

    if (fixture.getType() == Type.Edge) {
        EdgeShape edge = (EdgeShape) fixture.getShape();
        edge.getVertex1(vertices[0]);
        edge.getVertex2(vertices[1]);
        transform.mul(vertices[0]);
        transform.mul(vertices[1]);
        drawSolidPolygon(vertices, 2, color, true);
        return;
    }

    if (fixture.getType() == Type.Polygon) {
        PolygonShape chain = (PolygonShape) fixture.getShape();
        int vertexCount = chain.getVertexCount();
        for (int i = 0; i < vertexCount; i++) {
            chain.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
            vertices[i].x = vertices[i].x * 29f;
            vertices[i].y = vertices[i].y * 29f;
        }
        drawSolidPolygon(vertices, vertexCount, color, true);
        return;
    }

    if (fixture.getType() == Type.Chain) {
        ChainShape chain = (ChainShape) fixture.getShape();
        int vertexCount = chain.getVertexCount();
        for (int i = 0; i < vertexCount; i++) {
            chain.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
        }
        drawSolidPolygon(vertices, vertexCount, color, false);
    }
}

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   ww w  . j av  a 2s.  c o  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:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Box2DUtils.java

License:Apache License

/** splits the given Shape using the segment described by the two given Vector2s
 *  @param shape the Shape to split//from  www  .ja v  a  2  s  . c om
 *  @param a the first point of the segment
 *  @param b the second point of the segment
 *  @param store the {@link Pair} to store the split Shapes in
 *  @return if the given shape was split */
@SuppressWarnings("unchecked")
public static <T extends Shape> boolean split(T shape, Vector2 a, Vector2 b, Pair<T, T> store) {
    Type type = shape.getType();

    if (type == Type.Circle)
        throw new IllegalArgumentException("shapes of the type " + Type.Circle
                + " cannot be split since Box2D does not support curved shapes other than circles: " + shape);

    if (type == Type.Edge) {
        Vector2 vertex1 = Pools.obtain(Vector2.class), vertex2 = Pools.obtain(Vector2.class),
                intersection = Pools.obtain(Vector2.class);
        EdgeShape es = (EdgeShape) shape;
        es.getVertex1(vertex1);
        es.getVertex2(vertex2);
        if (!Intersector.intersectSegments(a, b, vertex1, vertex2, intersection)) {
            Pools.free(vertex1);
            Pools.free(vertex2);
            Pools.free(intersection);
            return false;
        }

        EdgeShape sa = new EdgeShape(), sb = new EdgeShape();
        sa.set(vertex1, intersection);
        sb.set(intersection, vertex2);
        store.set((T) sa, (T) sb);

        Pools.free(vertex1);
        Pools.free(vertex2);
        Pools.free(intersection);
        return true;
    }

    store.clear();

    Vector2 vertices[] = vertices(shape), aa = Pools.obtain(Vector2.class).set(a),
            bb = Pools.obtain(Vector2.class).set(b);
    Array<Vector2> aVertices = Pools.obtain(Array.class), bVertices = Pools.obtain(Array.class);
    aVertices.clear();
    bVertices.clear();

    if (type == Type.Polygon) {
        aVertices.add(aa);
        aVertices.add(bb);
        GeometryUtils.arrangeClockwise(aVertices);

        if (GeometryUtils.intersectSegments(a, b, GeometryUtils.toFloatArray(vertices), aVertices.first(),
                aVertices.peek()) < 2) {
            Pools.free(aa);
            Pools.free(bb);
            Pools.free(aVertices);
            Pools.free(bVertices);
            return false;
        }

        bVertices.add(aa);
        bVertices.add(bb);

        for (Vector2 vertice : vertices) {
            float det = MathUtils.det(aa.x, aa.y, vertice.x, vertice.y, bb.x, bb.y);
            if (det < 0)
                aVertices.add(vertice);
            else if (det > 0)
                bVertices.add(vertice);
            else {
                aVertices.add(vertice);
                bVertices.add(vertice);
            }
        }

        GeometryUtils.arrangeClockwise(aVertices);
        GeometryUtils.arrangeClockwise(bVertices);

        Vector2[] aVerticesArray = aVertices.toArray(Vector2.class),
                bVerticesArray = bVertices.toArray(Vector2.class);

        if (checkPreconditions) {
            if (aVertices.size >= 3 && aVertices.size <= maxPolygonVertices && bVertices.size >= 3
                    && bVertices.size <= maxPolygonVertices) {
                float[] aVerticesFloatArray = GeometryUtils.toFloatArray(aVerticesArray),
                        bVerticesFloatArray = GeometryUtils.toFloatArray(bVerticesArray);
                if (GeometryUtils.area(aVerticesFloatArray) > minExclusivePolygonArea
                        && GeometryUtils.area(bVerticesFloatArray) > minExclusivePolygonArea) {
                    PolygonShape sa = new PolygonShape(), sb = new PolygonShape();
                    sa.set(aVerticesFloatArray);
                    sb.set(bVerticesFloatArray);
                    store.set((T) sa, (T) sb);
                }
            }
        } else {
            PolygonShape sa = new PolygonShape(), sb = new PolygonShape();
            sa.set(aVerticesArray);
            sb.set(bVerticesArray);
            store.set((T) sa, (T) sb);
        }
    } else if (type == Type.Chain) {
        Vector2 tmp = Pools.obtain(Vector2.class);
        boolean intersected = false;
        for (int i = 0; i < vertices.length; i++) {
            if (!intersected)
                aVertices.add(vertices[i]);
            else
                bVertices.add(vertices[i]);
            if (!intersected && i + 1 < vertices.length
                    && Intersector.intersectSegments(vertices[i], vertices[i + 1], aa, bb, tmp)) {
                intersected = true;
                aVertices.add(tmp);
                bVertices.add(tmp);
            }
        }
        if (intersected)
            if (!checkPreconditions || aVertices.size >= 3 && bVertices.size >= 3) {
                ChainShape sa = new ChainShape(), sb = new ChainShape();
                sa.createChain((Vector2[]) aVertices.toArray(Vector2.class));
                sb.createChain((Vector2[]) bVertices.toArray(Vector2.class));
                store.set((T) sa, (T) sb);
            }
        Pools.free(tmp);
    }

    Pools.free(aa);
    Pools.free(bb);
    Pools.free(aVertices);
    Pools.free(bVertices);

    return store.isFull();
}

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  w  w  w .  j a  v  a  2s  . 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;
}

From source file:org.box2d.r3.gdx.GDXBox2DDebugRenderer.java

License:Apache License

private void drawShape(final Fixture fixture, final Transform transform, final Color color) {
    if (fixture.getType() == Type.Circle) {
        final CircleShape circle = (CircleShape) fixture.getShape();
        t.set(circle.getPosition());//from  w w  w  .  jav  a 2 s  .  c  om
        transform.mul(t);
        this.drawSolidCircle(t, circle.getRadius(),
                axis.set(transform.vals[Transform.COS], transform.vals[Transform.SIN]), color);
        return;
    }

    if (fixture.getType() == Type.Edge) {
        final EdgeShape edge = (EdgeShape) fixture.getShape();
        edge.getVertex1(vertices[0]);
        edge.getVertex2(vertices[1]);
        transform.mul(vertices[0]);
        transform.mul(vertices[1]);
        this.drawSolidPolygon(vertices, 2, color, true);
        return;
    }

    if (fixture.getType() == Type.Polygon) {
        final PolygonShape chain = (PolygonShape) fixture.getShape();
        final int vertexCount = chain.getVertexCount();
        for (int i = 0; i < vertexCount; i++) {
            chain.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
        }
        this.drawSolidPolygon(vertices, vertexCount, color, true);
        return;
    }

    if (fixture.getType() == Type.Chain) {
        final ChainShape chain = (ChainShape) fixture.getShape();
        final int vertexCount = chain.getVertexCount();
        for (int i = 0; i < vertexCount; i++) {
            chain.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
        }
        this.drawSolidPolygon(vertices, vertexCount, color, false);
    }
}