Search in sources :

Example 86 with Vector2

use of com.badlogic.gdx.math.Vector2 in project Catacomb-Snatch by Catacomb-Snatch.

the class TmxLevelGenerator method generate.

@Override
public Level generate(Campaign campaign) {
    Level level = null;
    final GeneratorStringOption empty = (GeneratorStringOption) getOption("emptyTile");
    final boolean fill = empty.value != null && Tiles.isRegistered(empty.value);
    for (MapLayer layer : map.getLayers()) {
        if (layer instanceof TiledMapTileLayer) {
            // Tile layer - the first one always decides the map size
            final TiledMapTileLayer tileLayer = (TiledMapTileLayer) layer;
            if (level == null) {
                level = new Level(campaign, this, tileLayer.getWidth(), tileLayer.getHeight());
            }
            for (int x = 0; x < tileLayer.getWidth(); x++) {
                for (int y = 0; y < tileLayer.getHeight(); y++) {
                    final Cell cell = tileLayer.getCell(x, y);
                    final String type = (cell == null || cell.getTile() == null) ? (fill ? empty.value : null) : (String) cell.getTile().getProperties().get("type");
                    if (type != null) {
                        Tiles.createAndAdd(type, level, x, y);
                    }
                }
            }
        } else {
            // Object layer for additional entities (monsters, spawn points, triggers)
            final MapObjects objects = layer.getObjects();
            for (int i = 0; i < objects.getCount(); i++) {
                MapObject obj = objects.get(i);
                String type = obj.getProperties().get("type", String.class);
                if ("spawnPoint".equalsIgnoreCase(type)) {
                    float x = obj.getProperties().get("x", Float.class);
                    float y = obj.getProperties().get("y", Float.class);
                    spawns.add(new Vector2(x, y).add(0.5f, 0.5f));
                }
            }
        }
    }
    // Safety check
    if (level == null) {
        return null;
    }
    return level;
}
Also used : TiledMapTileLayer(com.badlogic.gdx.maps.tiled.TiledMapTileLayer) Vector2(com.badlogic.gdx.math.Vector2) MapLayer(com.badlogic.gdx.maps.MapLayer) Level(net.catacombsnatch.game.world.level.Level) GeneratorStringOption(net.catacombsnatch.game.world.level.generator.options.GeneratorStringOption) Cell(com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell) MapObjects(com.badlogic.gdx.maps.MapObjects) MapObject(com.badlogic.gdx.maps.MapObject)

Example 87 with Vector2

use of com.badlogic.gdx.math.Vector2 in project libgdx-inGameConsole by StrongJoshua.

the class Box2DTest method create.

@Override
public void create() {
    float w = Gdx.graphics.getWidth();
    w *= 2;
    float h = Gdx.graphics.getHeight();
    h *= 2;
    ratio = h / w;
    Gdx.app.getGraphics().setWindowedMode((int) w, (int) h);
    mX = (float) WIDTH / w;
    mY = (float) HEIGHT / h;
    Box2D.init();
    world = new World(new Vector2(0, -9.81f), true);
    batch = new SpriteBatch();
    sprites = new Sprite[250];
    bodies = new Body[sprites.length];
    float k, j;
    for (int i = 0; i < sprites.length; i++) {
        if (i < 50) {
            k = 0;
            j = 1;
        } else if (i < 100) {
            k = 50 * sprites[i - 1].getWidth() + sprites[i - 1].getWidth() / 2;
            j = 2;
        } else if (i < 150) {
            k = 100 * sprites[i - 1].getWidth() + sprites[i - 1].getWidth() / 2;
            j = 3;
        } else if (i < 200) {
            k = 150 * sprites[i - 1].getWidth() + sprites[i - 1].getWidth() / 2;
            j = 4;
        } else {
            k = 200 * sprites[i - 1].getWidth() + sprites[i - 1].getWidth() / 2;
            j = 5;
        }
        sprites[i] = new Sprite(new Texture(Gdx.files.classpath("tests/badlogic.jpg")));
        sprites[i].setSize(2, 2);
        sprites[i].setOriginCenter();
        BodyDef bdef = new BodyDef();
        bdef.type = BodyDef.BodyType.DynamicBody;
        bdef.position.set(i * sprites[i].getWidth() + sprites[i].getWidth() / 2 - k, 15 * j);
        bodies[i] = world.createBody(bdef);
        PolygonShape poly = new PolygonShape();
        poly.setAsBox(sprites[i].getWidth() / 2, sprites[i].getHeight() / 2);
        FixtureDef fdef = new FixtureDef();
        fdef.shape = poly;
        fdef.restitution = .2f;
        fdef.density = 1f;
        bodies[i].createFixture(fdef);
        poly.dispose();
    }
    BodyDef bdef = new BodyDef();
    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set(WIDTH / 2, -5);
    Body b = world.createBody(bdef);
    PolygonShape poly = new PolygonShape();
    poly.setAsBox(50, 5);
    b.createFixture(poly, 0);
    bdef = new BodyDef();
    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set(-5, HEIGHT / 2);
    b = world.createBody(bdef);
    poly = new PolygonShape();
    poly.setAsBox(5, 50);
    b.createFixture(poly, 0);
    bdef = new BodyDef();
    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set(WIDTH + 5, (HEIGHT * ratio) / 2);
    b = world.createBody(bdef);
    poly = new PolygonShape();
    poly.setAsBox(5, 50);
    b.createFixture(poly, 0);
    bdef = new BodyDef();
    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set(WIDTH / 2, HEIGHT * ratio + 5);
    b = world.createBody(bdef);
    poly = new PolygonShape();
    poly.setAsBox(50, 5);
    b.createFixture(poly, 0);
    poly.dispose();
    c = new OrthographicCamera(WIDTH, HEIGHT * ratio);
    c.position.set(c.viewportWidth / 2, c.viewportHeight / 2, 0);
    c.update();
    batch.setProjectionMatrix(c.combined);
    debugRenderer = new Box2DDebugRenderer();
    console = new GUIConsole(false);
    cExec = new MyCommandExecutor();
    console.setCommandExecutor(cExec);
    // set to 'Z' to demonstrate that it works with binds other than the
    // default
    console.setDisplayKeyID(Input.Keys.Z);
    console.setVisible(true);
    // test multiple resets with nested multiplexers
    InputMultiplexer im1 = new InputMultiplexer();
    im1.addProcessor(new Stage());
    im1.addProcessor(new Stage());
    InputMultiplexer im2 = new InputMultiplexer();
    im2.addProcessor(new Stage());
    im2.addProcessor(new Stage());
    im1.addProcessor(im2);
    Gdx.input.setInputProcessor(im1);
    console.setMaxEntries(16);
    console.resetInputProcessing();
    // console already present, logged to consoles
    console.resetInputProcessing();
    console.setSizePercent(100, 33);
    console.setPositionPercent(0, 67);
}
Also used : GUIConsole(com.strongjoshua.console.GUIConsole) PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) Sprite(com.badlogic.gdx.graphics.g2d.Sprite) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) World(com.badlogic.gdx.physics.box2d.World) SpriteBatch(com.badlogic.gdx.graphics.g2d.SpriteBatch) Texture(com.badlogic.gdx.graphics.Texture) InputMultiplexer(com.badlogic.gdx.InputMultiplexer) Box2DDebugRenderer(com.badlogic.gdx.physics.box2d.Box2DDebugRenderer) Vector2(com.badlogic.gdx.math.Vector2) Stage(com.badlogic.gdx.scenes.scene2d.Stage) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) Body(com.badlogic.gdx.physics.box2d.Body) FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef)

Example 88 with Vector2

use of com.badlogic.gdx.math.Vector2 in project libgdx by libgdx.

the class Box2DDebugRenderer method drawContact.

private void drawContact(Contact contact) {
    WorldManifold worldManifold = contact.getWorldManifold();
    if (worldManifold.getNumberOfContactPoints() == 0)
        return;
    Vector2 point = worldManifold.getPoints()[0];
    renderer.setColor(getColorByBody(contact.getFixtureA().getBody()));
    renderer.point(point.x, point.y, 0);
}
Also used : Vector2(com.badlogic.gdx.math.Vector2)

Example 89 with Vector2

use of com.badlogic.gdx.math.Vector2 in project libgdx by libgdx.

the class Contact method getWorldManifold.

/** Get the world manifold. */
public WorldManifold getWorldManifold() {
    int numContactPoints = jniGetWorldManifold(addr, tmp);
    worldManifold.numContactPoints = numContactPoints;
    worldManifold.normal.set(tmp[0], tmp[1]);
    for (int i = 0; i < numContactPoints; i++) {
        Vector2 point = worldManifold.points[i];
        point.x = tmp[2 + i * 2];
        point.y = tmp[2 + i * 2 + 1];
    }
    worldManifold.separations[0] = tmp[6];
    worldManifold.separations[1] = tmp[7];
    return worldManifold;
}
Also used : Vector2(com.badlogic.gdx.math.Vector2)

Example 90 with Vector2

use of com.badlogic.gdx.math.Vector2 in project libgdx by libgdx.

the class Box2DDebugRenderer method renderBody.

protected void renderBody(Body body) {
    Transform transform = body.getTransform();
    for (Fixture fixture : body.getFixtureList()) {
        if (drawBodies) {
            drawShape(fixture, transform, getColorByBody(body));
            if (drawVelocities) {
                Vector2 position = body.getPosition();
                drawSegment(position, body.getLinearVelocity().add(position), VELOCITY_COLOR);
            }
        }
        if (drawAABBs) {
            drawAABB(fixture, transform);
        }
    }
}
Also used : Vector2(com.badlogic.gdx.math.Vector2)

Aggregations

Vector2 (com.badlogic.gdx.math.Vector2)103 Body (com.badlogic.gdx.physics.box2d.Body)21 BodyDef (com.badlogic.gdx.physics.box2d.BodyDef)14 FixtureDef (com.badlogic.gdx.physics.box2d.FixtureDef)10 PolygonShape (com.badlogic.gdx.physics.box2d.PolygonShape)10 Texture (com.badlogic.gdx.graphics.Texture)7 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)7 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)7 EdgeShape (com.badlogic.gdx.physics.box2d.EdgeShape)7 Vector3 (com.badlogic.gdx.math.Vector3)6 Fixture (com.badlogic.gdx.physics.box2d.Fixture)6 World (com.badlogic.gdx.physics.box2d.World)6 GameEntity (ilargia.egdx.logicbricks.gen.game.GameEntity)6 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)5 CircleShape (com.badlogic.gdx.physics.box2d.CircleShape)5 Test (org.junit.Test)5 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)4 Box2DDebugRenderer (com.badlogic.gdx.physics.box2d.Box2DDebugRenderer)4 Contact (com.badlogic.gdx.physics.box2d.Contact)4 WorldManifold (com.badlogic.gdx.physics.box2d.WorldManifold)4