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

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

Introduction

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

Prototype

public int getVertexCount() 

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// w ww .  j  a  v  a 2  s .  co 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  ww  .j  ava2s . co  m
        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 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  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.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());
        }/*from   w  ww. j  av  a2 s  . co m*/
        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 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  va  2s  .  co  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;//  ww  w .  j  a v a2s  .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.tnf.ptm.entities.ship.ShipBuilder.java

License:Apache License

private static Fixture getBase(boolean hasBase, Body body) {
    if (!hasBase) {
        return null;
    }//from  w  w w.  ja va2  s. c  o  m
    Fixture base = null;
    Vector2 v = PtmMath.getVec();
    float lowestX = Float.MAX_VALUE;
    for (Fixture f : body.getFixtureList()) {
        Shape s = f.getShape();
        if (!(s instanceof PolygonShape)) {
            continue;
        }
        PolygonShape poly = (PolygonShape) s;
        int pointCount = poly.getVertexCount();
        for (int i = 0; i < pointCount; i++) {
            poly.getVertex(i, v);
            if (v.x < lowestX) {
                base = f;
                lowestX = v.x;
            }
        }
    }
    PtmMath.free(v);
    return base;
}

From source file:edu.lehigh.cse.lol.Actor.java

License:Open Source License

/**
 * Change the size of an actor, and/or change its position
 *
 * @param x      The new X coordinate of its bottom left corner
 * @param y      The new Y coordinate of its bototm left corner
 * @param width  The new width of the actor
 * @param height The new height of the actor
 *///from  w w  w  . j a  v a 2s. c  o m
public void resize(float x, float y, float width, float height) {
    // To scale a polygon, we'll need a scaling factor, so we can
    // manually scale each point
    float xscale = height / mSize.y;
    float yscale = width / mSize.x;
    // set new height and width
    mSize.x = width;
    mSize.y = height;
    // read old body information
    Body oldBody = mBody;
    // make a new body
    if (mIsCircleBody) {
        Fixture oldFix = oldBody.getFixtureList().get(0);
        setCirclePhysics(oldFix.getDensity(), oldFix.getRestitution(), oldFix.getFriction(), oldBody.getType(),
                oldBody.isBullet(), x, y, (width > height) ? width / 2 : height / 2);
    } else if (mIsBoxBody) {
        Fixture oldFix = oldBody.getFixtureList().get(0);
        setBoxPhysics(oldFix.getDensity(), oldFix.getRestitution(), oldFix.getFriction(), oldBody.getType(),
                oldBody.isBullet(), x, y);
    } else if (mIsPolygonBody) {
        Fixture oldFix = oldBody.getFixtureList().get(0);
        // we need to manually scale all the vertices
        PolygonShape ps = (PolygonShape) oldFix.getShape();
        float[] verts = new float[ps.getVertexCount() * 2];
        for (int i = 0; i < ps.getVertexCount(); ++i) {
            ps.getVertex(i, mTmpVert);
            verts[2 * i] = mTmpVert.x * xscale;
            verts[2 * i + 1] = mTmpVert.y * yscale;
        }
        setPolygonPhysics(oldFix.getDensity(), oldFix.getRestitution(), oldFix.getFriction(), oldBody.getType(),
                oldBody.isBullet(), x, y, verts);
    }
    // clone forces
    mBody.setAngularVelocity(oldBody.getAngularVelocity());
    mBody.setTransform(mBody.getPosition(), oldBody.getAngle());
    mBody.setGravityScale(oldBody.getGravityScale());
    mBody.setLinearDamping(oldBody.getLinearDamping());
    mBody.setLinearVelocity(oldBody.getLinearVelocity());
    // disable the old body
    oldBody.setActive(false);
}

From source file:loon.physics.PhysicsUtils.java

License:Apache License

public static void calculateShapeCenter(PolygonShape polygonShape, Vector2f center) {
    center.x = 0f;//from   w ww . j  a  v  a  2 s  .co m
    center.y = 0f;

    int vertexCount = polygonShape.getVertexCount();

    if (vertexCount == 0) {
        return;
    }

    for (int i = 0; i < vertexCount; i++) {
        polygonShape.getVertex(i, tmp);
        center.x += tmp.x;
        center.y += tmp.y;
    }

    center.x /= vertexCount;
    center.y /= vertexCount;
}

From source file:loon.physics.PhysicsUtils.java

License:Apache License

public static void translatePolygonShape(PolygonShape polygonShape, float tx, float ty) {
    if (polygonShape.getVertexCount() > vertices.length)
        throw new RuntimeException("unexpected polygon shape length, should be less than " + vertices.length);
    Vector2f[] newVertices = new Vector2f[polygonShape.getVertexCount()];
    for (int i = 0; i < polygonShape.getVertexCount(); i++) {
        polygonShape.getVertex(i, vertices[i]);
        vertices[i].add(tx, ty);//from www .  j a va 2 s .c  om
        newVertices[i] = vertices[i];
    }
    polygonShape.set(newVertices);
}