Search in sources :

Example 36 with Body

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

Example 37 with Body

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

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

Example 39 with Body

use of com.badlogic.gdx.physics.box2d.Body in project Entitas-Java by Rubentxu.

the class TextureRendererSystem method render.

@Override
public void render() {
    batch.setProjectionMatrix(cam.combined);
    batch.begin();
    for (GameEntity e : groupTextureView.getEntities()) {
        TextureView view = e.getTextureView();
        Body body = e.getRigidBody().body;
        processTextureFlip(view);
        batch.setColor(1f, 1f, 1f, 1f);
        if (view.opacity < 1)
            batch.setColor(1f, 1f, 1f, view.opacity);
        batch.draw(view.texture, body.getPosition().x - view.bounds.extentsX, body.getPosition().y - view.bounds.extentsY, view.bounds.extentsX, view.bounds.extentsY, view.bounds.extentsX * 2, view.bounds.extentsY * 2, 1, 1, MathUtils.radiansToDegrees * body.getTransform().getRotation());
    }
    for (ActuatorEntity e : groupEffect.getEntities()) {
        if (e.getLink().isOpen) {
            ParticleEffectActuator effectActuator = e.getParticleEffectActuator();
            effectActuator.particleEffect.draw(batch);
        }
    }
    batch.end();
}
Also used : GameEntity(ilargia.egdx.logicbricks.gen.game.GameEntity) ParticleEffectActuator(ilargia.egdx.logicbricks.component.actuator.ParticleEffectActuator) TextureView(ilargia.egdx.logicbricks.component.game.TextureView) Body(com.badlogic.gdx.physics.box2d.Body) ActuatorEntity(ilargia.egdx.logicbricks.gen.actuator.ActuatorEntity)

Example 40 with Body

use of com.badlogic.gdx.physics.box2d.Body in project Entitas-Java by Rubentxu.

the class RigidBodySystem method execute.

@Override
protected void execute(List<GameEntity> entities) {
    for (GameEntity e : entities) {
        Body body = e.getRigidBody().body;
        body.setUserData(e.getCreationIndex());
    }
}
Also used : GameEntity(ilargia.egdx.logicbricks.gen.game.GameEntity) Body(com.badlogic.gdx.physics.box2d.Body)

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