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(Vector2 v) 

Source Link

Document

Set the linear velocity of the center of mass.

Usage

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

License:Apache License

/**
 * Drop ball into centre.//from  www  .  j  a  va2  s.  co m
 * 
 */
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.nuliy.example.Car.java

public void KillOrthoVelocity(Body body) {
    Vector2 localP = new Vector2(0, 0);
    Vector2 velocity = body.getLinearVelocityFromLocalPoint(localP);

    float r = body.getTransform().getRotation();
    Vector2 sideways = new Vector2((float) -Math.sin(r), (float) Math.cos(r));
    sideways.scl(velocity.dot(sideways));

    body.setLinearVelocity(sideways);
}

From source file:com.pcg.roguelike.entity.systems.MovementSystem.java

@Override
public void processEntity(Entity entity, float deltaTime) {
    Body body = bm.get(entity).body;

    if (body == null) {
        return;/*from  www  . ja v a  2 s  .c  om*/
    }

    Vector2 movement = mm.get(entity).movement.cpy();

    float speed = 1;
    SpeedComponent sc = sm.get(entity);
    if (sc != null) {
        speed = sc.speed;
        movement.scl(speed);
    }

    System.out.println("Movement: " + movement);

    body.setLinearVelocity(movement);
    //body.applyLinearImpulse(movement, body.getPosition(), true);
}

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 {// w ww  .  j ava  2  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, 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  ww  .ja va 2  s . c  o  m*/
    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.planet.PlanetManager.java

License:Apache License

private boolean recoverObj(PtmObject obj, float toNp, float npMinH) {
    if (npMinH < toNp) {
        return false;
    }/*from  w ww . j  a v a2  s .c om*/
    if (!(obj instanceof PtmShip)) {
        return false;
    }
    PtmShip ship = (PtmShip) obj;
    Hull hull = ship.getHull();
    if (hull.config.getType() == HullConfig.Type.STATION) {
        return false;
    }
    float fh = myNearestPlanet.getFullHeight();
    Vector2 npPos = myNearestPlanet.getPos();
    Vector2 toShip = PtmMath.distVec(npPos, ship.getPosition());
    float len = toShip.len();
    if (len == 0) {
        toShip.set(0, fh);
    } else {
        toShip.scl(fh / len);
    }
    toShip.add(npPos);
    Body body = hull.getBody();
    body.setTransform(toShip, 0);
    body.setLinearVelocity(Vector2.Zero);
    PtmMath.free(toShip);
    return true;
}

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);/* w ww  .  j  a va  2s  .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.j a va 2 s  .c o  m*/

    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.Teleport.java

License:Apache License

public void maybeTeleport(PtmGame game, PtmShip owner) {
    if (!myShouldTeleport) {
        return;//  w  w w  .  ja  va2  s.  c  om
    }

    TextureAtlas.AtlasRegion tex = game.getTexMan().getTexture(TEX_PATH);
    float blipSz = owner.getHull().config.getApproxRadius() * 3;
    game.getPartMan().blip(game, owner.getPosition(), PtmMath.rnd(180), blipSz, 1, Vector2.Zero, tex);
    game.getPartMan().blip(game, myNewPos, PtmMath.rnd(180), blipSz, 1, Vector2.Zero, tex);

    float newAngle = owner.getAngle() + myAngle;
    Vector2 newSpd = PtmMath.getVec(owner.getSpd());
    PtmMath.rotate(newSpd, myAngle);

    Body body = owner.getHull().getBody();
    body.setTransform(myNewPos, newAngle * PtmMath.degRad);
    body.setLinearVelocity(newSpd);

    PtmMath.free(newSpd);
}

From source file:de.dsi8.vhackandroidgame.handler.DriveMessageHandler.java

License:Open Source License

/**
 * {@inheritDoc}/*  www .ja  v a 2s .com*/
 */
@Override
public void handleMessage(CommunicationPartner partner, DriveMessage message) throws InvalidMessageException {
    //TODO driveCar
    Body body = this.serverLogic.getCarBody(partner);

    final Vector2 velocity = Vector2Pool.obtain(message.valueX * 5, message.valueY * 5);

    body.setLinearVelocity(velocity);
    Vector2Pool.recycle(velocity);

    final float rotationInRad = (float) Math.atan2(-message.valueX, message.valueY);
    body.setTransform(body.getWorldCenter(), rotationInRad);

    //      carView.car.setRotation(MathUtils.radToDeg(rotationInRad)); // TODO Move to Presentation Handler
}