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

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

Introduction

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

Prototype

public void setLinearVelocity(float vX, float vY) 

Source Link

Document

Set the linear velocity of the center of mass.

Usage

From source file:com.github.mkjensen.breakall.actor.Ball.java

License:Apache License

@Override
protected Body createBody(World world) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DynamicBody;

    CircleShape circle = new CircleShape();
    circle.setRadius(getRadius());/*from ww  w  .j av  a 2 s . c  o  m*/

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = circle;
    fixtureDef.filter.groupIndex = FILTER_GROUP_INDEX;
    fixtureDef.friction = FRICTION;
    fixtureDef.restitution = RESTITUTION;

    Body body = world.createBody(bodyDef);
    body.setLinearVelocity(4f, 4f); // TODO Relative to world size?
    body.createFixture(fixtureDef);
    // body.setBullet(true); // TODO Bullet?
    circle.dispose();

    return body;
}

From source file:com.trance.view.utils.WorldUtils.java

License:Apache License

public static void createExplode(World world, Explode explode, int numRays, float x, float y, float power) {
    for (int i = 0; i < numRays; i++) {
        float angle = (i / (float) numRays) * 360 * MathUtils.degreesToRadians;

        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;/* w w  w  .  ja  v  a2 s  . c o  m*/
        bd.fixedRotation = true; // rotation not necessary
        bd.bullet = true; // prevent tunneling at high speed
        bd.linearDamping = 0f; // drag due to moving through air
        bd.gravityScale = 0; // ignore gravity
        bd.position.set(x * WORLD_TO_BOX, y * WORLD_TO_BOX);
        Body body = world.createBody(bd);

        CircleShape shape = new CircleShape();
        shape.setRadius(0.5f); // very small

        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 60 / (float) numRays; // very high - shared across all particles
        fd.friction = 0; // friction not necessary
        fd.restitution = 0.99f; // high restitution to reflect off obstacles
        fd.filter.groupIndex = -1; // particles should not collide with each other
        body.createFixture(fd);
        shape.dispose();
        body.setUserData(explode);
        body.setLinearVelocity(MathUtils.sin(angle) * power, MathUtils.cos(angle) * power);
    }
}

From source file:org.csproduction.descendant.screen.GameScreen.java

private boolean handleInput() {
    if (GameInput.isHeld(Keys.ENTER)
            && (GameInput.isHeld(Keys.SHIFT_LEFT) || GameInput.isHeld(Keys.SHIFT_RIGHT))) {
        return false;
    }//from   ww w  . j  a va  2 s.c  om

    if (player != null) {
        player.resetVelocity();
        Body pb = player.getBody();
        if (GameInput.isPressed(Keys.W)) {
            if (player.isOnGround()) {
                player.jump(200);
            }
        }

        if (GameInput.isHeld(Keys.D)) {
            if (player.isOnGround()) {
                player.move(150);
                player.toggleFriction(false);
            } else {
                player.move(Math.abs(Math.max(player.getPrevVelocity().x * pb.getMass(), 75)));
            }
        } else if (GameInput.isReleased(Keys.D)) {
            player.toggleFriction(true);
        }

        if (GameInput.isHeld(Keys.A)) {
            if (player.isOnGround()) {
                player.move(-150);
                player.toggleFriction(false);
            } else {
                player.move(-Math.abs(Math.min(player.getPrevVelocity().x * pb.getMass(), -75)));
            }
        } else if (GameInput.isReleased(Keys.A)) {
            player.toggleFriction(true);
        }

        if (GameInput.isPressed(Keys.F)) {
            player.attack(spells, "fireball");
        }

        if (GameInput.isPressed(Keys.V) && pb.getLinearVelocity().y == 0) {
            if (player.isOnGround())
                player.attack(spells, "earthspike");
        }

        if (GameInput.isPressed(Keys.R)) {
            player.attack(spells, "airslash");
        }

        if (GameInput.isPressed(Keys.C)) {
            player.attack(spells, "fireblast");
        }

        if (GameInput.isPressed(Keys.B)) {
            player.attack(spells, "flamestrike");
        }

        if (GameInput.isPressed(Keys.T)) {
            player.attack(spells, "iceshard");
        }

        if (GameInput.isPressed(Keys.Y)) {
            player.attack(spells, "hellfireblast");
        }

        if (GameInput.isPressed(Keys.H)) {
            player.attack(spells, "inferno");
        }

        if (GameInput.isPressed(Keys.G) && pb.getLinearVelocity().y == 0) {
            if (player.isOnGround())
                player.attack(spells, "groundslam");
        }
    }

    if (false) {//player2!=null){
        Body pb = player2.getBody();
        if (GameInput.isPressed(Keys.I)) {
            if (player2.isOnGround()) {
                player2.jump(240);
            }
        }

        if (GameInput.isHeld(Keys.L)) {
            if (player2.isOnGround()) {
                player2.move(500);
                if (pb.getLinearVelocity().x > MAX_VX)
                    pb.setLinearVelocity(MAX_VX, pb.getLinearVelocity().y);
            } else {
                player2.move(150);
                if (pb.getLinearVelocity().x > MAX_VX)
                    pb.setLinearVelocity(MAX_VX, pb.getLinearVelocity().y);
            }
        }

        if (GameInput.isHeld(Keys.J)) {
            if (player2.isOnGround()) {
                player2.move(-500);
                if (pb.getLinearVelocity().x < -MAX_VX)
                    pb.setLinearVelocity(-MAX_VX, pb.getLinearVelocity().y);
            } else {
                player2.move(-150);
                if (pb.getLinearVelocity().x < -MAX_VX)
                    pb.setLinearVelocity(-MAX_VX, pb.getLinearVelocity().y);
            }
        }

        if (GameInput.isPressed(Keys.SEMICOLON)) {
            player2.attack(spells, "fireball");
        }

        if (GameInput.isPressed(Keys.SLASH) && pb.getLinearVelocity().y == 0) {
            if (player2.isOnGround())
                player2.attack(spells, "earthspike");
        }

        if (GameInput.isPressed(Keys.P)) {
            player2.attack(spells, "airslash");
        }

        if (GameInput.isPressed(Keys.PERIOD)) {
            player2.attack(spells, "fireblast");
        }
    }

    ///////////////CALL THIS LAST///////////////
    GameInput.update();

    return true;
}

From source file:us.notsoserio.ninja.physics.CollisionPhysics.java

License:Apache License

public static void EntityGround(Body entity) {
    ((Entity) entity.getUserData()).isInControl = true;
    ((Entity) entity.getUserData()).CollidingWithCount++;
    entity.setLinearVelocity(0, 0);
}

From source file:us.notsoserio.ninja.physics.EntityPhysics.java

License:Apache License

public static void MoveCharacter(Body body) {

    Entity entity = (Entity) body.getUserData();

    boolean Top = false;
    boolean Bottom = false;
    boolean Left = false;
    boolean Right = false;

    if (entity.Top.collisionCount > 0)
        Top = true;/* w ww. j a va 2  s .  c  om*/
    else if (entity.Bottom.collisionCount > 0)
        Bottom = true;
    if (entity.Left.collisionCount > 0)
        Left = true;
    else if (entity.Right.collisionCount > 0)
        Right = true;

    if (!Top && !Left && !Right)
        body.setGravityScale(1);

    if (Left || Right || Top)
        body.setGravityScale(0);

    if (entity.isJumping) {
        boolean doJump = true;

        if (Left && entity.JumpX < 0)
            doJump = false;

        if (Right && entity.JumpX > 0)
            doJump = false;

        if (Top && entity.JumpY > 0)
            doJump = false;

        if (Bottom && entity.JumpY < 0)
            doJump = false;

        if (doJump) {
            body.applyLinearImpulse(entity.JumpX, entity.JumpY, body.getPosition().x, body.getPosition().y,
                    true);
            entity.isInControl = false;
            body.setGravityScale(1);
            return;
        }
    }

    if (entity.isWalkingLeft && Top) {
        body.setLinearVelocity(-entity.speed, 0);
        body.setGravityScale(0);
    } else if (entity.isWalkingLeft && Bottom) {
        body.setLinearVelocity(-entity.speed, body.getLinearVelocity().y);
        body.setGravityScale(1);
    }

    else if (entity.isWalkingRight && Top) {
        body.setLinearVelocity(entity.speed, 0);
        body.setGravityScale(0);
    } else if (entity.isWalkingRight && Bottom) {
        body.setLinearVelocity(entity.speed, body.getLinearVelocity().y);
        body.setGravityScale(1);
    }

    else if (entity.isWalkingUp && Left) {
        body.setLinearVelocity(body.getLinearVelocity().x, entity.speed);
        body.setGravityScale(0);
    } else if (entity.isWalkingUp && Right) {
        body.setLinearVelocity(body.getLinearVelocity().x, entity.speed);
        body.setGravityScale(0);
    }

    else if (entity.isWalkingDown && Left) {
        body.setLinearVelocity(body.getLinearVelocity().x, -entity.speed);
        body.setGravityScale(0);
    } else if (entity.isWalkingDown && Right) {
        body.setLinearVelocity(body.getLinearVelocity().x, -entity.speed);
        body.setGravityScale(0);
    }

    else if (entity.isInControl) {
        body.setLinearVelocity(body.getLinearVelocity().x *= 0.1f, body.getLinearVelocity().y *= 0.1f);
    }

}