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

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

Introduction

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

Prototype

public void setAngularVelocity(float omega) 

Source Link

Document

Set the angular velocity.

Usage

From source file:com.netthreads.gdx.app.layer.SimulationLayer.java

License:Apache License

/**
 * Drop ball into centre.//from   w  ww .  j  ava  2 s .  c om
 * 
 */
private void handleDropShape(float x, float y) {

    // Assign random starting position and angle.
    float tx = x / pixelsPerMetre;
    float ty = y / pixelsPerMetre;
    float angle = rand.nextFloat() * MathUtils.PI * 2;

    // -----------------------
    // Body
    // -----------------------
    BodyDef ballBodyDef = new BodyDef();
    ballBodyDef.position.set(tx, ty);
    ballBodyDef.angle = angle;
    ballBodyDef.type = BodyType.DynamicBody;

    CircleShape ballShape = new CircleShape();
    ballShape.setRadius(BALL_BODY_SIZE.x / 2);

    Body body = world.createBody(ballBodyDef);
    body.createFixture(ballShape, 1);
    body.setUserData(BALL_BODY_SIZE);

    // Properties
    body.setActive(true);
    body.setAwake(true);
    body.setLinearVelocity(tmpVec.set(0, 0));
    body.setAngularVelocity(0);

    // -----------------------
    // Get sprite.
    // -----------------------
    SimpleSprite sprite = pool.obtain();

    // Size.
    sprite.setScaleX((BALL_BODY_SIZE.x * pixelsPerMetre) / sprite.getWidth());
    sprite.setScaleY((BALL_BODY_SIZE.y * pixelsPerMetre) / sprite.getHeight());

    // Centre of rotation.
    sprite.setOriginX(sprite.getWidth() / 2);
    sprite.setOriginY(sprite.getHeight() / 2);

    // -----------------------
    // Add actor to the view.
    // -----------------------
    addActor(sprite);

    // -----------------------
    // Create Action
    // -----------------------
    BodyUpdateAction updateAction = BodyUpdateAction.$(world, body, pixelsPerMetre, true);

    // -----------------------
    // Run action on actor.
    // -----------------------
    sprite.addAction(updateAction);
}

From source file:com.tnf.ptm.entities.asteroid.AsteroidBuilder.java

License:Apache License

public Asteroid build(PtmGame game, Vector2 pos, TextureAtlas.AtlasRegion tex, float sz, float angle,
        float rotSpd, Vector2 spd, RemoveController removeController) {

    ArrayList<Dra> dras = new ArrayList<Dra>();
    Body body;
    if (MAX_BALL_SZ < sz) {
        body = myCollisionMeshLoader.getBodyAndSprite(game, "asteroids", removePath(tex.name) + "_" + tex.index,
                sz, BodyDef.BodyType.DynamicBody, pos, angle, dras, DENSITY, DraLevel.BODIES, tex);
    } else {//  ww  w.j a v a  2  s.  c  o  m
        body = buildBall(game, pos, angle, sz / 2, DENSITY, false);
        RectSprite s = new RectSprite(tex, sz, 0, 0, new Vector2(), DraLevel.BODIES, 0, 0, PtmColor.WHITE,
                false);
        dras.add(s);
    }
    body.setAngularVelocity(rotSpd);
    body.setLinearVelocity(spd);

    Asteroid res = new Asteroid(game, tex, body, sz, removeController, dras);
    body.setUserData(res);
    return res;
}

From source file:com.tnf.ptm.entities.item.LootBuilder.java

License:Apache License

public Loot build(PtmGame game, Vector2 pos, PtmItem item, Vector2 spd, int life, float rotSpd, PtmShip owner) {
    List<Dra> dras = new ArrayList<Dra>();
    TextureAtlas.AtlasRegion tex = item.getIcon(game);
    float sz = item.getItemType().sz;
    RectSprite s = new RectSprite(tex, sz, 0, 0, new Vector2(), DraLevel.GUNS, 0, 0, PtmColor.WHITE, false);
    dras.add(s);/*from   w w w .j av a2s  .  c om*/
    Body b = buildBody(game, pos, sz);
    b.setLinearVelocity(spd);
    b.setAngularVelocity(rotSpd);
    Color col = item.getItemType().color;
    LightSrc ls = new LightSrc(game, sz + .18f, false, .5f, new Vector2(), col);
    ls.collectDras(dras);
    Loot loot = new Loot(item, b, life, dras, ls, owner);
    b.setUserData(loot);
    return loot;
}

From source file:com.tnf.ptm.entities.ShardBuilder.java

License:Apache License

public Shard build(PtmGame game, Vector2 basePos, Vector2 baseSpd, float size) {

    ArrayList<Dra> dras = new ArrayList<Dra>();
    float scale = PtmMath.rnd(MIN_SCALE, MAX_SCALE);
    TextureAtlas.AtlasRegion tex = PtmMath.elemRnd(myTexs);
    float spdAngle = PtmMath.rnd(180);
    Vector2 pos = new Vector2();
    PtmMath.fromAl(pos, spdAngle, PtmMath.rnd(size));
    pos.add(basePos);/*from w  ww .ja  v a 2 s.  c o m*/
    Body body = myCollisionMeshLoader.getBodyAndSprite(game, "smallGameObjects",
            AsteroidBuilder.removePath(tex.name) + "_" + tex.index, scale, BodyDef.BodyType.DynamicBody, pos,
            PtmMath.rnd(180), dras, ShipBuilder.SHIP_DENSITY, DraLevel.PROJECTILES, tex);

    body.setAngularVelocity(PtmMath.rnd(MAX_ROT_SPD));
    Vector2 spd = PtmMath.fromAl(spdAngle, PtmMath.rnd(MAX_SPD));
    spd.add(baseSpd);
    body.setLinearVelocity(spd);
    PtmMath.free(spd);

    Shard shard = new Shard(body, dras);
    body.setUserData(shard);
    return shard;
}

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

License:Apache License

private Hull buildHull(PtmGame game, Vector2 pos, Vector2 spd, float angle, float rotSpd, HullConfig hullConfig,
        float life, ArrayList<Dra> dras) {
    //TODO: This logic belongs in the HullConfigManager/HullConfig
    String shipName = hullConfig.getInternalName();

    Json json = Assets.getJson(new ResourceUrn(shipName));

    JsonValue rigidBodyNode = json.getJsonValue().get("rigidBody");
    myCollisionMeshLoader.readRigidBody(rigidBodyNode, hullConfig);

    // TODO: Ensure that this does not cause any problems
    json.dispose();//from w w  w.ja v  a 2  s . c om

    BodyDef.BodyType bodyType = hullConfig.getType() == HullConfig.Type.STATION ? BodyDef.BodyType.KinematicBody
            : BodyDef.BodyType.DynamicBody;
    DraLevel level = hullConfig.getType() == HullConfig.Type.STD ? DraLevel.BODIES : DraLevel.BIG_BODIES;
    Body body = myCollisionMeshLoader.getBodyAndSprite(game, hullConfig, hullConfig.getSize(), bodyType, pos,
            angle, dras, SHIP_DENSITY, level, hullConfig.getTexture());
    Fixture shieldFixture = createShieldFixture(hullConfig, body);

    GunMount gunMount0 = new GunMount(hullConfig.getGunSlot(0));
    GunMount gunMount1 = (hullConfig.getNrOfGunSlots() > 1) ? new GunMount(hullConfig.getGunSlot(1)) : null;

    List<LightSrc> lCs = new ArrayList<LightSrc>();
    for (Vector2 p : hullConfig.getLightSourcePositions()) {
        LightSrc lc = new LightSrc(game, .35f, true, .7f, p, game.getCols().hullLights);
        lc.collectDras(dras);
        lCs.add(lc);
    }

    ArrayList<ForceBeacon> beacons = new ArrayList<ForceBeacon>();
    for (Vector2 relPos : hullConfig.getForceBeaconPositions()) {
        ForceBeacon fb = new ForceBeacon(game, relPos, pos, spd);
        fb.collectDras(dras);
        beacons.add(fb);
    }

    ArrayList<Door> doors = new ArrayList<Door>();
    for (Vector2 doorRelPos : hullConfig.getDoorPositions()) {
        Door door = createDoor(game, pos, angle, body, doorRelPos);
        door.collectDras(dras);
        doors.add(door);
    }

    Fixture base = getBase(hullConfig.hasBase(), body);
    Hull hull = new Hull(game, hullConfig, body, gunMount0, gunMount1, base, lCs, life, beacons, doors,
            shieldFixture);
    body.setLinearVelocity(spd);
    body.setAngularVelocity(rotSpd * PtmMath.degRad);
    return hull;
}

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;/*from   w  ww  . j  a v  a2 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.asteroid.AsteroidBuilder.java

License:Apache License

public Asteroid build(SolGame game, Vector2 pos, TextureAtlas.AtlasRegion tex, float sz, float angle,
        float rotSpd, Vector2 spd, RemoveController removeController) {

    ArrayList<Dra> dras = new ArrayList<Dra>();
    Body body;
    if (MAX_BALL_SZ < sz) {
        body = myPathLoader.getBodyAndSprite(game, "asteroids", removePath(tex.name) + "_" + tex.index, sz,
                BodyDef.BodyType.DynamicBody, pos, angle, dras, DENSITY, DraLevel.BODIES, tex);
    } else {/*w  w  w  .  j a  v a2 s  .  co m*/
        body = buildBall(game, pos, angle, sz / 2, DENSITY, false);
        RectSprite s = new RectSprite(tex, sz, 0, 0, new Vector2(), DraLevel.BODIES, 0, 0, SolColor.W, false);
        dras.add(s);
    }
    body.setAngularVelocity(rotSpd);
    body.setLinearVelocity(spd);

    Asteroid res = new Asteroid(game, tex, body, sz, removeController, dras);
    body.setUserData(res);
    return res;
}

From source file:org.destinationsol.game.item.LootBuilder.java

License:Apache License

public Loot build(SolGame game, Vector2 pos, SolItem item, Vector2 spd, int life, float rotSpd, SolShip owner) {
    List<Dra> dras = new ArrayList<Dra>();
    TextureAtlas.AtlasRegion tex = item.getIcon(game);
    float sz = item.getItemType().sz;
    RectSprite s = new RectSprite(tex, sz, 0, 0, new Vector2(), DraLevel.GUNS, 0, 0, SolColor.W, false);
    dras.add(s);/*from   w ww .j  ava  2s. c  om*/
    Body b = buildBody(game, pos, sz);
    b.setLinearVelocity(spd);
    b.setAngularVelocity(rotSpd);
    Color col = item.getItemType().color;
    LightSrc ls = new LightSrc(game, sz + .18f, false, .5f, new Vector2(), col);
    ls.collectDras(dras);
    Loot loot = new Loot(item, b, life, dras, ls, owner);
    b.setUserData(loot);
    return loot;
}

From source file:org.destinationsol.game.ShardBuilder.java

License:Apache License

public Shard build(SolGame game, Vector2 basePos, Vector2 baseSpd, float size) {

    ArrayList<Dra> dras = new ArrayList<Dra>();
    float scale = SolMath.rnd(MIN_SCALE, MAX_SCALE);
    TextureAtlas.AtlasRegion tex = SolMath.elemRnd(myTexs);
    float spdAngle = SolMath.rnd(180);
    Vector2 pos = new Vector2();
    SolMath.fromAl(pos, spdAngle, SolMath.rnd(size));
    pos.add(basePos);//from  www. jav a 2  s.com
    Body body = myPathLoader.getBodyAndSprite(game, "smallGameObjs",
            AsteroidBuilder.removePath(tex.name) + "_" + tex.index, scale, BodyDef.BodyType.DynamicBody, pos,
            SolMath.rnd(180), dras, ShipBuilder.SHIP_DENSITY, DraLevel.PROJECTILES, tex);

    body.setAngularVelocity(SolMath.rnd(MAX_ROT_SPD));
    Vector2 spd = SolMath.fromAl(spdAngle, SolMath.rnd(MAX_SPD));
    spd.add(baseSpd);
    body.setLinearVelocity(spd);
    SolMath.free(spd);

    Shard shard = new Shard(body, dras);
    body.setUserData(shard);
    return shard;
}

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;/*from   w  w  w .  jav a 2  s.  c  o  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;
}