Search in sources :

Example 11 with Body

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

the class RubeLoaderTest method processScene.

/**
    * Builds up world based on info from the scene...
    */
private void processScene() {
    createSpatialsFromRubeImages(mScene);
    createPolySpatialsFromRubeFixtures(mScene);
    mWorld = mScene.getWorld();
    // configure simulation settings
    mVelocityIter = mScene.velocityIterations;
    mPositionIter = mScene.positionIterations;
    if (mScene.stepsPerSecond != 0) {
        mSecondsPerStep = 1f / mScene.stepsPerSecond;
    }
    mWorld.setContactListener(this);
    //
    // example of custom property handling
    //
    Array<Body> bodies = mScene.getBodies();
    if ((bodies != null) && (bodies.size > 0)) {
        for (int i = 0; i < bodies.size; i++) {
            Body body = bodies.get(i);
            String gameInfo = (String) mScene.getCustom(body, "GameInfo", null);
            if (gameInfo != null) {
                System.out.println("GameInfo custom property: " + gameInfo);
            }
        }
    }
    // Example of accessing data based on name
    System.out.println("body0 count: " + mScene.getNamed(Body.class, "body0").size);
    System.out.println("fixture0 count: " + mScene.getNamed(Fixture.class, "fixture0").size);
    mScene.printStats();
    testSceneSettings();
    // no longer need any scene references
    mScene.clear();
}
Also used : Fixture(com.badlogic.gdx.physics.box2d.Fixture) Body(com.badlogic.gdx.physics.box2d.Body)

Example 12 with Body

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

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

the class ImageSerializer method read.

@SuppressWarnings("rawtypes")
@Override
public RubeImage read(Json json, JsonValue jsonData, Class type) {
    // Images reference bodies based on indexing in the .json file. -1 means no body reference
    Array<Body> bodies = scene.getBodies();
    RubeImage defaults = RubeDefaults.Image.image;
    RubeImage image = new RubeImage();
    image.angleInRads = json.readValue("angle", float.class, defaults.angleInRads, jsonData);
    int bodyIndex = json.readValue("body", int.class, jsonData);
    if (bodyIndex >= 0) {
        bodyIndex += scene.getCurrentBodyOffset();
        if ((bodies != null) && (bodyIndex < bodies.size)) {
            image.body = bodies.get(bodyIndex);
        } else {
            throw new RuntimeException("RubeImage creation error.  bodies: " + bodies + ", bodyIndex: " + bodyIndex);
        }
    }
    image.center.set(json.readValue("center", Vector2.class, defaults.center, jsonData));
    RubeVertexArray corners = json.readValue("corners", RubeVertexArray.class, jsonData);
    if (corners != null) {
        mTmp.set(corners.x[0], corners.y[0]).sub(corners.x[1], corners.y[1]);
        image.width = mTmp.len();
        mTmp.set(corners.x[1], corners.y[1]).sub(corners.x[2], corners.y[2]);
        image.height = mTmp.len();
    }
    image.file = json.readValue("file", String.class, jsonData);
    image.filter = json.readValue("filter", int.class, defaults.filter, jsonData);
    image.flip = json.readValue("flip", boolean.class, defaults.flip, jsonData);
    image.name = json.readValue("name", String.class, jsonData);
    image.opacity = json.readValue("opacity", float.class, defaults.opacity, jsonData);
    int[] colorArray = json.readValue("colorTint", int[].class, RubeDefaults.Image.colorArray, jsonData);
    image.color.set((float) colorArray[0] / 255, (float) colorArray[1] / 255, (float) colorArray[2] / 255, (float) colorArray[3] / 255);
    image.renderOrder = json.readValue("renderOrder", int.class, defaults.renderOrder, jsonData);
    image.scale = json.readValue("scale", float.class, defaults.scale, jsonData);
    scene.parseCustomProperties(json, image, jsonData);
    String name = json.readValue("name", String.class, jsonData);
    if (name != null) {
        scene.putNamed(name, image);
    }
    return image;
}
Also used : Vector2(com.badlogic.gdx.math.Vector2) RubeVertexArray(com.gushikustudios.rube.loader.serializers.utils.RubeVertexArray) Body(com.badlogic.gdx.physics.box2d.Body) RubeImage(com.gushikustudios.rube.loader.serializers.utils.RubeImage)

Example 14 with Body

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

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

the class PhysicsContactListener method endContact.

@Override
public void endContact(Contact contact) {
    Body bodyA = contact.getFixtureA().getBody();
    Body bodyB = contact.getFixtureB().getBody();
    Entity entityA = (Entity) bodyA.getUserData();
    Entity entityB = (Entity) bodyB.getUserData();
    removeBodyFromContacts(entityA, contact, true);
    removeBodyFromContacts(entityB, contact, false);
}
Also used : Entity(com.artemis.Entity) Body(com.badlogic.gdx.physics.box2d.Body)

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