Search in sources :

Example 26 with Body

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

the class AntiGravitySystem method process.

@Override
protected void process(Entity e) {
    PhysicsComponent physicsComponent = Components.getPhysicsComponent(e);
    Body body = physicsComponent.getPhysics().getBody();
    Vector2 gravity = body.getWorld().getGravity();
    tmp.set(gravity).mul(-body.getMass());
    body.applyForceToCenter(tmp);
}
Also used : PhysicsComponent(com.gemserk.commons.artemis.components.PhysicsComponent) Vector2(com.badlogic.gdx.math.Vector2) Body(com.badlogic.gdx.physics.box2d.Body)

Example 27 with Body

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

the class BodyBuilder method build.

public Body build(boolean disposeShapes) {
    Body body = world.createBody(bodyDef);
    for (int i = 0; i < fixtureDefs.size(); i++) {
        FixtureDef fixtureDef = fixtureDefs.get(i);
        Fixture fixture = body.createFixture(fixtureDef);
        fixture.setUserData(fixtureUserDatas.get(i));
    }
    if (massSet) {
        MassData bodyMassData = body.getMassData();
        // massData.center.set(position);
        massData.center.set(bodyMassData.center);
        // massData.I = bodyMassData.I;
        body.setMassData(massData);
    }
    // MassData massData = body.getMassData();
    // massData.mass = mass;
    // massData.I = 1f;
    body.setUserData(userData);
    body.setTransform(position, angle);
    reset(disposeShapes);
    return body;
}
Also used : MassData(com.badlogic.gdx.physics.box2d.MassData) Fixture(com.badlogic.gdx.physics.box2d.Fixture) Body(com.badlogic.gdx.physics.box2d.Body) FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef)

Example 28 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 29 with Body

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

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

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