Example usage for com.badlogic.gdx.physics.box2d CircleShape getPosition

List of usage examples for com.badlogic.gdx.physics.box2d CircleShape getPosition

Introduction

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

Prototype

public Vector2 getPosition() 

Source Link

Usage

From source file:Tabox2D.java

License:Open Source License

/**
 * Combines different tabodies in a single one.<br/>
 * This is useful to have a body with different fixtures in an easy way
 * @param tabodyArray Array of tabodies to combine
 * @return A new Tabody//from  w  w  w  .ja v  a  2s. c  o m
 */
public Tabody combine(String type, Tabody... tabodyArray) {
    if (tabodyArray.length > 0) {

        Tabody newTabody = new Tabody();

        BodyDef bodyDef = new BodyDef();
        setType(bodyDef, type);

        //List<Vector2> centers = new ArrayList<Vector2>();

        //AABB center of combines bodies:
        List<Vector2> ptsCombined = new ArrayList<Vector2>();
        for (int i = 0; i < tabodyArray.length; i++) {
            //centers.add(tabodyArray[i].body.getWorldCenter());
            Tabody t = tabodyArray[i];
            for (Fixture f : t.body.getFixtureList()) {
                if (f.getShape() instanceof CircleShape) {
                    CircleShape cS = (CircleShape) f.getShape();
                    // top-left and bottom-right of circle bounds:
                    ptsCombined.add(new Vector2(cS.getPosition().x - cS.getRadius(),
                            cS.getPosition().y + cS.getRadius()));
                    ptsCombined.add(new Vector2(cS.getPosition().x + cS.getRadius(),
                            cS.getPosition().y - cS.getRadius()));
                } else if (f.getShape() instanceof PolygonShape) {
                    PolygonShape pS = (PolygonShape) f.getShape();
                    for (int n = 0; n < pS.getVertexCount(); n++) {
                        Vector2 tmp = new Vector2();
                        pS.getVertex(n, tmp);
                        ptsCombined.add(
                                new Vector2(t.body.getPosition().x + tmp.x, t.body.getPosition().y + tmp.y));
                    }
                }
            }
        }

        Rectangle rectangle = boundingBoxOf(ptsCombined);

        bodyDef.position.set(rectangle.x + rectangle.width / 2, rectangle.y + rectangle.height / 2);

        newTabody.w = rectangle.width * meterSize;
        newTabody.h = rectangle.height * meterSize;
        newTabody.body = world.createBody(bodyDef);

        for (int i = 0; i < tabodyArray.length; i++) {

            Tabody t = tabodyArray[i];

            for (Fixture f : t.body.getFixtureList()) {
                FixtureDef fixtureDef = new FixtureDef();

                fixtureDef.density = f.getDensity();
                fixtureDef.friction = f.getFriction();
                fixtureDef.restitution = f.getRestitution();
                // Delta X and Y for translating the shapes:
                float dx = t.body.getWorldCenter().x - bodyDef.position.x;
                float dy = t.body.getWorldCenter().y - bodyDef.position.y;
                if (f.getShape() instanceof CircleShape) {

                    CircleShape circleShape = (CircleShape) f.getShape();
                    circleShape.setPosition(
                            new Vector2(circleShape.getPosition().x + dx, circleShape.getPosition().y + dy));
                    fixtureDef.shape = circleShape;

                } else if (f.getShape() instanceof PolygonShape) {

                    PolygonShape polygonShape = (PolygonShape) f.getShape();

                    float[] pts = new float[polygonShape.getVertexCount() * 2];
                    int vertexIndex = 0;
                    // delta X and delta Y respect to the main body:
                    dx = t.body.getPosition().x - bodyDef.position.x;
                    dy = t.body.getPosition().y - bodyDef.position.y;
                    for (int j = 0; j < pts.length - 1; j += 2) {
                        Vector2 tmp = new Vector2();
                        polygonShape.getVertex(vertexIndex, tmp);
                        pts[j] = tmp.x + dx;
                        pts[j + 1] = tmp.y + dy;
                        vertexIndex++;
                    }
                    polygonShape.set(pts);
                    fixtureDef.shape = polygonShape;

                } else if (f.getShape() instanceof EdgeShape) {
                    EdgeShape edgeShape = (EdgeShape) f.getShape();
                    fixtureDef.shape = edgeShape;
                }

                newTabody.body.createFixture(fixtureDef);
            }
        }

        // Destroy:
        for (int i = 0; i < tabodyArray.length; i++) {
            world.destroyBody(tabodyArray[i].body);
            tabodies.remove(tabodyArray[i]);
        }

        // Add new Tabody:
        tabodies.add(newTabody);
        return newTabody;
    } else {
        System.err.println("No tabodies specified in Tabox2D.combine()");
        return null;
    }
}

From source file:com.altportalgames.colorrain.utils.Box2DDebugRenderer.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());
        transform.mul(t);//from  w  w w .j  av  a  2  s.  c o  m
        drawSolidCircle(t, circle.getRadius(),
                axis.set(transform.vals[Transform.COL1_X], transform.vals[Transform.COL1_Y]), color);
    } else {
        PolygonShape poly = (PolygonShape) fixture.getShape();
        int vertexCount = poly.getVertexCount();
        for (int i = 0; i < vertexCount; i++) {
            poly.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
        }
        drawSolidPolygon(vertices, vertexCount, color);
    }
}

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

License:Apache License

private void drawAABB(Fixture fixture, Transform transform) {
    if (fixture.getType() == Type.Circle) {

        CircleShape shape = (CircleShape) fixture.getShape();
        float radius = shape.getRadius();
        vertices[0].set(shape.getPosition());
        vertices[0].rotate(transform.getRotation()).add(transform.getPosition());
        lower.set(vertices[0].x - radius, vertices[0].y - radius);
        upper.set(vertices[0].x + radius, vertices[0].y + radius);

        // define vertices in ccw fashion...
        vertices[0].set(lower.x, lower.y);
        vertices[1].set(upper.x, lower.y);
        vertices[2].set(upper.x, upper.y);
        vertices[3].set(lower.x, upper.y);

        drawSolidPolygon(vertices, 4, AABB_COLOR);
    } else if (fixture.getType() == Type.Polygon) {
        PolygonShape shape = (PolygonShape) fixture.getShape();
        int vertexCount = shape.getVertexCount();

        shape.getVertex(0, vertices[0]);
        lower.set(transform.mul(vertices[0]));
        upper.set(lower);// www.  ja  v  a 2 s .  co m
        for (int i = 1; i < vertexCount; i++) {
            shape.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
            lower.x = Math.min(lower.x, vertices[i].x);
            lower.y = Math.min(lower.y, vertices[i].y);
            upper.x = Math.max(upper.x, vertices[i].x);
            upper.y = Math.max(upper.y, vertices[i].y);
        }

        // define vertices in ccw fashion...
        vertices[0].set(lower.x, lower.y);
        vertices[1].set(upper.x, lower.y);
        vertices[2].set(upper.x, upper.y);
        vertices[3].set(lower.x, upper.y);

        drawSolidPolygon(vertices, 4, AABB_COLOR);
    }
}

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());
        transform.mul(t);//from   w  w w.j a  v  a2s. co  m
        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 drawAABB(Fixture fixture, Transform transform) {
    if (fixture.getType() == Type.Circle) {

        CircleShape shape = (CircleShape) fixture.getShape();
        float radius = shape.getRadius();
        vertices[0].set(shape.getPosition());
        vertices[0].rotate(transform.getRotation()).add(transform.getPosition());
        lower.set(vertices[0].x - radius, vertices[0].y - radius);
        upper.set(vertices[0].x + radius, vertices[0].y + radius);

        // define vertices in ccw fashion...
        vertices[0].set(lower.x, lower.y);
        vertices[1].set(upper.x, lower.y);
        vertices[2].set(upper.x, upper.y);
        vertices[3].set(lower.x, upper.y);

        drawSolidPolygon(vertices, 4, AABB_COLOR, true);
    } else if (fixture.getType() == Type.Polygon) {
        PolygonShape shape = (PolygonShape) fixture.getShape();
        int vertexCount = shape.getVertexCount();

        shape.getVertex(0, vertices[0]);
        lower.set(transform.mul(vertices[0]));
        upper.set(lower);/*from  w w w  .  j  ava2 s. co  m*/
        for (int i = 1; i < vertexCount; i++) {
            shape.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
            lower.x = Math.min(lower.x, vertices[i].x);
            lower.y = Math.min(lower.y, vertices[i].y);
            upper.x = Math.max(upper.x, vertices[i].x);
            upper.y = Math.max(upper.y, vertices[i].y);
        }

        // define vertices in ccw fashion...
        vertices[0].set(lower.x, lower.y);
        vertices[1].set(upper.x, lower.y);
        vertices[2].set(upper.x, upper.y);
        vertices[3].set(lower.x, upper.y);

        drawSolidPolygon(vertices, 4, AABB_COLOR, true);
    }
}

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());

        transform.mul(t);/* w  w w  . j a v a 2s  . c o m*/
        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;// w  w  w  .  ja v a2  s.  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:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Box2DUtils.java

License:Apache License

/** @see #positionRelative(Shape, float)
 *  @see CircleShape#getPosition() */
public static Vector2 positionRelative(CircleShape shape) {
    return shape.getPosition();
}

From source file:loon.physics.PhysicsUtils.java

License:Apache License

public static void translateCircleShape(CircleShape circleShape, float tx, float ty) {
    Vector2f position = circleShape.getPosition();
    position.add(tx, ty);//  w  w  w.  j  a v a  2  s .  c  om
    circleShape.setPosition(position);
}

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  va 2  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;
}