Search in sources :

Example 6 with FixtureDef

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

the class FixtureDefBuilder method build.

public FixtureDef build() {
    FixtureDef fixtureDef = this.fixtureDef;
    reset();
    return fixtureDef;
}
Also used : FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef)

Example 7 with FixtureDef

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

the class CreateNearSensorSystem method execute.

@Override
protected void execute(List<SensorEntity> entities) {
    for (SensorEntity e : entities) {
        GameEntity gameEntity = Indexed.getInteractiveEntity(e.getLink().ownerEntity);
        RigidBody rigidBody = gameEntity.getRigidBody();
        NearSensor nearSensor = e.getNearSensor();
        if (nearSensor.distance > 0) {
            FixtureDef nearFixture = bodyBuilder.fixtureDefBuilder().circleShape(nearSensor.distance).sensor().build();
            rigidBody.body.createFixture(nearFixture).setUserData("NearSensor");
            if (nearSensor.resetDistance > nearSensor.distance) {
                FixtureDef nearResetFixture = bodyBuilder.fixtureDefBuilder().circleShape(nearSensor.resetDistance).sensor().build();
                rigidBody.body.createFixture(nearResetFixture).setUserData("ResetNearSensor");
            }
        }
    }
}
Also used : GameEntity(ilargia.egdx.logicbricks.gen.game.GameEntity) SensorEntity(ilargia.egdx.logicbricks.gen.sensor.SensorEntity) RigidBody(ilargia.egdx.logicbricks.component.game.RigidBody) FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef) NearSensor(ilargia.egdx.logicbricks.component.sensor.NearSensor)

Example 8 with FixtureDef

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

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

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

the class VerticalStack method keyDown.

@Override
public boolean keyDown(int keyCode) {
    if (keyCode == Input.Keys.COMMA) {
        if (m_bullet != null) {
            world.destroyBody(m_bullet);
            m_bullet = null;
        }
        {
            CircleShape shape = new CircleShape();
            shape.setRadius(0.25f);
            FixtureDef fd = new FixtureDef();
            fd.shape = shape;
            fd.density = 20.0f;
            fd.restitution = 0.05f;
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DynamicBody;
            bd.bullet = true;
            bd.position.set(-31, 5);
            m_bullet = world.createBody(bd);
            m_bullet.createFixture(fd);
            m_bullet.setLinearVelocity(new Vector2(400, 0));
        }
    }
    return false;
}
Also used : CircleShape(com.badlogic.gdx.physics.box2d.CircleShape) Vector2(com.badlogic.gdx.math.Vector2) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef)

Aggregations

FixtureDef (com.badlogic.gdx.physics.box2d.FixtureDef)15 Vector2 (com.badlogic.gdx.math.Vector2)10 BodyDef (com.badlogic.gdx.physics.box2d.BodyDef)10 Body (com.badlogic.gdx.physics.box2d.Body)9 PolygonShape (com.badlogic.gdx.physics.box2d.PolygonShape)9 EdgeShape (com.badlogic.gdx.physics.box2d.EdgeShape)7 CircleShape (com.badlogic.gdx.physics.box2d.CircleShape)5 ChainShape (com.badlogic.gdx.physics.box2d.ChainShape)2 Fixture (com.badlogic.gdx.physics.box2d.Fixture)2 World (com.badlogic.gdx.physics.box2d.World)2 RigidBody (ilargia.egdx.logicbricks.component.game.RigidBody)2 GameEntity (ilargia.egdx.logicbricks.gen.game.GameEntity)2 SensorEntity (ilargia.egdx.logicbricks.gen.sensor.SensorEntity)2 InputMultiplexer (com.badlogic.gdx.InputMultiplexer)1 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)1 Texture (com.badlogic.gdx.graphics.Texture)1 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)1 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)1 Box2DDebugRenderer (com.badlogic.gdx.physics.box2d.Box2DDebugRenderer)1 Contact (com.badlogic.gdx.physics.box2d.Contact)1