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

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

Introduction

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

Prototype

public void applyForceToCenter(Vector2 force, boolean wake) 

Source Link

Document

Apply a force to the center of mass.

Usage

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

License:Apache License

public void moveBody(Body body, Vector2 direction) {
    body.applyForceToCenter(direction, true);
}

From source file:com.tnf.ptm.entities.ship.PtmShip.java

License:Apache License

@Override
public void receiveForce(Vector2 force, PtmGame game, boolean acc) {
    Body body = myHull.getBody();
    if (acc) {//ww w.  j a  v a2 s  .c  o  m
        force.scl(myHull.getMass());
    }
    body.applyForceToCenter(force, true);
}

From source file:com.tnf.ptm.entities.ship.ShipEngine.java

License:Apache License

private boolean applyInput(PtmGame cmp, float shipAngle, Pilot provider, Body body, Vector2 spd,
        boolean controlsEnabled, float mass) {
    boolean spdOk = PtmMath.canAccelerate(shipAngle, spd);
    boolean working = controlsEnabled && provider.isUp() && spdOk;

    Engine e = myItem;/* w  w  w  .  j a  v  a  2 s. c o  m*/
    if (working) {
        Vector2 v = PtmMath.fromAl(shipAngle, mass * e.getAcc());
        body.applyForceToCenter(v, true);
        PtmMath.free(v);
    }

    float ts = cmp.getTimeStep();
    float rotSpd = body.getAngularVelocity() * PtmMath.radDeg;
    float desiredRotSpd = 0;
    float rotAcc = e.getRotAcc();
    boolean l = controlsEnabled && provider.isLeft();
    boolean r = controlsEnabled && provider.isRight();
    float absRotSpd = PtmMath.abs(rotSpd);
    if (absRotSpd < e.getMaxRotSpd() && l != r) {
        desiredRotSpd = PtmMath.toInt(r) * e.getMaxRotSpd();
        if (absRotSpd < MAX_RECOVER_ROT_SPD) {
            if (myRecoverAwait > 0) {
                myRecoverAwait -= ts;
            }
            if (myRecoverAwait <= 0) {
                rotAcc *= RECOVER_MUL;
            }
        }
    } else {
        myRecoverAwait = RECOVER_AWAIT;
    }
    body.setAngularVelocity(PtmMath.degRad * PtmMath.approach(rotSpd, desiredRotSpd, rotAcc * ts));
    return working;
}

From source file:org.destinationsol.game.ship.ShipEngine.java

License:Apache License

private boolean applyInput(SolGame cmp, float shipAngle, Pilot provider, Body body, Vector2 spd,
        boolean controlsEnabled, float mass) {
    boolean spdOk = SolMath.canAccelerate(shipAngle, spd);
    boolean working = controlsEnabled && provider.isUp() && spdOk;

    EngineItem e = myItem;/*  ww w. ja v a2 s  .  co  m*/
    if (working) {
        Vector2 v = SolMath.fromAl(shipAngle, mass * e.getAcc());
        body.applyForceToCenter(v, true);
        SolMath.free(v);
    }

    float ts = cmp.getTimeStep();
    float rotSpd = body.getAngularVelocity() * SolMath.radDeg;
    float desiredRotSpd = 0;
    float rotAcc = e.getRotAcc();
    boolean l = controlsEnabled && provider.isLeft();
    boolean r = controlsEnabled && provider.isRight();
    float absRotSpd = SolMath.abs(rotSpd);
    if (absRotSpd < e.getMaxRotSpd() && l != r) {
        desiredRotSpd = SolMath.toInt(r) * e.getMaxRotSpd();
        if (absRotSpd < MAX_RECOVER_ROT_SPD) {
            if (myRecoverAwait > 0)
                myRecoverAwait -= ts;
            if (myRecoverAwait <= 0)
                rotAcc *= RECOVER_MUL;
        }
    } else {
        myRecoverAwait = RECOVER_AWAIT;
    }
    body.setAngularVelocity(SolMath.degRad * SolMath.approach(rotSpd, desiredRotSpd, rotAcc * ts));
    return working;
}

From source file:org.destinationsol.game.ship.SolShip.java

License:Apache License

@Override
public void receiveForce(Vector2 force, SolGame game, boolean acc) {
    Body body = myHull.getBody();
    if (acc) {//w  ww .  jav  a2  s .  c o m
        force.scl(myHull.getMass());
    }
    body.applyForceToCenter(force, true);
}

From source file:org.matheusdev.ror.controller.component.ComponentMovement.java

License:Open Source License

@Override
public void apply(Entity entity) {
    Body body = entity.getBody();
    moving = false;//from   www .j  a va 2s .c om

    // If trying to move (pressing buttons on Keyboard, steering with Gamepad)
    if (xsteer != 0f || ysteer != 0f) {
        moving = true;

        if (Math.abs(xsteer) > Math.abs(ysteer)) {
            if (xsteer < 0) {
                direction = Dir.LEFT;
            } else {
                direction = Dir.RIGHT;
            }
        } else {
            if (ysteer < 0) {
                direction = Dir.DOWN;
            } else {
                direction = Dir.UP;
            }
        }
    }

    Vector2 linVel = body.getLinearVelocity();
    if (linVel.len() > maxspeed) {
        body.setLinearVelocity(linVel.cpy().nor().scl(maxspeed));
    }

    body.applyForceToCenter(strength * xsteer, strength * ysteer);

    if (friction > 1f && !moving) {
        body.setLinearVelocity(linVel.div(friction));
    }
}