Search in sources :

Example 11 with PolygonShape

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

the class SimpleTest method createWorld.

@Override
protected void createWorld(World world) {
    // next we create a static ground platform. This platform
    // is not moveable and will not react to any influences from
    // outside. It will however influence other bodies. First we
    // create a PolygonShape that holds the form of the platform.
    // it will be 100 meters wide and 2 meters high, centered
    // around the origin
    PolygonShape groundPoly = new PolygonShape();
    groundPoly.setAsBox(50, 1);
    // next we create the body for the ground platform. It's
    // simply a static body.
    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.type = BodyType.StaticBody;
    groundBody = world.createBody(groundBodyDef);
    // finally we add a fixture to the body using the polygon
    // defined above. Note that we have to dispose PolygonShapes
    // and CircleShapes once they are no longer used. This is the
    // only time you have to care explicitly for memomry managment.
    groundBody.createFixture(groundPoly, 10);
    groundPoly.dispose();
    // next we create 50 boxes at random locations above the ground
    // body. First we create a nice polygon representing a box 2 meters
    // wide and high.
    PolygonShape boxPoly = new PolygonShape();
    boxPoly.setAsBox(1, 1);
    // body. Note that we reuse the polygon for each body fixture.
    for (int i = 0; i < 20; i++) {
        // Create the BodyDef, set a random position above the
        // ground and create a new body
        BodyDef boxBodyDef = new BodyDef();
        boxBodyDef.type = BodyType.DynamicBody;
        boxBodyDef.position.x = -24 + (float) (Math.random() * 48);
        boxBodyDef.position.y = 10 + (float) (Math.random() * 100);
        Body boxBody = world.createBody(boxBodyDef);
        // add the boxPoly shape as a fixture
        boxBody.createFixture(boxPoly, 10);
    }
    // we are done, all that's left is disposing the boxPoly
    boxPoly.dispose();
    // next we add a few more circles
    CircleShape circleShape = new CircleShape();
    circleShape.setRadius(1);
    for (int i = 0; i < 10; i++) {
        BodyDef circleBodyDef = new BodyDef();
        circleBodyDef.type = BodyType.DynamicBody;
        circleBodyDef.position.x = -24 + (float) (Math.random() * 48);
        circleBodyDef.position.y = 10 + (float) (Math.random() * 100);
        Body circleBody = world.createBody(circleBodyDef);
        // add the boxPoly shape as a fixture
        circleBody.createFixture(circleShape, 10);
    }
    circleShape.dispose();
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) CircleShape(com.badlogic.gdx.physics.box2d.CircleShape) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) Body(com.badlogic.gdx.physics.box2d.Body)

Example 12 with PolygonShape

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

the class Bridge method createWorld.

@Override
protected void createWorld(World world) {
    Body ground;
    {
        BodyDef bd = new BodyDef();
        ground = world.createBody(bd);
        EdgeShape shape = new EdgeShape();
        shape.set(new Vector2(-40, 0), new Vector2(40.0f, 0));
        ground.createFixture(shape, 0);
        shape.dispose();
    }
    {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.5f, 0.125f);
        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 20.0f;
        fd.friction = 0.2f;
        RevoluteJointDef jd = new RevoluteJointDef();
        Body prevBody = ground;
        for (int i = 0; i < e_count; i++) {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DynamicBody;
            bd.position.set(-14.5f + 1.0f * i, 5.0f);
            Body body = world.createBody(bd);
            body.createFixture(fd);
            Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 5.0f);
            jd.initialize(prevBody, body, anchor);
            world.createJoint(jd);
            prevBody = body;
        }
        Vector2 anchor = new Vector2(-15.0f + 1.0f * e_count, 5.0f);
        jd.initialize(prevBody, ground, anchor);
        world.createJoint(jd);
        shape.dispose();
    }
    for (int i = 0; i < 2; i++) {
        Vector2[] vertices = new Vector2[3];
        vertices[0] = new Vector2(-0.5f, 0);
        vertices[1] = new Vector2(0.5f, 0);
        vertices[2] = new Vector2(0, 1.5f);
        PolygonShape shape = new PolygonShape();
        shape.set(vertices);
        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 1.0f;
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.position.set(-8.0f + 8.0f * i, 12.0f);
        Body body = world.createBody(bd);
        body.createFixture(fd);
        shape.dispose();
    }
    for (int i = 0; i < 3; i++) {
        CircleShape shape = new CircleShape();
        shape.setRadius(0.5f);
        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 1.0f;
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.position.set(-6.0f + 6.0f * i, 10.0f);
        Body body = world.createBody(bd);
        body.createFixture(fd);
        shape.dispose();
    }
}
Also used : EdgeShape(com.badlogic.gdx.physics.box2d.EdgeShape) PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) Vector2(com.badlogic.gdx.math.Vector2) CircleShape(com.badlogic.gdx.physics.box2d.CircleShape) Body(com.badlogic.gdx.physics.box2d.Body) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef) RevoluteJointDef(com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef)

Example 13 with PolygonShape

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

the class Cantilever method createWorld.

@Override
protected void createWorld(World world) {
    Body ground;
    {
        BodyDef bd = new BodyDef();
        ground = world.createBody(bd);
        EdgeShape shape = new EdgeShape();
        shape.set(new Vector2(-40, 0), new Vector2(40, 0));
        ground.createFixture(shape, 0);
        shape.dispose();
    }
    {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.5f, 0.125f);
        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 20.0f;
        WeldJointDef jd = new WeldJointDef();
        Body prevBody = ground;
        for (int i = 0; i < e_count; i++) {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DynamicBody;
            bd.position.set(-14.5f + 1.0f * i, 5.0f);
            Body body = world.createBody(bd);
            body.createFixture(fd);
            Vector2 anchor = new Vector2(-15.0f + 1 * i, 5.0f);
            jd.initialize(prevBody, body, anchor);
            world.createJoint(jd);
            prevBody = body;
        }
        shape.dispose();
    }
    {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.5f, 0.125f);
        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 20.0f;
        WeldJointDef jd = new WeldJointDef();
        Body prevBody = ground;
        for (int i = 0; i < e_count; i++) {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DynamicBody;
            bd.position.set(-14.5f + 1.0f * i, 15.0f);
            bd.gravityScale = 10.0f;
            Body body = world.createBody(bd);
            body.createFixture(fd);
            Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 15.0f);
            jd.initialize(prevBody, body, anchor);
            world.createJoint(jd);
            prevBody = body;
        }
        shape.dispose();
    }
    {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.5f, 0.125f);
        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 20.0f;
        WeldJointDef jd = new WeldJointDef();
        Body prevBody = ground;
        for (int i = 0; i < e_count; i++) {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DynamicBody;
            bd.position.set(-4.5f + 1.0f * i, 5.0f);
            Body body = world.createBody(bd);
            body.createFixture(fd);
            if (i > 0) {
                Vector2 anchor = new Vector2(-5.0f + 1.0f * i, 5.0f);
                jd.initialize(prevBody, body, anchor);
                world.createJoint(jd);
            }
            prevBody = body;
        }
        shape.dispose();
    }
    {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.5f, 0.125f);
        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 20.0f;
        WeldJointDef jd = new WeldJointDef();
        Body prevBody = ground;
        for (int i = 0; i < e_count; i++) {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DynamicBody;
            bd.position.set(5.5f + 1.0f * i, 10.0f);
            bd.gravityScale = 10.0f;
            Body body = world.createBody(bd);
            body.createFixture(fd);
            if (i > 0) {
                Vector2 anchor = new Vector2(5.0f + 1.0f * i, 10.0f);
                jd.initialize(prevBody, body, anchor);
                world.createJoint(jd);
            }
            prevBody = body;
        }
        shape.dispose();
    }
    for (int i = 0; i < 2; i++) {
        Vector2[] vertices = new Vector2[3];
        vertices[0] = new Vector2(-0.5f, 0);
        vertices[1] = new Vector2(0.5f, 0);
        vertices[2] = new Vector2(0, 1.5f);
        PolygonShape shape = new PolygonShape();
        shape.set(vertices);
        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 1.0f;
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.position.set(-8.0f + 8.0f * i, 12.0f);
        Body body = world.createBody(bd);
        body.createFixture(fd);
        shape.dispose();
    }
    for (int i = 0; i < 2; i++) {
        CircleShape shape = new CircleShape();
        shape.setRadius(0.5f);
        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 1.0f;
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.position.set(-6.0f + 6.0f * i, 10.0f);
        Body body = world.createBody(bd);
        body.createFixture(fd);
        shape.dispose();
    }
}
Also used : EdgeShape(com.badlogic.gdx.physics.box2d.EdgeShape) PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) Vector2(com.badlogic.gdx.math.Vector2) CircleShape(com.badlogic.gdx.physics.box2d.CircleShape) Body(com.badlogic.gdx.physics.box2d.Body) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) WeldJointDef(com.badlogic.gdx.physics.box2d.joints.WeldJointDef) FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef)

Example 14 with PolygonShape

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

the class CollisionFiltering method createWorld.

@Override
protected void createWorld(World world) {
    {
        EdgeShape shape = new EdgeShape();
        shape.set(new Vector2(-40.0f, 0), new Vector2(40, 0));
        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.friction = 0.3f;
        BodyDef bd = new BodyDef();
        Body ground = world.createBody(bd);
        ground.createFixture(fd);
        shape.dispose();
    }
    Vector2[] vertices = new Vector2[3];
    vertices[0] = new Vector2(-1, 0);
    vertices[1] = new Vector2(1, 0);
    vertices[2] = new Vector2(0, 2);
    PolygonShape polygon = new PolygonShape();
    polygon.set(vertices);
    FixtureDef triangleShapeDef = new FixtureDef();
    triangleShapeDef.shape = polygon;
    triangleShapeDef.density = 1.0f;
    triangleShapeDef.filter.groupIndex = k_smallGroup;
    triangleShapeDef.filter.categoryBits = k_triangleCategory;
    triangleShapeDef.filter.maskBits = k_triangleMask;
    BodyDef triangleBodyDef = new BodyDef();
    triangleBodyDef.type = BodyType.DynamicBody;
    triangleBodyDef.position.set(-5, 2);
    Body body1 = world.createBody(triangleBodyDef);
    body1.createFixture(triangleShapeDef);
    vertices[0].scl(2);
    vertices[1].scl(2);
    vertices[2].scl(2);
    polygon.set(vertices);
    triangleShapeDef.filter.groupIndex = k_largeGroup;
    triangleBodyDef.position.set(-5, 6);
    triangleBodyDef.fixedRotation = true;
    Body body2 = world.createBody(triangleBodyDef);
    body2.createFixture(triangleShapeDef);
    {
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.position.set(-5, 10);
        Body body = world.createBody(bd);
        PolygonShape p = new PolygonShape();
        p.setAsBox(0.5f, 1.0f);
        body.createFixture(p, 1);
        PrismaticJointDef jd = new PrismaticJointDef();
        jd.bodyA = body2;
        jd.bodyB = body;
        jd.enableLimit = true;
        jd.localAnchorA.set(0, 4);
        jd.localAnchorB.set(0, 0);
        jd.localAxisA.set(0, 1);
        jd.lowerTranslation = -1;
        jd.upperTranslation = 1;
        world.createJoint(jd);
        p.dispose();
    }
    polygon.setAsBox(1, 0.5f);
    FixtureDef boxShapeDef = new FixtureDef();
    boxShapeDef.shape = polygon;
    boxShapeDef.density = 1;
    boxShapeDef.restitution = 0.1f;
    boxShapeDef.filter.groupIndex = k_smallGroup;
    boxShapeDef.filter.categoryBits = k_boxCategory;
    boxShapeDef.filter.maskBits = k_boxMask;
    BodyDef boxBodyDef = new BodyDef();
    boxBodyDef.type = BodyType.DynamicBody;
    boxBodyDef.position.set(0, 2);
    Body body3 = world.createBody(boxBodyDef);
    body3.createFixture(boxShapeDef);
    polygon.setAsBox(2, 1);
    boxShapeDef.filter.groupIndex = k_largeGroup;
    boxBodyDef.position.set(0, 6);
    Body body4 = world.createBody(boxBodyDef);
    body4.createFixture(boxShapeDef);
    CircleShape circle = new CircleShape();
    circle.setRadius(1);
    FixtureDef circleShapeDef = new FixtureDef();
    circleShapeDef.shape = circle;
    circleShapeDef.density = 1.0f;
    circleShapeDef.filter.groupIndex = k_smallGroup;
    circleShapeDef.filter.categoryBits = k_circleCategory;
    circleShapeDef.filter.maskBits = k_circleMask;
    BodyDef circleBodyDef = new BodyDef();
    circleBodyDef.type = BodyType.DynamicBody;
    circleBodyDef.position.set(5, 2);
    Body body5 = world.createBody(circleBodyDef);
    body5.createFixture(circleShapeDef);
    circle.setRadius(2);
    circleShapeDef.filter.groupIndex = k_largeGroup;
    circleBodyDef.position.set(5, 6);
    Body body6 = world.createBody(circleBodyDef);
    body6.createFixture(circleShapeDef);
}
Also used : EdgeShape(com.badlogic.gdx.physics.box2d.EdgeShape) PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) Vector2(com.badlogic.gdx.math.Vector2) CircleShape(com.badlogic.gdx.physics.box2d.CircleShape) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) Body(com.badlogic.gdx.physics.box2d.Body) FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef) PrismaticJointDef(com.badlogic.gdx.physics.box2d.joints.PrismaticJointDef)

Example 15 with PolygonShape

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

the class Box2DCharacterControllerTest method createBox.

Body createBox(BodyType type, float width, float height, float density) {
    BodyDef def = new BodyDef();
    def.type = type;
    Body box = world.createBody(def);
    PolygonShape poly = new PolygonShape();
    poly.setAsBox(width, height);
    box.createFixture(poly, density);
    poly.dispose();
    return box;
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) Body(com.badlogic.gdx.physics.box2d.Body)

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