Search in sources :

Example 1 with PolygonSpriteBatch

use of com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch in project libgdx by libgdx.

the class PolygonRegionTest method create.

@Override
public void create() {
    texture = new Texture(Gdx.files.internal("data/tree.png"));
    PolygonRegionLoader loader = new PolygonRegionLoader();
    region = loader.load(new TextureRegion(texture), Gdx.files.internal("data/tree.psh"));
    // create a region from an arbitrary set of vertices (a triangle in this case)
    region2 = new PolygonRegion(new TextureRegion(texture), new float[] { 0, 0, 100, 100, 0, 100 }, new short[] { 0, 1, 2 });
    camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.position.x = 0;
    camera.position.y = 0;
    batch = new PolygonSpriteBatch();
    debugRenderer = new PolygonRegionDebugRenderer();
    Gdx.input.setInputProcessor(this);
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) PolygonRegionLoader(com.badlogic.gdx.graphics.g2d.PolygonRegionLoader) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) Texture(com.badlogic.gdx.graphics.Texture) PolygonSpriteBatch(com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch) PolygonRegion(com.badlogic.gdx.graphics.g2d.PolygonRegion)

Example 2 with PolygonSpriteBatch

use of com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch in project libgdx by libgdx.

the class PolygonSpriteTest method create.

@Override
public void create() {
    texture = new Texture(Gdx.files.internal("data/tree.png"));
    PolygonRegionLoader loader = new PolygonRegionLoader();
    region = loader.load(new TextureRegion(texture), Gdx.files.internal("data/tree.psh"));
    renderer = new ShapeRenderer();
    camera = new OrthographicCamera(480, 320);
    camera.position.x = 240;
    camera.position.y = 160;
    camera.update();
    batch = new PolygonSpriteBatch();
    for (int i = 0; i < 50; i++) {
        PolygonSprite sprite = new PolygonSprite(region);
        sprite.setPosition(MathUtils.random(-30, 440), MathUtils.random(-30, 290));
        sprite.setColor(MathUtils.random(), MathUtils.random(), MathUtils.random(), 1.0f);
        sprite.setScale(MathUtils.random(0.5f, 1.5f), MathUtils.random(0.5f, 1.5f));
        sprites.add(sprite);
    }
}
Also used : TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) PolygonRegionLoader(com.badlogic.gdx.graphics.g2d.PolygonRegionLoader) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) PolygonSprite(com.badlogic.gdx.graphics.g2d.PolygonSprite) Texture(com.badlogic.gdx.graphics.Texture) PolygonSpriteBatch(com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch) ShapeRenderer(com.badlogic.gdx.graphics.glutils.ShapeRenderer)

Example 3 with PolygonSpriteBatch

use of com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch in project RubeLoader by tescott.

the class RubeLoaderTest method create.

@Override
public void create() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    Gdx.input.setInputProcessor(this);
    mB2Controllers = new Array<B2Controller>();
    mCamPos = new Vector3();
    mCurrentPos = new Vector3();
    camera = new OrthographicCamera(100, 100 * h / w);
    camera.position.set(50, 50, 0);
    camera.zoom = 1.8f;
    camera.update();
    loader = new RubeSceneLoader();
    scene = loader.loadScene(Gdx.files.internal("data/palmcontrollers.json"));
    debugRender = new Box2DDebugRenderer();
    batch = new SpriteBatch();
    polygonBatch = new PolygonSpriteBatch();
    textureMap = new HashMap<String, Texture>();
    textureRegionMap = new HashMap<Texture, TextureRegion>();
    createSpatialsFromRubeImages(scene);
    createPolySpatialsFromRubeFixtures(scene);
    mWorld = scene.getWorld();
    // configure simulation settings
    mVelocityIter = scene.velocityIterations;
    mPositionIter = scene.positionIterations;
    if (scene.stepsPerSecond != 0) {
        mSecondsPerStep = 1f / scene.stepsPerSecond;
    }
    mWorld.setContactListener(this);
    //
    // example of custom property handling
    //
    Array<Body> bodies = scene.getBodies();
    if ((bodies != null) && (bodies.size > 0)) {
        for (int i = 0; i < bodies.size; i++) {
            Body body = bodies.get(i);
            String gameInfo = (String) scene.getCustom(body, "GameInfo", null);
            if (gameInfo != null) {
                System.out.println("GameInfo custom property: " + gameInfo);
            }
        }
    }
    // Instantiate any controllers that are in the scene
    Array<Fixture> fixtures = scene.getFixtures();
    if ((fixtures != null) && (fixtures.size > 0)) {
        for (int i = 0; i < fixtures.size; i++) {
            Fixture fixture = fixtures.get(i);
            int controllerType = (Integer) scene.getCustom(fixture, "ControllerType", 0);
            switch(controllerType) {
                case B2Controller.BUOYANCY_CONTROLLER:
                    // only allow polygon buoyancy controllers for now..
                    if (fixture.getShape().getType() == Shape.Type.Polygon) {
                        float bodyHeight = fixture.getBody().getPosition().y;
                        // B2BuoyancyController b2c = new B2BuoyancyController();
                        // need to calculate the fluid surface height for the buoyancy controller
                        PolygonShape shape = (PolygonShape) fixture.getShape();
                        shape.getVertex(0, mTmp);
                        // initialize the height, transforming to 'world'
                        float maxHeight = mTmp.y + bodyHeight;
                        // find the maxHeight
                        for (int j = 1; j < shape.getVertexCount(); j++) {
                            shape.getVertex(j, mTmp);
                            // transform to world coordinates
                            maxHeight = Math.max(maxHeight, mTmp.y + bodyHeight);
                        }
                        B2BuoyancyController b2c = new B2BuoyancyController(// assume up
                        B2BuoyancyController.DEFAULT_SURFACE_NORMAL, (Vector2) scene.getCustom(fixture, "ControllerVelocity", B2BuoyancyController.DEFAULT_FLUID_VELOCITY), mWorld.getGravity(), maxHeight, fixture.getDensity(), (Float) scene.getCustom(fixture, "LinearDrag", B2BuoyancyController.DEFAULT_LINEAR_DRAG), (Float) scene.getCustom(fixture, "AngularDrag", B2BuoyancyController.DEFAULT_ANGULAR_DRAG));
                        // reference back to the controller from the fixture (see
                        fixture.setUserData(b2c);
                        // beginContact/endContact)
                        // add it to the list so it can be stepped later
                        mB2Controllers.add(b2c);
                    }
                    break;
                case B2Controller.GRAVITY_CONTROLLER:
                    {
                        B2GravityController b2c = new B2GravityController();
                        b2c = new B2GravityController((Vector2) scene.getCustom(fixture, "ControllerVelocity", B2GravityController.DEFAULT_GRAVITY));
                        fixture.setUserData(b2c);
                        mB2Controllers.add(b2c);
                    }
                    break;
            }
        }
    }
    scene.printStats();
    // no longer need any scene references
    scene.clear();
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) B2BuoyancyController(com.gushikustudios.box2d.controllers.B2BuoyancyController) Vector3(com.badlogic.gdx.math.Vector3) B2Controller(com.gushikustudios.box2d.controllers.B2Controller) PolygonSpriteBatch(com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) PolygonSpriteBatch(com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch) Texture(com.badlogic.gdx.graphics.Texture) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) Box2DDebugRenderer(com.badlogic.gdx.physics.box2d.Box2DDebugRenderer) RubeSceneLoader(com.gushikustudios.rube.loader.RubeSceneLoader) B2GravityController(com.gushikustudios.box2d.controllers.B2GravityController) Fixture(com.badlogic.gdx.physics.box2d.Fixture) Body(com.badlogic.gdx.physics.box2d.Body)

Aggregations

OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)3 Texture (com.badlogic.gdx.graphics.Texture)3 PolygonSpriteBatch (com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch)3 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)3 PolygonRegionLoader (com.badlogic.gdx.graphics.g2d.PolygonRegionLoader)2 PolygonRegion (com.badlogic.gdx.graphics.g2d.PolygonRegion)1 PolygonSprite (com.badlogic.gdx.graphics.g2d.PolygonSprite)1 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)1 ShapeRenderer (com.badlogic.gdx.graphics.glutils.ShapeRenderer)1 Vector3 (com.badlogic.gdx.math.Vector3)1 Body (com.badlogic.gdx.physics.box2d.Body)1 Box2DDebugRenderer (com.badlogic.gdx.physics.box2d.Box2DDebugRenderer)1 Fixture (com.badlogic.gdx.physics.box2d.Fixture)1 PolygonShape (com.badlogic.gdx.physics.box2d.PolygonShape)1 B2BuoyancyController (com.gushikustudios.box2d.controllers.B2BuoyancyController)1 B2Controller (com.gushikustudios.box2d.controllers.B2Controller)1 B2GravityController (com.gushikustudios.box2d.controllers.B2GravityController)1 RubeSceneLoader (com.gushikustudios.rube.loader.RubeSceneLoader)1