Search in sources :

Example 6 with PolygonShape

use of com.badlogic.gdx.physics.box2d.PolygonShape in project commons-gdx by gemserk.

the class FixtureDefBuilder method boxShape.

public FixtureDefBuilder boxShape(float hx, float hy, Vector2 center, float angleInRadians) {
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(hx, hy, center, angleInRadians);
    fixtureDef.shape = shape;
    return this;
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape)

Example 7 with PolygonShape

use of com.badlogic.gdx.physics.box2d.PolygonShape in project commons-gdx by gemserk.

the class FixtureDefBuilder method polygonShape.

public FixtureDefBuilder polygonShape(Vector2[] vertices) {
    PolygonShape shape = new PolygonShape();
    shape.set(vertices);
    fixtureDef.shape = shape;
    return this;
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape)

Example 8 with PolygonShape

use of com.badlogic.gdx.physics.box2d.PolygonShape in project commons-gdx by gemserk.

the class FixtureDefBuilder method boxShape.

public FixtureDefBuilder boxShape(float hx, float hy) {
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(hx, hy);
    fixtureDef.shape = shape;
    return this;
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape)

Example 9 with PolygonShape

use of com.badlogic.gdx.physics.box2d.PolygonShape in project RubeLoader by tescott.

the class RubeLoaderTest method createPolySpatialsFromRubeFixtures.

/**
    * Creates an array of PolySpatials based on fixture information from the scene. Note that
    * fixtures create aligned textures.
    * 
    * @param scene
    */
private void createPolySpatialsFromRubeFixtures(RubeScene scene) {
    Array<Body> bodies = scene.getBodies();
    EarClippingTriangulator ect = new EarClippingTriangulator();
    if ((bodies != null) && (bodies.size > 0)) {
        polySpatials = new Array<PolySpatial>();
        Vector2 bodyPos = new Vector2();
        // for each body in the scene...
        for (int i = 0; i < bodies.size; i++) {
            Body body = bodies.get(i);
            bodyPos.set(body.getPosition());
            Array<Fixture> fixtures = body.getFixtureList();
            if ((fixtures != null) && (fixtures.size > 0)) {
                // for each fixture on the body...
                for (int j = 0; j < fixtures.size; j++) {
                    Fixture fixture = fixtures.get(j);
                    String textureName = (String) scene.getCustom(fixture, "TextureMask", null);
                    if (textureName != null) {
                        String textureFileName = "data/" + textureName;
                        Texture texture = textureMap.get(textureFileName);
                        TextureRegion textureRegion = null;
                        if (texture == null) {
                            texture = new Texture(textureFileName);
                            texture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
                            textureMap.put(textureFileName, texture);
                            textureRegion = new TextureRegion(texture);
                            textureRegionMap.put(texture, textureRegion);
                        } else {
                            textureRegion = textureRegionMap.get(texture);
                        }
                        // only handle polygons at this point -- no chain, edge, or circle fixtures.
                        if (fixture.getType() == Shape.Type.Polygon) {
                            PolygonShape shape = (PolygonShape) fixture.getShape();
                            int vertexCount = shape.getVertexCount();
                            float[] vertices = new float[vertexCount * 2];
                            // static bodies are texture aligned and do not get drawn based off of the related body.
                            if (body.getType() == BodyType.StaticBody) {
                                for (int k = 0; k < vertexCount; k++) {
                                    shape.getVertex(k, mTmp);
                                    mTmp.rotate(body.getAngle() * MathUtils.radiansToDegrees);
                                    // convert local coordinates to world coordinates to that textures are
                                    mTmp.add(bodyPos);
                                    // aligned
                                    vertices[k * 2] = mTmp.x * PolySpatial.PIXELS_PER_METER;
                                    vertices[k * 2 + 1] = mTmp.y * PolySpatial.PIXELS_PER_METER;
                                }
                                short[] triangleIndices = ect.computeTriangles(vertices).toArray();
                                PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangleIndices);
                                PolySpatial spatial = new PolySpatial(region, Color.WHITE);
                                polySpatials.add(spatial);
                            } else {
                                // all other fixtures are aligned based on their associated body.
                                for (int k = 0; k < vertexCount; k++) {
                                    shape.getVertex(k, mTmp);
                                    vertices[k * 2] = mTmp.x * PolySpatial.PIXELS_PER_METER;
                                    vertices[k * 2 + 1] = mTmp.y * PolySpatial.PIXELS_PER_METER;
                                }
                                short[] triangleIndices = ect.computeTriangles(vertices).toArray();
                                PolygonRegion region = new PolygonRegion(textureRegion, vertices, triangleIndices);
                                PolySpatial spatial = new PolySpatial(region, body, Color.WHITE);
                                polySpatials.add(spatial);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) EarClippingTriangulator(com.badlogic.gdx.math.EarClippingTriangulator) Texture(com.badlogic.gdx.graphics.Texture) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) Vector2(com.badlogic.gdx.math.Vector2) Fixture(com.badlogic.gdx.physics.box2d.Fixture) Body(com.badlogic.gdx.physics.box2d.Body) PolygonRegion(com.badlogic.gdx.graphics.g2d.PolygonRegion)

Example 10 with PolygonShape

use of com.badlogic.gdx.physics.box2d.PolygonShape in project libgdx by libgdx.

the class ApplyForce method createWorld.

@Override
protected void createWorld(World world) {
    world.setGravity(new Vector2(0, 0));
    float k_restitution = 0.4f;
    Body ground;
    {
        BodyDef bd = new BodyDef();
        bd.position.set(0, 20);
        ground = world.createBody(bd);
        EdgeShape shape = new EdgeShape();
        FixtureDef sd = new FixtureDef();
        sd.shape = shape;
        sd.density = 0;
        sd.restitution = k_restitution;
        shape.set(new Vector2(-20, -20), new Vector2(-20, 20));
        ground.createFixture(sd);
        shape.set(new Vector2(20, -20), new Vector2(20, 20));
        ground.createFixture(sd);
        shape.set(new Vector2(-20, 20), new Vector2(20, 20));
        ground.createFixture(sd);
        shape.set(new Vector2(-20, -20), new Vector2(20, -20));
        ground.createFixture(sd);
        shape.dispose();
    }
    {
        Transform xf1 = new Transform(new Vector2(), 0.3524f * (float) Math.PI);
        xf1.setPosition(xf1.mul(new Vector2(1, 0)));
        Vector2[] vertices = new Vector2[3];
        vertices[0] = xf1.mul(new Vector2(-1, 0));
        vertices[1] = xf1.mul(new Vector2(1, 0));
        vertices[2] = xf1.mul(new Vector2(0, 0.5f));
        PolygonShape poly1 = new PolygonShape();
        poly1.set(vertices);
        FixtureDef sd1 = new FixtureDef();
        sd1.shape = poly1;
        sd1.density = 4.0f;
        Transform xf2 = new Transform(new Vector2(), -0.3524f * (float) Math.PI);
        xf2.setPosition(xf2.mul(new Vector2(-1, 0)));
        vertices[0] = xf2.mul(new Vector2(-1, 0));
        vertices[1] = xf2.mul(new Vector2(1, 0));
        vertices[2] = xf2.mul(new Vector2(0, 0.5f));
        PolygonShape poly2 = new PolygonShape();
        poly2.set(vertices);
        FixtureDef sd2 = new FixtureDef();
        sd2.shape = poly2;
        sd2.density = 2.0f;
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.angularDamping = 5.0f;
        bd.linearDamping = 0.1f;
        bd.position.set(0, 2);
        bd.angle = (float) Math.PI;
        bd.allowSleep = false;
        m_body = world.createBody(bd);
        m_body.createFixture(sd1);
        m_body.createFixture(sd2);
        poly1.dispose();
        poly2.dispose();
    }
    {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.5f, 0.5f);
        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 1.0f;
        fd.friction = 0.3f;
        for (int i = 0; i < 10; i++) {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DynamicBody;
            bd.position.set(0, 5 + 1.54f * i);
            Body body = world.createBody(bd);
            body.createFixture(fd);
            float gravity = 10.0f;
            float I = body.getInertia();
            float mass = body.getMass();
            float radius = (float) Math.sqrt(2 * I / mass);
            FrictionJointDef jd = new FrictionJointDef();
            jd.localAnchorA.set(0, 0);
            jd.localAnchorB.set(0, 0);
            jd.bodyA = ground;
            jd.bodyB = body;
            jd.collideConnected = true;
            jd.maxForce = mass * gravity;
            jd.maxTorque = mass * radius * gravity;
            world.createJoint(jd);
        }
        shape.dispose();
    }
}
Also used : EdgeShape(com.badlogic.gdx.physics.box2d.EdgeShape) PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) Vector2(com.badlogic.gdx.math.Vector2) FrictionJointDef(com.badlogic.gdx.physics.box2d.joints.FrictionJointDef) Transform(com.badlogic.gdx.physics.box2d.Transform) Body(com.badlogic.gdx.physics.box2d.Body) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef)

Aggregations

PolygonShape (com.badlogic.gdx.physics.box2d.PolygonShape)19 Body (com.badlogic.gdx.physics.box2d.Body)14 BodyDef (com.badlogic.gdx.physics.box2d.BodyDef)12 Vector2 (com.badlogic.gdx.math.Vector2)10 FixtureDef (com.badlogic.gdx.physics.box2d.FixtureDef)9 CircleShape (com.badlogic.gdx.physics.box2d.CircleShape)7 EdgeShape (com.badlogic.gdx.physics.box2d.EdgeShape)7 Fixture (com.badlogic.gdx.physics.box2d.Fixture)4 Texture (com.badlogic.gdx.graphics.Texture)3 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)2 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)2 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)2 Box2DDebugRenderer (com.badlogic.gdx.physics.box2d.Box2DDebugRenderer)2 ChainShape (com.badlogic.gdx.physics.box2d.ChainShape)2 World (com.badlogic.gdx.physics.box2d.World)2 InputMultiplexer (com.badlogic.gdx.InputMultiplexer)1 PolygonRegion (com.badlogic.gdx.graphics.g2d.PolygonRegion)1 PolygonSpriteBatch (com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch)1 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)1 EarClippingTriangulator (com.badlogic.gdx.math.EarClippingTriangulator)1