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

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

Introduction

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

Prototype

public Vector2 getLinearVelocity() 

Source Link

Usage

From source file:com.agateau.pixelwheels.racer.Racer.java

License:Open Source License

private void applySimplifiedRacerCollision(Racer other) {
    Body body1 = getVehicle().getBody();
    Body body2 = other.getVehicle().getBody();

    mTmp.set(body2.getLinearVelocity()).sub(body1.getLinearVelocity());
    float deltaV = mTmp.len();

    final float k = GamePlay.instance.simplifiedCollisionKFactor
            * MathUtils.clamp(deltaV / GamePlay.instance.simplifiedCollisionMaxDeltaV, 0, 1);
    mTmp.set(body2.getWorldCenter()).sub(body1.getWorldCenter()).nor().scl(k);

    body2.applyLinearImpulse(mTmp, body2.getWorldCenter(), true);
    mTmp.scl(-1);/*from   w w w . j  av  a2s . c om*/
    body1.applyLinearImpulse(mTmp, body1.getWorldCenter(), true);
}

From source file:com.agateau.pixelwheels.racer.SpinningComponent.java

License:Open Source License

@Override
public void act(float delta) {
    if (!mActive) {
        return;/*  www.j  a va2  s.  com*/
    }
    Body body = mVehicle.getBody();

    // Slow down
    body.applyLinearImpulse(body.getLinearVelocity().nor().scl(-body.getMass()), body.getWorldCenter(), true);

    // Spin
    float nextAngle = body.getAngle() + body.getAngularVelocity() * GameWorld.BOX2D_TIME_STEP;
    if (nextAngle > mTargetBodyAngle) {
        stopSpinning();
        return;
    }

    float totalRotation = mTargetBodyAngle - nextAngle;
    float desiredAngularVelocity = totalRotation / GameWorld.BOX2D_TIME_STEP;
    if (desiredAngularVelocity < 0) {
        desiredAngularVelocity = MathUtils.clamp(desiredAngularVelocity, -MAX_ANGULAR_VELOCITY,
                -MIN_ANGULAR_VELOCITY);
    } else {
        desiredAngularVelocity = MathUtils.clamp(desiredAngularVelocity, MIN_ANGULAR_VELOCITY,
                MAX_ANGULAR_VELOCITY);
    }
    float impulse = body.getInertia() * (desiredAngularVelocity - body.getAngularVelocity());
    body.applyAngularImpulse(impulse, true);
}

From source file:com.agateau.pixelwheels.utils.Box2DUtils.java

License:Open Source License

@SuppressWarnings("unused")
public static Vector2 getForwardVelocity(Body body) {
    Vector2 currentRightNormal = body.getWorldVector(FORWARD_VECTOR);
    float v = currentRightNormal.dot(body.getLinearVelocity());
    return currentRightNormal.scl(v);
}

From source file:com.agateau.pixelwheels.utils.Box2DUtils.java

License:Open Source License

public static Vector2 getLateralVelocity(Body body) {
    Vector2 currentRightNormal = body.getWorldVector(LATERAL_VECTOR);
    float v = currentRightNormal.dot(body.getLinearVelocity());
    return currentRightNormal.scl(v);
}

From source file:com.agateau.pixelwheels.utils.Box2DUtils.java

License:Open Source License

public static void applyDrag(Body body, float factor) {
    Vector2 dragForce = body.getLinearVelocity().scl(-factor);
    body.applyForce(dragForce, body.getWorldCenter(), true);
}

From source file:com.blindtigergames.werescrewed.debug.SBox2DDebugRenderer.java

License:Apache License

protected void renderBody(Body body) {
    Transform transform = body.getTransform();
    int len = body.getFixtureList().size();
    List<Fixture> fixtures = body.getFixtureList();
    for (int i = 0; i < len; i++) {
        Fixture fixture = fixtures.get(i);

        if (drawBodies) {
            if (body.isActive() == false)
                drawShape(fixture, transform, SHAPE_NOT_ACTIVE);
            else if (body.getType() == BodyType.StaticBody)
                drawShape(fixture, transform, SHAPE_STATIC);
            else if (body.getType() == BodyType.KinematicBody)
                drawShape(fixture, transform, SHAPE_KINEMATIC);
            else if (body.isAwake() == false)
                drawShape(fixture, transform, SHAPE_NOT_AWAKE);
            else/*from  ww w  .j  a  va  2s .  c  o m*/
                drawShape(fixture, transform, SHAPE_AWAKE);

            if (drawVelocities) {
                Vector2 position = body.getPosition();
                drawSegment(position, body.getLinearVelocity().add(position), VELOCITY_COLOR);
            }
        }

        if (drawAABBs) {
            drawAABB(fixture, transform);
        }
    }
}

From source file:com.dongbat.game.util.MovementUtil.java

public static void newMovementMechanism(World world, Entity e) {
    UnitMovement unitMovement = EntityUtil.getComponent(world, e, UnitMovement.class);

    Physics physicsComponent = EntityUtil.getComponent(world, e, Physics.class);
    Body body = physicsComponent.getBody();
    float mass = body.getMass();
    if (unitMovement.getDirectionVelocity() == null) {
        //      body.setLinearVelocity(new Vector2(0, 0));
        return;//from  w w  w. j av  a2s. co m
    }

    Vector2 directionVel = unitMovement.getDirectionVelocity().cpy();

    //heading
    float angleRad = body.getLinearVelocity().angleRad();
    body.setTransform(body.getPosition(), angleRad);

    // TODO: calculate based on radius, use fixed mass
    // or make a steering-like behavior with real mass
    float desiredSpd = calculalteDesiredSpeed(world, e);
    Vector2 currentVector = body.getLinearVelocity();
    directionVel.nor().scl(desiredSpd).sub(currentVector);

    Vector2 impulse = directionVel.scl(mass);

    PhysicsUtil.applyImpulse(world, e, impulse);
}

From source file:com.dongbat.game.util.MovementUtil.java

/**
 * Move unit to specific location on map
 *
 * @param entity entity want to move//from w ww .  j  a  v a2  s.co m
 * @param destination location that entity will move to
 */
public static void moveTo(Entity entity, Vector2 destination) {
    World world = entity.getWorld();
    Physics physicsComponent = EntityUtil.getComponent(world, entity, Physics.class);
    Stats stat = EntityUtil.getComponent(world, entity, Stats.class);
    Body body = physicsComponent.getBody();
    float mass = body.getMass();
    Vector2 currentVelocity = body.getLinearVelocity();
    Vector2 position = body.getPosition();
    float scale = stat.getBaseMovementSpeed() + stat.getModifierSpeed();
    Vector2 desiredVelocity = destination.cpy().sub(position).nor().scl(scale);

    Vector2 impulse = desiredVelocity.sub(currentVelocity).scl(mass * 10);

    PhysicsUtil.applyImpulse(entity.getWorld(), entity, impulse);
}

From source file:com.dongbat.invasion.util.MovementUtil.java

public static void moveTo(Entity entity, Vector2 destination) {
    Physics physicsComponent = EntityUtil.getComponent(entity, Physics.class);
    Body body = physicsComponent.getBody();
    float mass = body.getMass();
    Vector2 currentVelocity = body.getLinearVelocity();
    Vector2 position = body.getPosition();

    Vector2 desiredVelocity = destination.cpy().sub(position).nor().scl(physicsComponent.getMaxSpeed());

    Vector2 impulse = desiredVelocity.sub(currentVelocity).scl(mass);

    PhysicsUtil.applyImpulse(entity, impulse);
}

From source file:com.gmail.emersonmx.asteroids.system.PhysicSystem.java

License:Open Source License

private void processOutputEntity(Entity entity) {
    TransformComponent transform = transformMapper.get(entity);
    PhysicBodyComponent bodyComponent = physicBodyMapper.get(entity);
    Body body = bodyComponent.body;

    Vector2 position = body.getPosition();
    transform.position = ConverterUtil.unitToPixel(position);
    transform.rotation = body.getAngle() * MathUtils.radiansToDegrees;
    System.out.println("velocity: " + body.getLinearVelocity());
}