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

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

Introduction

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

Prototype

@Override
    public Vector2 setZero() 

Source Link

Usage

From source file:com.ore.infinium.systems.MovementSystem.java

License:Open Source License

private void simulateDroppedItem(Entity item, float delta) {
    ItemComponent itemComponent = itemMapper.get(item);
    if (itemComponent.state != ItemComponent.State.DroppedInWorld) {
        return;//from  w w  w.j a  v  a  2  s.c  o  m
        //only interested in simulating gravity for dropped items
    }

    SpriteComponent itemSpriteComponent = spriteMapper.get(item);
    VelocityComponent itemVelocityComponent = velocityMapper.get(item);

    Vector2 itemPosition = new Vector2(itemSpriteComponent.sprite.getX(), itemSpriteComponent.sprite.getY());

    int x = (int) (itemPosition.x / World.BLOCK_SIZE);
    int y = (int) (itemPosition.y / World.BLOCK_SIZE);

    Entity playerWhoDropped = m_world.playerForID(itemComponent.playerIdWhoDropped);

    VelocityComponent playerVelocityComponent = velocityMapper.get(playerWhoDropped);
    Vector2 playerVelocity = new Vector2(playerVelocityComponent.velocity);

    Vector2 acceleration = new Vector2(0.0f, World.GRAVITY_ACCEL);

    if (itemComponent.justDropped) {
        //acceleration.x += Math.max(playerVelocity.x * 0.5f, World.GRAVITY_ACCEL);
        acceleration.x += 2;//Math.max(playerVelocity.x * 0.5f, World.GRAVITY_ACCEL);
        acceleration.y += -World.GRAVITY_ACCEL * 8.0f;

        //only add player velocity the first tick, as soon as they drop it.
        itemComponent.justDropped = false;
    }

    final Vector2 itemOldVelocity = new Vector2(itemVelocityComponent.velocity);
    Vector2 itemNewVelocity = new Vector2(itemVelocityComponent.velocity);

    itemNewVelocity.add(acceleration);

    itemNewVelocity.x *= 0.95f;

    itemNewVelocity.x = MathUtils.clamp(itemNewVelocity.x, -PlayerComponent.maxMovementSpeed,
            PlayerComponent.maxMovementSpeed);
    //        newVelocity.y = MathUtils.clamp(newVelocity.y, PlayerComponent.jumpVelocity, World.GRAVITY_ACCEL_CLAMP);
    itemNewVelocity.y = MathUtils.clamp(itemNewVelocity.y, -World.GRAVITY_ACCEL_CLAMP * 10,
            World.GRAVITY_ACCEL_CLAMP);

    //clamp both axes between some max/min values..
    //    newVelocity = glm::clamp(newVelocity, glm::vec2(-maxMovementSpeed, PLAYER_JUMP_VELOCITY), glm::vec2(maxMovementSpeed, 9.8f / PIXELS_PER_METER /10.0f));

    //reset velocity once it gets small enough, and consider it non-moved.
    float epsilon = 0.00001f;
    if (Math.abs(itemNewVelocity.x) < epsilon && Math.abs(itemNewVelocity.y) < epsilon) {
        itemNewVelocity.setZero();
    } else {
        itemVelocityComponent.velocity.set(itemNewVelocity);
        Vector2 desiredPosition = itemPosition.add(itemOldVelocity.add(itemNewVelocity.scl(0.5f * delta)));
        Vector2 finalPosition = performCollision(desiredPosition, item);

        //TODO: add threshold to nullify velocity..so we don't infinitely move and thus burn through ticks/packets

        //HACK: obviously
        //    positionComponent->setPosition(desiredPosition);

        //    const glm::vec3 finalPosition ;

        //qCDebug(ORE_IMPORTANT) << "dropped item opsition: " << desiredPosition << " Id: " << entity.getId() << " velcotiy: " << v;

        itemSpriteComponent.sprite.setPosition(finalPosition.x, finalPosition.y);
        maybeSendEntityMoved(item);
    }
}