Example usage for com.badlogic.gdx.physics.box2d Fixture getShape

List of usage examples for com.badlogic.gdx.physics.box2d Fixture getShape

Introduction

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

Prototype

public Shape getShape() 

Source Link

Document

Returns the shape of this fixture

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/*  ww  w.j  av 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());/*w w w.j a  v a 2 s . com*/
        transform.mul(t);
        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);/*from  w  w  w  .j  a  v a2 s . c o 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());/*  ww w .  j a v a  2s.  co  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.hajnar.GravityShip.GameObjects.Terrain.java

License:Apache License

public void generateMeshes() {
    ArrayList<Fixture> terrainFixtures = objectBody.getFixtureList();
    Vector2 boxVertex = new Vector2();
    meshes = new ArrayList<Mesh>();
    boundingBoxes = new ArrayList<BoundingBox>();
    EarClippingTriangulator ear = new EarClippingTriangulator();

    for (Fixture terrainFixture : terrainFixtures) {
        PolygonShape shape = (PolygonShape) terrainFixture.getShape();
        boxVertex = new Vector2();
        int vc = shape.getVertexCount();
        ArrayList<Vector2> boxVertices = new ArrayList<Vector2>();
        ArrayList<Vector2> triaBoxVertices = new ArrayList<Vector2>();

        for (int i = 0; i < vc; i++) {
            shape.getVertex(i, boxVertex);
            boxVertex = objectBody.getWorldPoint(boxVertex).mul(Helper.BOX_TO_WORLD);
            boxVertices.add(boxVertex.cpy());
        }/*w  w w.  j a v a  2 s .  c  om*/
        triaBoxVertices = (ArrayList<Vector2>) ear.computeTriangles(boxVertices);
        float[] meshVertices = new float[triaBoxVertices.size() * 4];
        short[] meshIndices = new short[triaBoxVertices.size()];
        for (int i = 0; i < triaBoxVertices.size(); i++) {
            meshVertices[i * 4] = triaBoxVertices.get(i).x;
            meshVertices[i * 4 + 1] = triaBoxVertices.get(i).y;
            meshVertices[i * 4 + 2] = triaBoxVertices.get(i).x * TEXTURE_SCALE;
            meshVertices[i * 4 + 3] = triaBoxVertices.get(i).y * TEXTURE_SCALE;
            meshIndices[i] = (short) i;
        }

        Mesh mesh = new Mesh(true, triaBoxVertices.size(), triaBoxVertices.size(),
                new VertexAttribute(Usage.Position, 2, ShaderProgram.POSITION_ATTRIBUTE),
                new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"));
        mesh.setVertices(meshVertices);
        mesh.setIndices(meshIndices);
        meshes.add(mesh);
        boundingBoxes.add(mesh.calculateBoundingBox());
    }
}

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);//  w w  w  .  j a  v  a  2 s. c o 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());/*  w ww .j  av a  2  s.com*/

        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

/** @see #vertices(Shape) */
public static Vector2[] vertices(Fixture fixture) {
    return vertices(fixture.getShape());
}

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

License:Apache License

/** @see #minX(Shape) */
public static float minX(Fixture fixture) {
    return minX(fixture.getShape());
}

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

License:Apache License

/** @see #minY(Shape) */
public static float minY(Fixture fixture) {
    return minY(fixture.getShape());
}