Example usage for com.badlogic.gdx.math Vector2 scl

List of usage examples for com.badlogic.gdx.math Vector2 scl

Introduction

In this page you can find the example usage for com.badlogic.gdx.math Vector2 scl.

Prototype

@Override
    public Vector2 scl(Vector2 v) 

Source Link

Usage

From source file:AITest.Box2DSteeringEntity.java

private void applySteering(float delta) {
    boolean anyAccelerations = false;
    if (!steeringOutput.isZero()) {
        Vector2 force = steeringOutput.linear.scl(delta);
        body.applyForceToCenter(force, true);
        anyAccelerations = true;/*from w  ww .  j a v a2s . c  om*/
    }
    if (steeringOutput.angular != 0) {
        body.applyTorque(steeringOutput.angular * delta, true);
        anyAccelerations = true;
    }
    //        else { 
    //            Vector2 linVel = getLinearVelocity();
    //            if(!linVel.isZero())
    //            {
    //                float newOrientation = vectorToAngle(linVel);
    //                body.setAngularVelocity(newOrientation - getAngularVelocity() * delta);
    //                body.setTransform(body.getPosition(), newOrientation);
    //            }}

    if (anyAccelerations) {
        Vector2 velocity = body.getLinearVelocity();
        float currentSpeedSquare = velocity.len2();
        if (currentSpeedSquare > maxLinearSpeed * maxLinearSpeed) {
            body.setLinearVelocity(velocity.scl(maxLinearSpeed / (float) Math.sqrt(currentSpeedSquare)));
        }
        if (body.getAngularVelocity() > maxAngularSpeed) {
            body.setAngularVelocity(maxAngularSpeed);
        }
    }
}

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

License:Open Source License

private void actStopping(float dt) {
    Vector2 invVelocity = mBody.getLinearVelocity().scl(-0.1f);
    mBody.applyForce(invVelocity.scl(mBody.getMass()).scl(1 / dt), mBody.getWorldCenter(), 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.badlogic.gdx.ai.tests.steer.box2d.Box2dSteeringEntity.java

License:Apache License

protected void applySteering(SteeringAcceleration<Vector2> steering, float deltaTime) {
    boolean anyAccelerations = false;

    // Update position and linear velocity.
    if (!steeringOutput.linear.isZero()) {
        Vector2 force = steeringOutput.linear.scl(deltaTime);
        body.applyForceToCenter(force, true);
        anyAccelerations = true;/* ww w  .j a  va2  s. c  om*/
    }

    // Update orientation and angular velocity
    if (isIndependentFacing()) {
        if (steeringOutput.angular != 0) {
            body.applyTorque(steeringOutput.angular * deltaTime, true);
            anyAccelerations = true;
        }
    } else {
        // If we haven't got any velocity, then we can do nothing.
        Vector2 linVel = getLinearVelocity();
        if (!linVel.isZero(MathUtils.FLOAT_ROUNDING_ERROR)) {
            float newOrientation = vectorToAngle(linVel);
            body.setAngularVelocity((newOrientation - getAngularVelocity()) * deltaTime); // this is superfluous if independentFacing is always true
            body.setTransform(body.getPosition(), newOrientation);
        }
    }

    if (anyAccelerations) {
        // body.activate();

        // TODO:
        // Looks like truncating speeds here after applying forces doesn't work as expected.
        // We should likely cap speeds form inside an InternalTickCallback, see
        // http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Simulation_Tick_Callbacks

        // Cap the linear speed
        Vector2 velocity = body.getLinearVelocity();
        float currentSpeedSquare = velocity.len2();
        float maxLinearSpeed = getMaxLinearSpeed();
        if (currentSpeedSquare > maxLinearSpeed * maxLinearSpeed) {
            body.setLinearVelocity(velocity.scl(maxLinearSpeed / (float) Math.sqrt(currentSpeedSquare)));
        }

        // Cap the angular speed
        float maxAngVelocity = getMaxAngularSpeed();
        if (body.getAngularVelocity() > maxAngVelocity) {
            body.setAngularVelocity(maxAngVelocity);
        }
    }
}

From source file:com.dongbat.game.buff.effects.AttractedToSource.java

@Override
public void update(World world, Entity source, Entity target) {
    if (null == source || !source.isActive() || PhysicsUtil.getBody(world, source) == null) {
        return;//from ww w.j  a v  a  2 s  . c o  m
    }
    Vector2 impulse = PhysicsUtil.getPosition(world, source).cpy().sub(PhysicsUtil.getPosition(world, target))
            .nor().scl(forceStrength);
    if (ignoreMass) {
        impulse.scl(PhysicsUtil.getBody(world, target).getMass());
    }
    PhysicsUtil.applyImpulse(world, target, impulse);
}

From source file:com.dongbat.game.buff.effects.Forced.java

@Override
public void update(World world, Entity source, Entity target) {
    Vector2 impulse = direction.cpy().nor().scl(forceStrength);
    if (ignoreMass) {
        impulse.scl(PhysicsUtil.getBody(world, target).getMass());
    }//ww w  . j  av  a  2 s. com
    PhysicsUtil.applyImpulse(world, target, impulse);
}

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  ww .j av  a  2 s  .  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.invasion.system.EnemyMovementSystem.java

@Override
protected void process(Entity entity) {
    Delay delay = delayMapper.get(entity);
    if (delay != null && delay.getDuration() > 0) {
        long timePassed = TimeUtil.getGameTimeSince(delay.getStartTime());
        if (timePassed <= delay.getDuration()) {
            return;
        }//  w  w  w . ja  v a  2 s .  c  o m
    }

    //    Vector2 prevVelocity = PhysicsUtil.getVelocity(entity).cpy();
    Vector2 position = PhysicsUtil.getPosition(entity);
    EnemyMovement movement = EntityUtil.getComponent(entity, EnemyMovement.class);
    PhysicsUtil.setVelocity(entity, new Vector2());
    Vector2 intent = movement.getIntent().cpy();
    Vector2 travel;
    MovementUtil.moveEnemyTo(entity, intent);
    // TODO check free enemy
    intent.y = position.y;
    // TODO free movement unit use moveTo
    Vector2 distance = intent.cpy().sub(position);
    travel = PhysicsUtil.getVelocity(entity).cpy().scl(world.delta);
    if (travel.dot(distance) > 0 && travel.len2() - distance.len2() > 0) {
        PhysicsUtil.setPosition(entity, intent);
        PhysicsUtil.setVelocity(entity, new Vector2());
    }

    if (TimeUtil.getGameTimeSince(movement.getStartDrag()) >= movement.getDragDuration()) {
        movement.setDraggedTo(null);
    }

    if (movement.getDraggedTo() == null) {
        return;
    }

    float max = movement.getDraggedTo().cpy().sub(movement.getDraggedFrom()).len2();
    float actual = position.cpy().sub(movement.getDraggedFrom()).len2();
    if (actual >= max) {
        return;
    }

    Vector2 drag = movement.getDraggedTo().cpy().sub(position);
    float dragLen2 = drag.len2();
    if (dragLen2 > movement.getDragForce() * movement.getDragForce()) {
        drag.nor().scl(movement.getDragForce());
    }
    PhysicsUtil.applyImpulseOnEnemy(entity, drag);

    // TODO fix this similar to above
    travel = PhysicsUtil.getVelocity(entity).cpy().scl(world.delta);
    if (travel.sub(drag).len2() <= Constants.GAME.EPSILON_SQUARED) {
        PhysicsUtil.setPosition(entity, movement.getDraggedTo());
        PhysicsUtil.applyImpulseOnEnemy(entity, drag.scl(-1));
    }
}

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

private static Vector2 calculateImpulse(Vector2 fireVector, int power, float angle) {
    Vector2 impulse = new Vector2(fireVector);
    // TODO: -40 =.=
    return impulse.scl(power).rotate(angle).scl(-40);
}