Search in sources :

Example 1 with World

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

the class WorldSerializer method read.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public World read(Json json, JsonValue jsonData, Class type) {
    World world = scene.getWorld();
    if (world == null) {
        boolean allowSleep = json.readValue("allowSleep", boolean.class, RubeDefaults.World.allowSleep, jsonData);
        boolean autoClearForces = json.readValue("autoClearForces", boolean.class, RubeDefaults.World.autoClearForces, jsonData);
        boolean continuousPhysics = json.readValue("continuousPhysics", boolean.class, RubeDefaults.World.continuousPhysics, jsonData);
        boolean warmStarting = json.readValue("warmStarting", boolean.class, RubeDefaults.World.warmStarting, jsonData);
        Vector2 gravity = json.readValue("gravity", Vector2.class, RubeDefaults.World.gravity, jsonData);
        world = new World(gravity, allowSleep);
        world.setAutoClearForces(autoClearForces);
        world.setContinuousPhysics(continuousPhysics);
        world.setWarmStarting(warmStarting);
    }
    // else ignore world settings and use the ones that were previously loaded
    scene.parseCustomProperties(json, world, jsonData);
    // Bodies
    bodySerializer.setWorld(world);
    Array<Body> bodies = json.readValue("body", Array.class, Body.class, jsonData);
    if (bodies != null) {
        if (scene.getBodies() == null) {
            scene.setBodies(bodies);
        } else {
            scene.addBodies(bodies);
        }
    }
    // Joints
    // joints are done in two passes because gear joints reference other joints
    // First joint pass
    jointSerializer.init(world, bodies, null);
    Array<Joint> joints = json.readValue("joint", Array.class, Joint.class, jsonData);
    if (joints != null) {
        if (scene.getJoints() == null) {
            scene.setJoints(joints);
        } else {
            scene.getJoints().addAll(joints);
        }
    }
    // Second joint pass
    jointSerializer.init(world, bodies, joints);
    joints = json.readValue("joint", Array.class, Joint.class, jsonData);
    // Images
    Array<RubeImage> images = json.readValue("image", Array.class, RubeImage.class, jsonData);
    if (images != null) {
        if (scene.getImages() == null) {
            scene.setImages(images);
        } else {
            scene.getImages().addAll(images);
        }
        for (int i = 0; i < images.size; i++) {
            RubeImage image = images.get(i);
            scene.setMappedImage(image.body, image);
        }
    }
    return world;
}
Also used : Array(com.badlogic.gdx.utils.Array) Vector2(com.badlogic.gdx.math.Vector2) World(com.badlogic.gdx.physics.box2d.World) Joint(com.badlogic.gdx.physics.box2d.Joint) Body(com.badlogic.gdx.physics.box2d.Body) RubeImage(com.gushikustudios.rube.loader.serializers.utils.RubeImage) Joint(com.badlogic.gdx.physics.box2d.Joint)

Example 2 with World

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

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

the class Box2DCharacterControllerTest method create.

@Override
public void create() {
    world = new World(new Vector2(0, -40), true);
    renderer = new Box2DDebugRenderer();
    cam = new OrthographicCamera(28, 20);
    createWorld();
    Gdx.input.setInputProcessor(this);
    batch = new SpriteBatch();
    font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false);
}
Also used : Box2DDebugRenderer(com.badlogic.gdx.physics.box2d.Box2DDebugRenderer) Vector2(com.badlogic.gdx.math.Vector2) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) World(com.badlogic.gdx.physics.box2d.World) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch)

Example 4 with World

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

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

the class Box2DTest method create.

@Override
public void create() {
    // setup the camera. In Box2D we operate on a
    // meter scale, pixels won't do it. So we use
    // an orthographic camera with a viewport of
    // 48 meters in width and 32 meters in height.
    // We also position the camera so that it
    // looks at (0,16) (that's where the middle of the
    // screen will be located).
    camera = new OrthographicCamera(48, 32);
    camera.position.set(0, 15, 0);
    // create the debug renderer
    renderer = new Box2DDebugRenderer();
    // create the world
    world = new World(new Vector2(0, -10), true);
    // we also need an invisible zero size ground body
    // to which we can connect the mouse joint
    BodyDef bodyDef = new BodyDef();
    groundBody = world.createBody(bodyDef);
    // call abstract method to populate the world
    createWorld(world);
    batch = new SpriteBatch();
    font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false);
}
Also used : Box2DDebugRenderer(com.badlogic.gdx.physics.box2d.Box2DDebugRenderer) Vector2(com.badlogic.gdx.math.Vector2) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) World(com.badlogic.gdx.physics.box2d.World) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch)

Aggregations

World (com.badlogic.gdx.physics.box2d.World)7 Vector2 (com.badlogic.gdx.math.Vector2)5 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)3 Body (com.badlogic.gdx.physics.box2d.Body)3 BodyDef (com.badlogic.gdx.physics.box2d.BodyDef)3 Box2DDebugRenderer (com.badlogic.gdx.physics.box2d.Box2DDebugRenderer)3 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)2 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)2 Camera (com.badlogic.gdx.graphics.Camera)1 Batch (com.badlogic.gdx.graphics.g2d.Batch)1 ChainShape (com.badlogic.gdx.physics.box2d.ChainShape)1 CircleShape (com.badlogic.gdx.physics.box2d.CircleShape)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 FixtureDef (com.badlogic.gdx.physics.box2d.FixtureDef)1 Joint (com.badlogic.gdx.physics.box2d.Joint)1 Manifold (com.badlogic.gdx.physics.box2d.Manifold)1 MassData (com.badlogic.gdx.physics.box2d.MassData)1 PolygonShape (com.badlogic.gdx.physics.box2d.PolygonShape)1