Search in sources :

Example 31 with Body

use of com.badlogic.gdx.physics.box2d.Body 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 32 with Body

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

the class Box2DCharacterControllerTest method createEdge.

private Body createEdge(BodyType type, float x1, float y1, float x2, float y2, float density) {
    BodyDef def = new BodyDef();
    def.type = type;
    Body box = world.createBody(def);
    EdgeShape poly = new EdgeShape();
    poly.set(new Vector2(0, 0), new Vector2(x2 - x1, y2 - y1));
    box.createFixture(poly, density);
    box.setTransform(x1, y1, 0);
    poly.dispose();
    return box;
}
Also used : EdgeShape(com.badlogic.gdx.physics.box2d.EdgeShape) Vector2(com.badlogic.gdx.math.Vector2) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) Body(com.badlogic.gdx.physics.box2d.Body)

Example 33 with Body

use of com.badlogic.gdx.physics.box2d.Body 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)

Example 34 with Body

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

the class Box2DCharacterControllerTest method createPlayer.

private Body createPlayer() {
    BodyDef def = new BodyDef();
    def.type = BodyType.DynamicBody;
    Body box = world.createBody(def);
    PolygonShape poly = new PolygonShape();
    poly.setAsBox(0.45f, 1.4f);
    playerPhysicsFixture = box.createFixture(poly, 1);
    poly.dispose();
    CircleShape circle = new CircleShape();
    circle.setRadius(0.45f);
    circle.setPosition(new Vector2(0, -1.4f));
    playerSensorFixture = box.createFixture(circle, 0);
    circle.dispose();
    box.setBullet(true);
    return box;
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) CircleShape(com.badlogic.gdx.physics.box2d.CircleShape) Vector2(com.badlogic.gdx.math.Vector2) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) Body(com.badlogic.gdx.physics.box2d.Body)

Example 35 with Body

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

the class Box2DTest method createBoxes.

private void createBoxes() {
    // 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);
        boxBody.createFixture(boxPoly, 1);
        // add the box to our list of boxes
        boxes.add(boxBody);
    }
    // we are done, all that's left is disposing the boxPoly
    boxPoly.dispose();
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) Body(com.badlogic.gdx.physics.box2d.Body) MouseJoint(com.badlogic.gdx.physics.box2d.joints.MouseJoint)

Aggregations

Body (com.badlogic.gdx.physics.box2d.Body)49 Vector2 (com.badlogic.gdx.math.Vector2)21 BodyDef (com.badlogic.gdx.physics.box2d.BodyDef)16 PolygonShape (com.badlogic.gdx.physics.box2d.PolygonShape)14 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)7 BasePhysicsManager (com.ilargia.games.entitas.egdx.base.managers.BasePhysicsManager)6 GameEntity (com.indignado.games.states.game.gen.GameEntity)6 BodyBuilder (ilargia.egdx.util.BodyBuilder)6 GameEntity (ilargia.egdx.logicbricks.gen.game.GameEntity)5 FixtureDefBuilder (ilargia.egdx.util.FixtureDefBuilder)5 Entity (com.artemis.Entity)4 Texture (com.badlogic.gdx.graphics.Texture)4 Animation (com.badlogic.gdx.graphics.g2d.Animation)4 World (com.badlogic.gdx.physics.box2d.World)4 PhysicsComponent (com.gemserk.commons.artemis.components.PhysicsComponent)4 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)3 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)3