Example usage for com.badlogic.gdx.physics.box2d Body setGravityScale

List of usage examples for com.badlogic.gdx.physics.box2d Body setGravityScale

Introduction

In this page you can find the example usage for com.badlogic.gdx.physics.box2d Body setGravityScale.

Prototype

public void setGravityScale(float scale) 

Source Link

Document

Sets the gravity scale of the body

Usage

From source file:com.dongbat.invasion.registry.EnemyRegistry.java

public static Entity createEnemy(String enemyType, Float positionX) {
    EnemyInfo enemyInfo = get(enemyType);

    EnemyType enemyTypeAI = createEnemyType(enemyInfo);
    Enemy enemyComponent = new Enemy();
    enemyComponent.setType(enemyTypeAI);
    enemyComponent.setAirUnit(enemyInfo.isAirUnit());
    enemyComponent.setBoss(enemyInfo.isBoss());
    enemyComponent.setInfinitePickup(enemyInfo.isInfinitePickup());
    enemyComponent.setCanPickAds(enemyInfo.canPickAds());
    enemyComponent.setAdsPickingDelay(enemyInfo.getAdsPickingDelay());
    enemyComponent.setTributeDelay(enemyInfo.getTributeDelay());
    enemyComponent.setBounty(enemyInfo.getBounty());

    Vector2 position;/*from  ww  w .j a va  2  s  . c o m*/
    if (enemyInfo.isAirUnit()) {
        position = new Vector2(Constants.ENEMY.AIR_START_X, Constants.ENEMY.AIR_START_Y);
    } else {
        float startY = Constants.PHYSICS.GROUND_Y + enemyInfo.getRadius();
        position = new Vector2(Constants.ENEMY.GROUND_START_X, startY);
    }
    if (positionX != null) {
        position.x = positionX;
    }
    Body body = PhysicsUtil.createBody(position, enemyInfo.getRadius());
    body.setGravityScale(0);
    MassData massData = new MassData();
    massData.mass = 1;
    body.setMassData(massData);

    Physics physicsComponent = new Physics();
    physicsComponent.setBody(body);
    physicsComponent.setMaxSpeed(enemyInfo.getMaxSpeed());
    physicsComponent.setRadius(enemyInfo.getRadius());

    Health healthComponent = new Health(enemyInfo.getMaxHealth(), enemyInfo.getMaxHealth());

    Collision collision = new Collision();

    Buff buffComponent = new Buff();

    Delay delayComponent = new Delay();

    Ability abilityComponent = new Ability();
    String abilities = enemyInfo.getAbilities();
    if (abilities != null && !abilities.isEmpty()) {
        String[] names = enemyInfo.getAbilities().split(",");
        for (String name : names) {
            AbilityInfo ability = AbilityRegistry.getAbility(name);
            abilityComponent.addAbility(name, ability);
        }
    }

    EnemyMovement movementComponent = new EnemyMovement();
    DisplayPosition displayPosition = new DisplayPosition(position);
    Ads ads = new Ads();
    ads.setOffline(true);
    ads.setPosition(new Vector2(Constants.ADS.DEFAULT_OFFSET_X, Constants.ADS.DEFAULT_OFFSET_Y));

    Spine spineComponent = new Spine(enemyInfo.getSpineName());

    Entity enemy = ECSUtil.getWorld().createEntity().edit().add(enemyComponent).add(physicsComponent)
            .add(healthComponent).add(collision).add(buffComponent).add(abilityComponent).add(movementComponent)
            .add(ads).add(displayPosition).add(spineComponent).add(delayComponent).getEntity();

    physicsComponent.getBody().setUserData(enemy);

    // Add enemy to a group
    ECSUtil.getWorld().getManager(GroupManager.class).add(enemy, Constants.ENEMY.GROUP_NAME);

    return enemy;
}

From source file:com.gamestudio24.martianrun.utils.WorldUtils.java

License:Apache License

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();// w  w w.  java  2s.  com
    body.setUserData(new RunnerUserData(Constants.RUNNER_WIDTH, Constants.RUNNER_HEIGHT));
    shape.dispose();
    return body;
}

From source file:com.hatstick.fireman.physics.Box2DPhysicsWorld.java

License:Apache License

private Fixture createPhysicsObject(Rectangle bounds, ModelType type, Integer id) {

    UserData userData = new UserData(numEnterPoints, type, null);
    CircleShape objectPoly = new CircleShape();
    objectPoly.setRadius(bounds.width / 2);

    BodyDef enemyBodyDef = new BodyDef();
    enemyBodyDef.type = BodyType.DynamicBody;
    enemyBodyDef.position.x = bounds.x;/*  w w w.  j a v a 2s. co m*/
    enemyBodyDef.position.y = bounds.y;
    Body objectBody = world.createBody(enemyBodyDef);
    FixtureDef objectFixtureDef = new FixtureDef();
    objectFixtureDef.shape = objectPoly;

    //   objectBody.setUserData(userData);
    objectFixtureDef.restitution = .025f;
    Fixture fixture = objectBody.createFixture(objectFixtureDef);
    fixture.setUserData(userData);
    objectBody.setLinearDamping(2f);
    objectBody.setGravityScale(.4f);
    objectBody.setFixedRotation(true);
    objectPoly.dispose();
    numEnterPoints++;

    //add a sensor on the bottom to check if touching ground (for jumping)
    PolygonShape polygonShape = new PolygonShape();
    polygonShape.setAsBox(bounds.width / 3, bounds.height / 8, new Vector2(0, -bounds.height / 2), 0);
    objectFixtureDef = new FixtureDef();
    objectFixtureDef.shape = polygonShape;
    objectFixtureDef.isSensor = true;
    Fixture footSensorFixture = objectBody.createFixture(objectFixtureDef);
    footSensorFixture.setUserData(new UserData(numEnterPoints, ModelType.FOOT_SENSOR, id));

    //add a sensor on left side to check if touching wall (for grappling)
    PolygonShape polygonShape2 = new PolygonShape();
    polygonShape2.setAsBox(bounds.width / 8, bounds.height / 3, new Vector2(-bounds.width / 2, 0), 0);
    objectFixtureDef = new FixtureDef();
    objectFixtureDef.shape = polygonShape2;
    objectFixtureDef.isSensor = true;
    Fixture leftSideSensorFixture = objectBody.createFixture(objectFixtureDef);
    leftSideSensorFixture.setUserData(new UserData(numEnterPoints, ModelType.LEFT_SIDE_SENSOR, id));

    //add a sensor on right side to check if touching wall (for grappling)
    polygonShape2.setAsBox(bounds.width / 8, bounds.height / 3, new Vector2(bounds.width / 2, 0), 0);
    objectFixtureDef = new FixtureDef();
    objectFixtureDef.shape = polygonShape2;
    objectFixtureDef.isSensor = true;
    Fixture rightSideSensorFixture = objectBody.createFixture(objectFixtureDef);
    rightSideSensorFixture.setUserData(new UserData(numEnterPoints, ModelType.RIGHT_SIDE_SENSOR, id));

    return fixture;
}

From source file:Helper.WorldUtils.java

public static Body createPinguin(World world) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(new Vector2(Constants.RUNNER_X, Constants.RUNNER_Y));
    Body body = world.createBody(bodyDef);
    body.setGravityScale(Constants.RUNNER_GRAVITY_SCALE);
    body.setFixedRotation(false);/* w  ww .ja v  a2  s .  co m*/
    PolygonShape box = new PolygonShape();
    box.setAsBox(Constants.RUNNER_WIDTH / 2, Constants.RUNNER_HEIGHT / 2, body.getLocalCenter(), 0);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = box;
    fixtureDef.density = 0.0f;
    fixtureDef.friction = BuffsInfo.getFrictionBuff().getPower();
    fixtureDef.restitution = 0.35f;
    body.createFixture(fixtureDef);
    //body.resetMassData();
    body.getFixtureList().get(0).setUserData("PINGUIN");
    box.dispose();
    return body;
}

From source file:Helper.WorldUtils.java

public static Body createSnake(World world, float x, float y, float width, float height) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyDef.position.set(new Vector2(x, y + height));
    //   bodyDef.active = false;
    Body body = world.createBody(bodyDef);
    body.setGravityScale(Constants.RUNNER_GRAVITY_SCALE);
    body.setFixedRotation(false);//from www.j  a v a2 s  .  c o m
    PolygonShape box = new PolygonShape();
    box.setAsBox(width / 2, height / 2);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = box;
    fixtureDef.density = 0.0f;
    fixtureDef.friction = 1.8f;
    fixtureDef.restitution = 0.35f;
    body.createFixture(fixtureDef);
    body.resetMassData();
    body.getFixtureList().get(0).setUserData("SNAKE");
    box.dispose();
    return body;
}

From source file:Helper.WorldUtils.java

public static Body createAntelopeBack(World world, float x, float y, float width, float height) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(new Vector2(x, y + height));
    //   bodyDef.active = false;
    Body body = world.createBody(bodyDef);
    body.setGravityScale(0f);
    body.setFixedRotation(false);/*from  www .j  a v  a2 s  . c o m*/
    PolygonShape box = new PolygonShape();
    box.setAsBox(width / 2, height / 2);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = box;
    fixtureDef.density = 0.0f;
    fixtureDef.friction = 1.8f;
    fixtureDef.restitution = 0f;
    body.createFixture(fixtureDef);
    body.getFixtureList().get(0).setUserData("AntelopeBack");
    body.resetMassData();
    box.dispose();
    return body;
}

From source file:Helper.WorldUtils.java

public static Body createAntelopeBody(World world, float x, float y, float width, float height) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(new Vector2(x, y + height));
    Body body = world.createBody(bodyDef);
    body.setGravityScale(0f);
    body.setFixedRotation(false);//from  w ww .  java 2 s .  c  o m
    PolygonShape box = new PolygonShape();
    box.setAsBox(width / 2, height / 2);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = box;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 1.8f;
    fixtureDef.restitution = 0f;
    body.createFixture(fixtureDef);
    body.resetMassData();
    body.getFixtureList().get(0).setUserData("AntelopeBody");
    box.dispose();
    return body;
}

From source file:Helper.WorldUtils.java

public static Body createHippoHead(World world, float x, float y, float width, float height) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyDef.position.set(new Vector2(x, y + height / 2));
    //   bodyDef.active = false;
    Body body = world.createBody(bodyDef);
    body.setGravityScale(0f);
    body.setFixedRotation(false);//w w  w . j a v a 2 s  . co m
    PolygonShape box = new PolygonShape();
    box.setAsBox(width / 2, height / 2);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = box;
    fixtureDef.density = 0.0f;
    fixtureDef.friction = 1.8f;
    fixtureDef.restitution = 0f;
    body.createFixture(fixtureDef);
    body.getFixtureList().get(0).setUserData("HippoHead");
    body.resetMassData();
    box.dispose();
    return body;
}

From source file:Helper.WorldUtils.java

public static Body createHippoBody(World world, float x, float y, float width, float height) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyDef.position.set(new Vector2(x, y + height / 2));
    Body body = world.createBody(bodyDef);
    body.setGravityScale(0f);
    body.setFixedRotation(false);//from  ww w . ja va2 s.  com
    PolygonShape box = new PolygonShape();
    box.setAsBox(width / 2, height / 2);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = box;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 1.8f;
    fixtureDef.restitution = 0f;
    body.createFixture(fixtureDef);
    body.resetMassData();
    body.getFixtureList().get(0).setUserData("HippoBody");
    box.dispose();
    return body;
}

From source file:io.piotrjastrzebski.sfg.game.objects.PlayerRagDoll.java

License:Open Source License

private Body createBody(World world, int id, float scale) {
    final BodyDef obstacleBodyDef = new BodyDef();
    obstacleBodyDef.type = BodyDef.BodyType.DynamicBody;
    final Body body = world.createBody(obstacleBodyDef);
    // large dampening so tiny bodies dont explode super fast
    body.setLinearDamping(0.75f);/*from w  ww  . j a  v a2s.  c  o  m*/
    body.setAngularDamping(0.75f);

    final PolygonShape rect = new PolygonShape();
    rect.setAsBox(WIDTHS[id] * scale, HEIGHTS[id] * scale);

    final FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = rect;
    fixtureDef.density = 3f;
    fixtureDef.friction = 0.25f;
    fixtureDef.restitution = 0.25f;
    fixtureDef.filter.categoryBits = Collision.BODY_PART;
    fixtureDef.filter.maskBits = Collision.MASK_BODY_PART;
    // without this bodies are actually lower than expected when driven by animation
    body.setGravityScale(0);
    body.createFixture(fixtureDef);
    // Clean up
    rect.dispose();
    return body;
}