Search in sources :

Example 1 with BodyDef

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

the class BodySerializer method read.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Body read(Json json, JsonValue jsonData, Class type) {
    if (world == null)
        return null;
    BodyDef defaults = RubeDefaults.Body.definition;
    int bodyType = json.readValue("type", int.class, defaults.type.getValue(), jsonData);
    if (bodyType == BodyType.DynamicBody.getValue())
        def.type = BodyType.DynamicBody;
    else if (bodyType == BodyType.KinematicBody.getValue())
        def.type = BodyType.KinematicBody;
    else
        def.type = BodyType.StaticBody;
    def.position.set(json.readValue("position", Vector2.class, defaults.position, jsonData));
    def.linearVelocity.set(json.readValue("linearVelocity", Vector2.class, defaults.linearVelocity, jsonData));
    def.angle = json.readValue("angle", float.class, defaults.angle, jsonData);
    def.angularVelocity = json.readValue("angularVelocity", float.class, defaults.angularVelocity, jsonData);
    def.linearDamping = json.readValue("linearDamping", float.class, defaults.linearDamping, jsonData);
    def.angularDamping = json.readValue("angularDamping", float.class, defaults.angularDamping, jsonData);
    def.gravityScale = json.readValue("gravityScale", float.class, defaults.gravityScale, jsonData);
    def.allowSleep = json.readValue("allowSleep", boolean.class, defaults.allowSleep, jsonData);
    def.awake = json.readValue("awake", boolean.class, defaults.awake, jsonData);
    def.fixedRotation = json.readValue("fixedRotation", boolean.class, defaults.fixedRotation, jsonData);
    def.bullet = json.readValue("bullet", boolean.class, defaults.bullet, jsonData);
    def.active = json.readValue("active", boolean.class, defaults.active, jsonData);
    Body body = world.createBody(def);
    if (def.type == BodyType.DynamicBody) {
        Vector2 center = json.readValue("massData-center", Vector2.class, jsonData);
        float mass = json.readValue("massData-mass", float.class, 0.0f, jsonData);
        float I = json.readValue("massData-I", float.class, 0.0f, jsonData);
        if (center != null) {
            MassData massData = new MassData();
            massData.center.set(center);
            massData.mass = mass;
            massData.I = I;
            if (massData.mass != 0.0f || massData.I != 0.0f || massData.center.x != 0.0f || massData.center.y != 0.0f)
                body.setMassData(massData);
        }
    }
    scene.parseCustomProperties(json, body, jsonData);
    String name = json.readValue("name", String.class, jsonData);
    if (name != null) {
        scene.putNamed(name, body);
    }
    fixtureSerializer.setBody(body);
    scene.addFixtures(json.readValue("fixture", Array.class, Fixture.class, jsonData));
    return body;
}
Also used : Array(com.badlogic.gdx.utils.Array) Vector2(com.badlogic.gdx.math.Vector2) MassData(com.badlogic.gdx.physics.box2d.MassData) Fixture(com.badlogic.gdx.physics.box2d.Fixture) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) Body(com.badlogic.gdx.physics.box2d.Body)

Example 2 with BodyDef

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

the class KinematicBodyTest method create.

public void create() {
    cam = new OrthographicCamera(48, 32);
    cam.position.set(0, 15, 0);
    renderer = new Box2DDebugRenderer();
    world = new World(new Vector2(0, -10), true);
    Body body = world.createBody(new BodyDef());
    CircleShape shape = new CircleShape();
    shape.setRadius(1f);
    MassData mass = new MassData();
    mass.mass = 1f;
    body.setMassData(mass);
    body.setFixedRotation(true);
    body.setType(BodyType.KinematicBody);
    body.createFixture(shape, 1);
    body.setBullet(true);
    body.setTransform(new Vector2(0, 0), body.getAngle());
    body.setLinearVelocity(new Vector2(50f, 0));
}
Also used : Box2DDebugRenderer(com.badlogic.gdx.physics.box2d.Box2DDebugRenderer) Vector2(com.badlogic.gdx.math.Vector2) CircleShape(com.badlogic.gdx.physics.box2d.CircleShape) MassData(com.badlogic.gdx.physics.box2d.MassData) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) World(com.badlogic.gdx.physics.box2d.World) Body(com.badlogic.gdx.physics.box2d.Body) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef)

Example 3 with BodyDef

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

the class Box2DCharacterControllerTest method createCircle.

Body createCircle(BodyType type, float radius, float density) {
    BodyDef def = new BodyDef();
    def.type = type;
    Body box = world.createBody(def);
    CircleShape poly = new CircleShape();
    poly.setRadius(radius);
    box.createFixture(poly, density);
    poly.dispose();
    return box;
}
Also used : CircleShape(com.badlogic.gdx.physics.box2d.CircleShape) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) Body(com.badlogic.gdx.physics.box2d.Body)

Example 4 with BodyDef

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

the class Box2DTest method createPhysicsWorld.

private void createPhysicsWorld() {
    // we instantiate a new World with a proper gravity vector
    // and tell it to sleep when possible.
    world = new World(new Vector2(0, -10), true);
    float[] vertices = { -0.07421887f, -0.16276085f, -0.12109375f, -0.22786504f, -0.157552f, -0.7122401f, 0.04296875f, -0.7122401f, 0.110677004f, -0.6419276f, 0.13151026f, -0.49869835f, 0.08984375f, -0.3190109f };
    PolygonShape shape = new PolygonShape();
    shape.set(vertices);
    // 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 memory management.
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = groundPoly;
    fixtureDef.filter.groupIndex = 0;
    groundBody.createFixture(fixtureDef);
    groundPoly.dispose();
    // We also create a simple ChainShape we put above our
    // ground polygon for extra funkyness.
    ChainShape chainShape = new ChainShape();
    chainShape.createLoop(new Vector2[] { new Vector2(-10, 10), new Vector2(-10, 5), new Vector2(10, 5), new Vector2(10, 11) });
    BodyDef chainBodyDef = new BodyDef();
    chainBodyDef.type = BodyType.StaticBody;
    Body chainBody = world.createBody(chainBodyDef);
    chainBody.createFixture(chainShape, 0);
    chainShape.dispose();
    createBoxes();
    // You can savely ignore the rest of this method :)
    world.setContactListener(new ContactListener() {

        @Override
        public void beginContact(Contact contact) {
        // System.out.println("begin contact");
        }

        @Override
        public void endContact(Contact contact) {
        // System.out.println("end contact");
        }

        @Override
        public void preSolve(Contact contact, Manifold oldManifold) {
        // Manifold.ManifoldType type = oldManifold.getType();
        // Vector2 localPoint = oldManifold.getLocalPoint();
        // Vector2 localNormal = oldManifold.getLocalNormal();
        // int pointCount = oldManifold.getPointCount();
        // ManifoldPoint[] points = oldManifold.getPoints();
        // System.out.println("pre solve, " + type +
        // ", point: " + localPoint +
        // ", local normal: " + localNormal +
        // ", #points: " + pointCount +
        // ", [" + points[0] + ", " + points[1] + "]");
        }

        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) {
        // float[] ni = impulse.getNormalImpulses();
        // float[] ti = impulse.getTangentImpulses();
        // System.out.println("post solve, normal impulses: " + ni[0] + ", " + ni[1] + ", tangent impulses: " + ti[0] + ", " + ti[1]);
        }
    });
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) World(com.badlogic.gdx.physics.box2d.World) Contact(com.badlogic.gdx.physics.box2d.Contact) WorldManifold(com.badlogic.gdx.physics.box2d.WorldManifold) Manifold(com.badlogic.gdx.physics.box2d.Manifold) Vector2(com.badlogic.gdx.math.Vector2) ContactImpulse(com.badlogic.gdx.physics.box2d.ContactImpulse) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) ChainShape(com.badlogic.gdx.physics.box2d.ChainShape) Body(com.badlogic.gdx.physics.box2d.Body) FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef) ContactListener(com.badlogic.gdx.physics.box2d.ContactListener)

Example 5 with BodyDef

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

the class VerticalStack method createWorld.

@Override
protected void createWorld(World world) {
    {
        BodyDef bd = new BodyDef();
        Body ground = world.createBody(bd);
        EdgeShape shape = new EdgeShape();
        shape.set(new Vector2(-40, 0), new Vector2(40, 0));
        ground.createFixture(shape, 0.0f);
        shape.set(new Vector2(20, 0), new Vector2(20, 20));
        ground.createFixture(shape, 0);
        shape.dispose();
    }
    float[] xs = { 0, -10, -5, 5, 10 };
    for (int j = 0; j < e_columnCount; j++) {
        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 < e_rowCount; i++) {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DynamicBody;
            int n = j * e_rowCount + i;
            m_indices[n] = n;
            float x = 0;
            bd.position.set(xs[j] + x, 0.752f + 1.54f * i);
            Body body = world.createBody(bd);
            body.setUserData(n);
            m_bodies[n] = body;
            body.createFixture(fd);
        }
        shape.dispose();
    }
    m_bullet = null;
}
Also used : EdgeShape(com.badlogic.gdx.physics.box2d.EdgeShape) PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) Vector2(com.badlogic.gdx.math.Vector2) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) Body(com.badlogic.gdx.physics.box2d.Body) FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef)

Aggregations

BodyDef (com.badlogic.gdx.physics.box2d.BodyDef)18 Body (com.badlogic.gdx.physics.box2d.Body)15 Vector2 (com.badlogic.gdx.math.Vector2)13 PolygonShape (com.badlogic.gdx.physics.box2d.PolygonShape)11 FixtureDef (com.badlogic.gdx.physics.box2d.FixtureDef)9 CircleShape (com.badlogic.gdx.physics.box2d.CircleShape)8 EdgeShape (com.badlogic.gdx.physics.box2d.EdgeShape)7 World (com.badlogic.gdx.physics.box2d.World)3 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)2 Box2DDebugRenderer (com.badlogic.gdx.physics.box2d.Box2DDebugRenderer)2 MassData (com.badlogic.gdx.physics.box2d.MassData)2 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)1 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)1 ChainShape (com.badlogic.gdx.physics.box2d.ChainShape)1 Contact (com.badlogic.gdx.physics.box2d.Contact)1 ContactImpulse (com.badlogic.gdx.physics.box2d.ContactImpulse)1 ContactListener (com.badlogic.gdx.physics.box2d.ContactListener)1 Fixture (com.badlogic.gdx.physics.box2d.Fixture)1 Manifold (com.badlogic.gdx.physics.box2d.Manifold)1 Transform (com.badlogic.gdx.physics.box2d.Transform)1