Search in sources :

Example 1 with Joint

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

the class JointSerializer method read.

@SuppressWarnings("rawtypes")
@Override
public Joint read(Json json, JsonValue jsonData, Class type) {
    if (bodies == null || world == null)
        return null;
    int indexA = json.readValue("bodyA", int.class, bodies.size, jsonData);
    int indexB = json.readValue("bodyB", int.class, bodies.size, jsonData);
    if (indexA >= bodies.size || indexB >= bodies.size)
        return null;
    Joint joint = null;
    JointDef jointDef = null;
    String jointType = json.readValue("type", String.class, jsonData);
    if (jointType == null)
        return null;
    // First pass
    if (joints == null && !jointType.equals("gear")) {
        if (jointType.equals("revolute")) {
            jointDef = json.readValue(RevoluteJointDef.class, jsonData);
        } else if (jointType.equals("prismatic")) {
            jointDef = json.readValue(PrismaticJointDef.class, jsonData);
        } else if (jointType.equals("distance")) {
            jointDef = json.readValue(DistanceJointDef.class, jsonData);
        } else if (jointType.equals("pulley")) {
            jointDef = json.readValue(PulleyJointDef.class, jsonData);
        } else if (jointType.equals("mouse")) {
            jointDef = json.readValue(MouseJointDef.class, jsonData);
        } else if (jointType.equals("wheel")) {
            jointDef = json.readValue(WheelJointDef.class, jsonData);
        } else if (jointType.equals("weld")) {
            jointDef = json.readValue(WeldJointDef.class, jsonData);
        } else if (jointType.equals("friction")) {
            jointDef = json.readValue(FrictionJointDef.class, jsonData);
        } else if (jointType.equals("rope")) {
            jointDef = json.readValue(RopeJointDef.class, jsonData);
        }
    } else if (// Second pass
    joints != null && jointType.equals("gear")) {
        jointDef = json.readValue(GearJointDef.class, jsonData);
    }
    if (jointDef != null) {
        jointDef.bodyA = bodies.get(indexA);
        jointDef.bodyB = bodies.get(indexB);
        jointDef.collideConnected = json.readValue("collideConnected", boolean.class, RubeDefaults.Joint.collideConnected, jsonData);
        joint = world.createJoint(jointDef);
        if (jointType.equals("mouse")) {
            ((MouseJoint) joint).setTarget(mouseJointDefSerializer.target);
        }
    }
    scene.parseCustomProperties(json, joint, jsonData);
    String name = json.readValue("name", String.class, jsonData);
    if (name != null) {
        scene.putNamed(name, joint);
    }
    return joint;
}
Also used : PrismaticJointDef(com.badlogic.gdx.physics.box2d.joints.PrismaticJointDef) PulleyJointDef(com.badlogic.gdx.physics.box2d.joints.PulleyJointDef) FrictionJointDef(com.badlogic.gdx.physics.box2d.joints.FrictionJointDef) MouseJointDef(com.badlogic.gdx.physics.box2d.joints.MouseJointDef) RevoluteJointDef(com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef) RopeJointDef(com.badlogic.gdx.physics.box2d.joints.RopeJointDef) GearJointDef(com.badlogic.gdx.physics.box2d.joints.GearJointDef) WeldJointDef(com.badlogic.gdx.physics.box2d.joints.WeldJointDef) DistanceJointDef(com.badlogic.gdx.physics.box2d.joints.DistanceJointDef) WheelJointDef(com.badlogic.gdx.physics.box2d.joints.WheelJointDef) JointDef(com.badlogic.gdx.physics.box2d.JointDef) RopeJointDef(com.badlogic.gdx.physics.box2d.joints.RopeJointDef) DistanceJointDef(com.badlogic.gdx.physics.box2d.joints.DistanceJointDef) Joint(com.badlogic.gdx.physics.box2d.Joint) MouseJoint(com.badlogic.gdx.physics.box2d.joints.MouseJoint) MouseJoint(com.badlogic.gdx.physics.box2d.joints.MouseJoint) WeldJointDef(com.badlogic.gdx.physics.box2d.joints.WeldJointDef) Joint(com.badlogic.gdx.physics.box2d.Joint) MouseJoint(com.badlogic.gdx.physics.box2d.joints.MouseJoint) MouseJointDef(com.badlogic.gdx.physics.box2d.joints.MouseJointDef) RevoluteJointDef(com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef)

Example 2 with Joint

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

Aggregations

Joint (com.badlogic.gdx.physics.box2d.Joint)2 Vector2 (com.badlogic.gdx.math.Vector2)1 Body (com.badlogic.gdx.physics.box2d.Body)1 JointDef (com.badlogic.gdx.physics.box2d.JointDef)1 World (com.badlogic.gdx.physics.box2d.World)1 DistanceJointDef (com.badlogic.gdx.physics.box2d.joints.DistanceJointDef)1 FrictionJointDef (com.badlogic.gdx.physics.box2d.joints.FrictionJointDef)1 GearJointDef (com.badlogic.gdx.physics.box2d.joints.GearJointDef)1 MouseJoint (com.badlogic.gdx.physics.box2d.joints.MouseJoint)1 MouseJointDef (com.badlogic.gdx.physics.box2d.joints.MouseJointDef)1 PrismaticJointDef (com.badlogic.gdx.physics.box2d.joints.PrismaticJointDef)1 PulleyJointDef (com.badlogic.gdx.physics.box2d.joints.PulleyJointDef)1 RevoluteJointDef (com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef)1 RopeJointDef (com.badlogic.gdx.physics.box2d.joints.RopeJointDef)1 WeldJointDef (com.badlogic.gdx.physics.box2d.joints.WeldJointDef)1 WheelJointDef (com.badlogic.gdx.physics.box2d.joints.WheelJointDef)1 Array (com.badlogic.gdx.utils.Array)1 RubeImage (com.gushikustudios.rube.loader.serializers.utils.RubeImage)1