Search in sources :

Example 16 with PolygonShape

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

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

Example 18 with PolygonShape

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

the class Box2DTest method create.

@Override
public void create() {
    float w = Gdx.graphics.getWidth();
    w *= 2;
    float h = Gdx.graphics.getHeight();
    h *= 2;
    ratio = h / w;
    Gdx.app.getGraphics().setWindowedMode((int) w, (int) h);
    mX = (float) WIDTH / w;
    mY = (float) HEIGHT / h;
    Box2D.init();
    world = new World(new Vector2(0, -9.81f), true);
    batch = new SpriteBatch();
    sprites = new Sprite[250];
    bodies = new Body[sprites.length];
    float k, j;
    for (int i = 0; i < sprites.length; i++) {
        if (i < 50) {
            k = 0;
            j = 1;
        } else if (i < 100) {
            k = 50 * sprites[i - 1].getWidth() + sprites[i - 1].getWidth() / 2;
            j = 2;
        } else if (i < 150) {
            k = 100 * sprites[i - 1].getWidth() + sprites[i - 1].getWidth() / 2;
            j = 3;
        } else if (i < 200) {
            k = 150 * sprites[i - 1].getWidth() + sprites[i - 1].getWidth() / 2;
            j = 4;
        } else {
            k = 200 * sprites[i - 1].getWidth() + sprites[i - 1].getWidth() / 2;
            j = 5;
        }
        sprites[i] = new Sprite(new Texture(Gdx.files.classpath("tests/badlogic.jpg")));
        sprites[i].setSize(2, 2);
        sprites[i].setOriginCenter();
        BodyDef bdef = new BodyDef();
        bdef.type = BodyDef.BodyType.DynamicBody;
        bdef.position.set(i * sprites[i].getWidth() + sprites[i].getWidth() / 2 - k, 15 * j);
        bodies[i] = world.createBody(bdef);
        PolygonShape poly = new PolygonShape();
        poly.setAsBox(sprites[i].getWidth() / 2, sprites[i].getHeight() / 2);
        FixtureDef fdef = new FixtureDef();
        fdef.shape = poly;
        fdef.restitution = .2f;
        fdef.density = 1f;
        bodies[i].createFixture(fdef);
        poly.dispose();
    }
    BodyDef bdef = new BodyDef();
    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set(WIDTH / 2, -5);
    Body b = world.createBody(bdef);
    PolygonShape poly = new PolygonShape();
    poly.setAsBox(50, 5);
    b.createFixture(poly, 0);
    bdef = new BodyDef();
    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set(-5, HEIGHT / 2);
    b = world.createBody(bdef);
    poly = new PolygonShape();
    poly.setAsBox(5, 50);
    b.createFixture(poly, 0);
    bdef = new BodyDef();
    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set(WIDTH + 5, (HEIGHT * ratio) / 2);
    b = world.createBody(bdef);
    poly = new PolygonShape();
    poly.setAsBox(5, 50);
    b.createFixture(poly, 0);
    bdef = new BodyDef();
    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set(WIDTH / 2, HEIGHT * ratio + 5);
    b = world.createBody(bdef);
    poly = new PolygonShape();
    poly.setAsBox(50, 5);
    b.createFixture(poly, 0);
    poly.dispose();
    c = new OrthographicCamera(WIDTH, HEIGHT * ratio);
    c.position.set(c.viewportWidth / 2, c.viewportHeight / 2, 0);
    c.update();
    batch.setProjectionMatrix(c.combined);
    debugRenderer = new Box2DDebugRenderer();
    console = new GUIConsole(false);
    cExec = new MyCommandExecutor();
    console.setCommandExecutor(cExec);
    // set to 'Z' to demonstrate that it works with binds other than the
    // default
    console.setDisplayKeyID(Input.Keys.Z);
    console.setVisible(true);
    // test multiple resets with nested multiplexers
    InputMultiplexer im1 = new InputMultiplexer();
    im1.addProcessor(new Stage());
    im1.addProcessor(new Stage());
    InputMultiplexer im2 = new InputMultiplexer();
    im2.addProcessor(new Stage());
    im2.addProcessor(new Stage());
    im1.addProcessor(im2);
    Gdx.input.setInputProcessor(im1);
    console.setMaxEntries(16);
    console.resetInputProcessing();
    // console already present, logged to consoles
    console.resetInputProcessing();
    console.setSizePercent(100, 33);
    console.setPositionPercent(0, 67);
}
Also used : GUIConsole(com.strongjoshua.console.GUIConsole) PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) World(com.badlogic.gdx.physics.box2d.World) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) Texture(com.badlogic.gdx.graphics.Texture) InputMultiplexer(com.badlogic.gdx.InputMultiplexer) Box2DDebugRenderer(com.badlogic.gdx.physics.box2d.Box2DDebugRenderer) Vector2(com.badlogic.gdx.math.Vector2) Stage(com.badlogic.gdx.scenes.scene2d.Stage) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) Body(com.badlogic.gdx.physics.box2d.Body) FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef)

Example 19 with PolygonShape

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

the class RubeLoaderTest method create.

@Override
public void create() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    Gdx.input.setInputProcessor(this);
    mB2Controllers = new Array<B2Controller>();
    mCamPos = new Vector3();
    mCurrentPos = new Vector3();
    camera = new OrthographicCamera(100, 100 * h / w);
    camera.position.set(50, 50, 0);
    camera.zoom = 1.8f;
    camera.update();
    loader = new RubeSceneLoader();
    scene = loader.loadScene(Gdx.files.internal("data/palmcontrollers.json"));
    debugRender = new Box2DDebugRenderer();
    batch = new SpriteBatch();
    polygonBatch = new PolygonSpriteBatch();
    textureMap = new HashMap<String, Texture>();
    textureRegionMap = new HashMap<Texture, TextureRegion>();
    createSpatialsFromRubeImages(scene);
    createPolySpatialsFromRubeFixtures(scene);
    mWorld = scene.getWorld();
    // configure simulation settings
    mVelocityIter = scene.velocityIterations;
    mPositionIter = scene.positionIterations;
    if (scene.stepsPerSecond != 0) {
        mSecondsPerStep = 1f / scene.stepsPerSecond;
    }
    mWorld.setContactListener(this);
    //
    // example of custom property handling
    //
    Array<Body> bodies = scene.getBodies();
    if ((bodies != null) && (bodies.size > 0)) {
        for (int i = 0; i < bodies.size; i++) {
            Body body = bodies.get(i);
            String gameInfo = (String) scene.getCustom(body, "GameInfo", null);
            if (gameInfo != null) {
                System.out.println("GameInfo custom property: " + gameInfo);
            }
        }
    }
    // Instantiate any controllers that are in the scene
    Array<Fixture> fixtures = scene.getFixtures();
    if ((fixtures != null) && (fixtures.size > 0)) {
        for (int i = 0; i < fixtures.size; i++) {
            Fixture fixture = fixtures.get(i);
            int controllerType = (Integer) scene.getCustom(fixture, "ControllerType", 0);
            switch(controllerType) {
                case B2Controller.BUOYANCY_CONTROLLER:
                    // only allow polygon buoyancy controllers for now..
                    if (fixture.getShape().getType() == Shape.Type.Polygon) {
                        float bodyHeight = fixture.getBody().getPosition().y;
                        // B2BuoyancyController b2c = new B2BuoyancyController();
                        // need to calculate the fluid surface height for the buoyancy controller
                        PolygonShape shape = (PolygonShape) fixture.getShape();
                        shape.getVertex(0, mTmp);
                        // initialize the height, transforming to 'world'
                        float maxHeight = mTmp.y + bodyHeight;
                        // find the maxHeight
                        for (int j = 1; j < shape.getVertexCount(); j++) {
                            shape.getVertex(j, mTmp);
                            // transform to world coordinates
                            maxHeight = Math.max(maxHeight, mTmp.y + bodyHeight);
                        }
                        B2BuoyancyController b2c = new B2BuoyancyController(// assume up
                        B2BuoyancyController.DEFAULT_SURFACE_NORMAL, (Vector2) scene.getCustom(fixture, "ControllerVelocity", B2BuoyancyController.DEFAULT_FLUID_VELOCITY), mWorld.getGravity(), maxHeight, fixture.getDensity(), (Float) scene.getCustom(fixture, "LinearDrag", B2BuoyancyController.DEFAULT_LINEAR_DRAG), (Float) scene.getCustom(fixture, "AngularDrag", B2BuoyancyController.DEFAULT_ANGULAR_DRAG));
                        // reference back to the controller from the fixture (see
                        fixture.setUserData(b2c);
                        // beginContact/endContact)
                        // add it to the list so it can be stepped later
                        mB2Controllers.add(b2c);
                    }
                    break;
                case B2Controller.GRAVITY_CONTROLLER:
                    {
                        B2GravityController b2c = new B2GravityController();
                        b2c = new B2GravityController((Vector2) scene.getCustom(fixture, "ControllerVelocity", B2GravityController.DEFAULT_GRAVITY));
                        fixture.setUserData(b2c);
                        mB2Controllers.add(b2c);
                    }
                    break;
            }
        }
    }
    scene.printStats();
    // no longer need any scene references
    scene.clear();
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) B2BuoyancyController(com.gushikustudios.box2d.controllers.B2BuoyancyController) Vector3(com.badlogic.gdx.math.Vector3) B2Controller(com.gushikustudios.box2d.controllers.B2Controller) PolygonSpriteBatch(com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) PolygonSpriteBatch(com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch) Texture(com.badlogic.gdx.graphics.Texture) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) Box2DDebugRenderer(com.badlogic.gdx.physics.box2d.Box2DDebugRenderer) RubeSceneLoader(com.gushikustudios.rube.loader.RubeSceneLoader) B2GravityController(com.gushikustudios.box2d.controllers.B2GravityController) Fixture(com.badlogic.gdx.physics.box2d.Fixture) 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