Java Code Examples for com.badlogic.gdx.physics.box2d.PolygonShape#setAsBox()

The following examples show how to use com.badlogic.gdx.physics.box2d.PolygonShape#setAsBox() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Box2DFactory.java    From homescreenarcade with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a wall by constructing a rectangle whose corners are (xmin,ymin) and (xmax,ymax),
 * and rotating the box counterclockwise through the given angle, with specified restitution.
 */
public static Body createWall(World world, float xmin, float ymin, float xmax, float ymax,
        float angle, float restitution) {
    float cx = (xmin + xmax) / 2;
    float cy = (ymin + ymax) / 2;
    float hx = Math.abs((xmax - xmin) / 2);
    float hy = Math.abs((ymax - ymin) / 2);
    PolygonShape wallshape = new PolygonShape();
    // Don't set the angle here; instead call setTransform on the body below. This allows future
    // calls to setTransform to adjust the rotation as expected.
    wallshape.setAsBox(hx, hy, new Vector2(0f, 0f), 0f);

    FixtureDef fdef = new FixtureDef();
    fdef.shape = wallshape;
    fdef.density = 1.0f;
    if (restitution>0) fdef.restitution = restitution;

    BodyDef bd = new BodyDef();
    bd.position.set(cx, cy);
    Body wall = world.createBody(bd);
    wall.createFixture(fdef);
    wall.setType(BodyDef.BodyType.StaticBody);
    wall.setTransform(cx, cy, angle);
    return wall;
}
 
Example 2
Source File: ActorBuilder.java    From Bomberman_libGdx with MIT License 6 votes vote down vote up
public void createIndestructible(float x, float y, TextureAtlas tileTextureAtlas) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyDef.position.set(x, y);

    Body body = b2dWorld.createBody(bodyDef);
    PolygonShape polygonShape = new PolygonShape();
    polygonShape.setAsBox(0.5f, 0.5f);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = polygonShape;
    fixtureDef.filter.categoryBits = GameManager.INDESTRUCTIIBLE_BIT;
    fixtureDef.filter.maskBits = GameManager.PLAYER_BIT | GameManager.ENEMY_BIT | GameManager.BOMB_BIT;
    body.createFixture(fixtureDef);

    polygonShape.dispose();

    Renderer renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("indestructible"), 0, 0, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM);
    renderer.setOrigin(16 / GameManager.PPM / 2, 16 / GameManager.PPM / 2);

    new EntityBuilder(world)
            .with(
                    new Transform(x, y, 1f, 1f, 0),
                    renderer
            )
            .build();
}
 
Example 3
Source File: WorldUtils.java    From martianrun with Apache License 2.0 6 votes vote down vote up
public static Body createEnemy(World world) {
    EnemyType enemyType = RandomUtils.getRandomEnemyType();
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.KinematicBody;
    bodyDef.position.set(new Vector2(enemyType.getX(), enemyType.getY()));
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(enemyType.getWidth() / 2, enemyType.getHeight() / 2);
    Body body = world.createBody(bodyDef);
    body.createFixture(shape, enemyType.getDensity());
    body.resetMassData();
    EnemyUserData userData = new EnemyUserData(enemyType.getWidth(), enemyType.getHeight(),
            enemyType.getAnimationAssetId());
    body.setUserData(userData);
    shape.dispose();
    return body;
}
 
Example 4
Source File: Box2dRaycastObstacleAvoidanceTest.java    From gdx-ai with Apache License 2.0 6 votes vote down vote up
private void createRandomWalls (int n) {
	PolygonShape groundPoly = new PolygonShape();

	BodyDef groundBodyDef = new BodyDef();
	groundBodyDef.type = BodyType.StaticBody;

	FixtureDef fixtureDef = new FixtureDef();
	fixtureDef.shape = groundPoly;
	fixtureDef.filter.groupIndex = 0;

	walls = new Body[n];
	walls_hw = new int[n];
	walls_hh = new int[n];
	for (int i = 0; i < n; i++) {
		groundBodyDef.position.set(Box2dSteeringTest.pixelsToMeters(MathUtils.random(50, (int)container.stageWidth - 50)),
			Box2dSteeringTest.pixelsToMeters(MathUtils.random(50, (int)container.stageHeight - 50)));
		walls[i] = world.createBody(groundBodyDef);
		walls_hw[i] = (int)MathUtils.randomTriangular(20, 150);
		walls_hh[i] = (int)MathUtils.randomTriangular(30, 80);
		groundPoly.setAsBox(Box2dSteeringTest.pixelsToMeters(walls_hw[i]), Box2dSteeringTest.pixelsToMeters(walls_hh[i]));
		walls[i].createFixture(fixtureDef);

	}
	groundPoly.dispose();
}
 
Example 5
Source File: Scene2dRaycastObstacleAvoidanceTest.java    From gdx-ai with Apache License 2.0 6 votes vote down vote up
private void createRandomWalls (int n) {
	PolygonShape groundPoly = new PolygonShape();

	BodyDef groundBodyDef = new BodyDef();
	groundBodyDef.type = BodyType.StaticBody;

	FixtureDef fixtureDef = new FixtureDef();
	fixtureDef.shape = groundPoly;
	fixtureDef.filter.groupIndex = 0;

	walls = new Body[n];
	walls_hw = new int[n];
	walls_hh = new int[n];
	for (int i = 0; i < n; i++) {
		groundBodyDef.position.set(MathUtils.random(50, (int)container.stageWidth - 50),
			MathUtils.random(50, (int)container.stageHeight - 50));
		walls[i] = world.createBody(groundBodyDef);
		walls_hw[i] = (int)MathUtils.randomTriangular(20, 150);
		walls_hh[i] = (int)MathUtils.randomTriangular(30, 80);
		groundPoly.setAsBox(walls_hw[i], walls_hh[i]);
		walls[i].createFixture(fixtureDef);

	}
	groundPoly.dispose();
}
 
Example 6
Source File: WorldUtils.java    From martianrun with Apache License 2.0 5 votes vote down vote up
public static Body createGround(World world) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(new Vector2(Constants.GROUND_X, Constants.GROUND_Y));
    Body body = world.createBody(bodyDef);
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(Constants.GROUND_WIDTH / 2, Constants.GROUND_HEIGHT / 2);
    body.createFixture(shape, Constants.GROUND_DENSITY);
    body.setUserData(new GroundUserData(Constants.GROUND_WIDTH, Constants.GROUND_HEIGHT));
    shape.dispose();
    return body;
}
 
Example 7
Source File: WorldUtils.java    From martianrun with Apache License 2.0 5 votes vote down vote up
public static Body createRunner(World world) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(new Vector2(Constants.RUNNER_X, Constants.RUNNER_Y));
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(Constants.RUNNER_WIDTH / 2, Constants.RUNNER_HEIGHT / 2);
    Body body = world.createBody(bodyDef);
    body.setGravityScale(Constants.RUNNER_GRAVITY_SCALE);
    body.createFixture(shape, Constants.RUNNER_DENSITY);
    body.resetMassData();
    body.setUserData(new RunnerUserData(Constants.RUNNER_WIDTH, Constants.RUNNER_HEIGHT));
    shape.dispose();
    return body;
}
 
Example 8
Source File: Box2DFactory.java    From uracer-kotd with Apache License 2.0 4 votes vote down vote up
/** Creates a wall by constructing a rectangle whose corners are (xmin,ymin) and (xmax,ymax), and rotating the box
 * counterclockwise through the given angle, with specified restitution. */
public static Body createWall (World world, float xmin, float ymin, float xmax, float ymax, float angle, float restitution) {
	float cx = (xmin + xmax) / 2;
	float cy = (ymin + ymax) / 2;
	float hx = (xmax - xmin) / 2;
	float hy = (ymax - ymin) / 2;
	if (hx < 0) {
		hx = -hx;
	}

	if (hy < 0) {
		hy = -hy;
	}

	PolygonShape wallshape = new PolygonShape();
	wallshape.setAsBox(hx, hy, new Vector2(0f, 0f), angle);

	FixtureDef fdef = new FixtureDef();
	fdef.shape = wallshape;
	fdef.density = 1.0f;
	fdef.friction = 0.02f;

	fdef.filter.groupIndex = CollisionFilters.GroupTrackWalls;
	fdef.filter.categoryBits = CollisionFilters.CategoryTrackWalls;
	fdef.filter.maskBits = CollisionFilters.MaskWalls;

	if (Config.Debug.TraverseWalls) {
		fdef.filter.groupIndex = CollisionFilters.GroupNoCollisions;
	}

	if (restitution > 0) {
		fdef.restitution = restitution;
	}

	BodyDef bd = new BodyDef();
	bd.position.set(cx, cy);
	Body wall = world.createBody(bd);
	wall.createFixture(fdef);
	wall.setType(BodyDef.BodyType.StaticBody);
	return wall;
}
 
Example 9
Source File: PhysicsFactory.java    From tilt-game-android with MIT License 4 votes vote down vote up
public static Body createBoxBody(final PhysicsWorld pPhysicsWorld, final float pCenterX, final float pCenterY, final float pWidth, final float pHeight, final float pRotation, final BodyType pBodyType, final FixtureDef pFixtureDef, final float pPixelToMeterRatio) {
	final BodyDef boxBodyDef = new BodyDef();
	boxBodyDef.type = pBodyType;

	boxBodyDef.position.x = pCenterX / pPixelToMeterRatio;
	boxBodyDef.position.y = pCenterY / pPixelToMeterRatio;

	final Body boxBody = pPhysicsWorld.createBody(boxBodyDef);

	final PolygonShape boxPoly = new PolygonShape();

	final float halfWidth = pWidth * 0.5f / pPixelToMeterRatio;
	final float halfHeight = pHeight * 0.5f / pPixelToMeterRatio;

	boxPoly.setAsBox(halfWidth, halfHeight);
	pFixtureDef.shape = boxPoly;

	boxBody.createFixture(pFixtureDef);

	boxPoly.dispose();

	boxBody.setTransform(boxBody.getWorldCenter(), MathUtils.degToRad(pRotation));

	return boxBody;
}
 
Example 10
Source File: ActorBuilder.java    From Bomberman_libGdx with MIT License 4 votes vote down vote up
public void createWall(float x, float y, float mapWidth, float mapHeight, TextureAtlas tileTextureAtlas) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyDef.position.set(x, y);

    Body body = b2dWorld.createBody(bodyDef);
    PolygonShape polygonShape = new PolygonShape();
    polygonShape.setAsBox(0.5f, 0.5f);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = polygonShape;
    fixtureDef.filter.categoryBits = GameManager.INDESTRUCTIIBLE_BIT;
    fixtureDef.filter.maskBits = GameManager.PLAYER_BIT | GameManager.ENEMY_BIT | GameManager.BOMB_BIT;
    body.createFixture(fixtureDef);

    polygonShape.dispose();

    Renderer renderer;

    if (x < 1.0f) {
        if (y < 1.0f) {
            renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 0, 16 * 2, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM);
        } else if (y > mapHeight - 1) {
            renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 0, 16 * 0, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM);

        } else {
            renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 0, 16 * 1, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM);

        }
    } else if (x > mapWidth - 1) {
        if (y < 1.0f) {
            renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 16 * 2, 16 * 2, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM);

        } else if (y > mapHeight - 1) {
            renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 16 * 2, 16 * 0, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM);

        } else {
            renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 16 * 2, 16 * 1, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM);

        }
    } else if (y < 1.0f) {
        renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 16 * 1, 16 * 2, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM);

    } else if (y > mapHeight - 1) {
        renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 16 * 1, 16 * 0, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM);

    } else {
        renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 0, 0, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM);
    }

    renderer.setOrigin(16 / GameManager.PPM / 2, 16 / GameManager.PPM / 2);
    new EntityBuilder(world)
            .with(
                    new Transform(x, y, 1f, 1f, 0),
                    renderer
            )
            .build();
}
 
Example 11
Source File: ActorBuilder.java    From Bomberman_libGdx with MIT License 4 votes vote down vote up
public void createPowerUp(float x, float y) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(MathUtils.floor(x) + 0.5f, MathUtils.floor(y) + 0.5f);

    Body body = b2dWorld.createBody(bodyDef);

    PolygonShape polygonShape = new PolygonShape();
    polygonShape.setAsBox(0.4f, 0.4f);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = polygonShape;
    fixtureDef.filter.categoryBits = GameManager.POWERUP_BIT;
    fixtureDef.filter.maskBits = GameManager.PLAYER_BIT;
    fixtureDef.isSensor = true;
    body.createFixture(fixtureDef);

    PowerUp powerUp = new PowerUp();
    int i;
    switch (powerUp.type) {
        case ONE_UP:
            i = 5;
            break;
        case REMOTE:
            i = 4;
            break;
        case KICK:
            i = 3;
            break;
        case SPEED:
            i = 2;
            break;
        case POWER:
            i = 1;
            break;
        case AMMO:
        default:
            i = 0;
            break;

    }

    TextureAtlas textureAtlas = assetManager.get("img/actors.pack", TextureAtlas.class);
    Renderer renderer = new Renderer(new TextureRegion(textureAtlas.findRegion("Items"), i * 16, 0, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM);
    renderer.setOrigin(16 / GameManager.PPM / 2, 16 / GameManager.PPM / 2);

    Entity e = new EntityBuilder(world)
            .with(
                    powerUp,
                    new RigidBody(body),
                    new Transform(body.getPosition().x, body.getPosition().y, 1, 1, 0),
                    new State("normal"),
                    renderer
            )
            .build();

    body.setUserData(e);
    polygonShape.dispose();
}
 
Example 12
Source File: ActorBuilder.java    From Bomberman_libGdx with MIT License 4 votes vote down vote up
public void createPortal() {
    float x = GameManager.getInstance().getPortalPosition().x;
    float y = GameManager.getInstance().getPortalPosition().y;

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.KinematicBody;
    bodyDef.position.set(x + 0.5f, y + 0.5f);

    Body body = b2dWorld.createBody(bodyDef);

    PolygonShape polygonShape = new PolygonShape();
    polygonShape.setAsBox(0.2f, 0.2f);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = polygonShape;
    fixtureDef.filter.categoryBits = GameManager.PORTAL_BIT;
    fixtureDef.filter.maskBits = GameManager.PLAYER_BIT;
    fixtureDef.isSensor = true;
    body.createFixture(fixtureDef);

    polygonShape.dispose();

    TextureRegion textureRegion = assetManager.get("img/actors.pack", TextureAtlas.class).findRegion("Items");
    Array<TextureRegion> keyFrames = new Array<>();
    for (int i = 6; i < 8; i++) {
        keyFrames.add(new TextureRegion(textureRegion, i * 16, 0, 16, 16));
    }
    Animation anim = new Animation(0.2f, keyFrames, Animation.PlayMode.LOOP);

    HashMap<String, Animation> anims = new HashMap<>();
    anims.put("normal", anim);

    Transform transform = new Transform(body.getPosition().x, body.getPosition().y, 1, 1, 0);
    transform.z = 99; // make portal drawn below player
    Renderer renderer = new Renderer(textureRegion, 16 / GameManager.PPM, 16 / GameManager.PPM);
    renderer.setOrigin(16 / GameManager.PPM / 2, 16 / GameManager.PPM / 2);

    Entity e = new EntityBuilder(world)
            .with(
                    transform,
                    new State("normal"),
                    new Anim(anims),
                    renderer
            )
            .build();

    body.setUserData(e);
}
 
Example 13
Source File: Box2DFactory.java    From Codelabs with MIT License 4 votes vote down vote up
public static Shape createBoxShape(float halfWidth, float halfHeight, Vector2 center, float angle) {
	PolygonShape boxShape = new PolygonShape();
	boxShape.setAsBox(halfWidth, halfHeight, center, angle);
	
	return boxShape;
}