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

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

Introduction

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

Prototype

@Override
    public Vector2 add(Vector2 v) 

Source Link

Usage

From source file:be.ac.ucl.lfsab1509.bouboule.game.body.Bonus.java

License:Open Source License

/**
 * Constructor for a Bonus object /*from  w ww. j a v a2  s . c  o  m*/
 * @param px/py         : initial position
 * @param angle         : initial rotation
 * @param texRegionPath : Path to the image file
 * @param jsonFile      : Path to the jsonFile if needed ( "" else)
 * @param jsonName      : jsonName of the object ( must match the json file attribute )
 *
 * public Bonus( final float px, final float py, 
 *      final float angle, final String texRegionPath, 
 *      final String jsonFile, final String jsonName, final short bonusType)
 */
public Bonus(final float angle, final String texRegionPath, final String jsonFile, final String jsonName,
        final Entity.BonusType bonusType) {

    super();

    int size = GlobalSettings.ARENAWAYPOINTALLOW.size();
    MapNode node = GlobalSettings.ARENAWAYPOINTALLOW.get(random.nextInt(size));

    Vector2 pos = new Vector2(node.xToPixel() - 32, node.yToPixel() - 32);
    Vector2 radius = new Vector2(node.weightToPixel() * random.nextFloat(), 0);
    radius.rotate(random.nextInt(359));
    pos.add(radius);

    this.texture = new TextureRegion(new Texture(texRegionPath));

    this.sprite = new Sprite(texture);

    MakeBody(0, 0, 0, BodyType.StaticBody, 0, 0, true, pos, angle, jsonFile, jsonName,
            GraphicManager.convertToGame(texture.getRegionWidth()));

    //Ensure that the object don't rotate.
    body.setFixedRotation(true);

    //Create the userData of type Bonus and bonusType
    this.entity = new Entity(Entity.BONUS, true, bonusType);

    body.setUserData(this.entity);

    //Ensure that the body image position is set on the origin defined by 
    //the jsonFile
    if (origin != null) {
        pos = positionVector.cpy();
        pos = pos.sub(origin);
        sprite.setPosition(pos.x, pos.y);
        sprite.setOrigin(origin.x, origin.y);
        sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
    }

    // removed the bonus after a delay
    Timer.Task task = new Timer.Task() {
        @Override
        public void run() {
            entity.setAlive(false);
        }
    };
    Timer timer = new Timer();
    timer.scheduleTask(task, 10f);
}

From source file:com.badlogic.gdx.tests.box2d.Pyramid.java

License:Apache License

@Override
protected void createWorld(World world) {
    {//from  w  w  w . j  a  v a2 s  .c om
        BodyDef bd = new BodyDef();
        Body ground = world.createBody(bd);

        EdgeShape shape = new EdgeShape();
        shape.set(new Vector2(-40, 0), new Vector2(40, 0));
        ground.createFixture(shape, 0.0f);
        shape.dispose();
    }

    {
        float a = 0.5f;
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(a, a);

        Vector2 x = new Vector2(-7.0f, 0.75f);
        Vector2 y = new Vector2();
        Vector2 deltaX = new Vector2(0.5625f, 1.25f);
        Vector2 deltaY = new Vector2(1.125f, 0.0f);

        for (int i = 0; i < 20; i++) {
            y.set(x);

            for (int j = i; j < 20; j++) {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.DynamicBody;
                bd.position.set(y);
                Body body = world.createBody(bd);
                body.createFixture(shape, 5.0f);

                y.add(deltaY);
            }

            x.add(deltaX);
        }

    }
}

From source file:com.codefiddler.libgdx.tetris.domain.Tetrimino.java

License:Apache License

public List<Vector2> getCurrentPosition() {
    List<Vector2> vector2s = getPoints();

    List<Vector2> currentPoints = new ArrayList<Vector2>();
    for (Vector2 point : vector2s) {
        currentPoints.add(point.add(getPosition()));
    }//from  w  ww .j a v  a  2 s  .c o m

    return currentPoints;
}

From source file:com.github.skittishSloth.openSkies.battles.ships.PlayerShip.java

public void render(final SpriteBatch batch, final float delta) {
    final TextureRegion curFrame = getCurrentFrame(delta);
    final int width = curFrame.getRegionWidth();
    final int height = curFrame.getRegionHeight();
    final float originX = width / 2;
    final float originY = height / 2;

    final Vector2 origin = new Vector2(originX, originY);
    origin.add(position);
    final Vector2 lo = laserOffset.cpy();
    lo.setAngle(rotation + 90);//from  ww  w. j  a v  a  2s  .c om
    lo.add(origin);

    laserPosition.set(lo);
    batch.draw(curFrame, position.x, position.y, originX, originY, width, height, 1.0f, 1.0f, getRotation());
}

From source file:com.github.unluckyninja.defenseofhuman.model.entity.Rope.java

License:Open Source License

public Rope(GameWorld gameWorld, Body body, Vector2 direction) {
    this.gameWorld = gameWorld;
    world = body.getWorld();/*from  w  w  w . j  av a 2  s  .  com*/
    bodyA = body;

    // ???
    Vector2 oriPosition = new Vector2(body.getPosition());
    if (direction.equals(Vector2.Zero)) {
        direction = Vector2.X;
    }
    Vector2 dir = new Vector2(direction);
    dir.nor();
    Vector2 distance = dir.scl(width / 2 * 1.25f); // ,?1/4,?

    // ?body
    bodyDef.angle = (float) Math.toRadians(dir.angle());
    bodyDef.position.set(distance.add(oriPosition));
    bodyDef.linearDamping = 0.3f;
    bodyDef.type = BodyDef.BodyType.DynamicBody;

    Body piece = body.getWorld().createBody(bodyDef);
    piece.setGravityScale(0.1f);

    // ?
    // ??,????
    fixtureDef.isSensor = true;
    fixtureDef.density = 0.001f;

    polygonShape.setAsBox(width / 2, height / 2);

    fixtureDef.shape = polygonShape;

    piece.createFixture(fixtureDef);

    // ?
    RevoluteJointDef rjDef = new RevoluteJointDef();
    rjDef.bodyA = body;
    rjDef.bodyB = piece;

    rjDef.localAnchorA.x = 0;
    rjDef.localAnchorA.y = 0;
    rjDef.localAnchorB.x = -width / 2 * 1.25f;
    rjDef.localAnchorB.y = 0;

    joints.add(body.getWorld().createJoint(rjDef));
    pieces.add(piece);

    piece.setUserData(this);

}

From source file:com.jmolina.orb.screens.Level.java

License:Open Source License

/**
 * Calcula las fuerzas de atraccin y repulsin activas sobre el orbe y las aplica.
 *///w w w . j  a  v  a 2 s. c  om
private void computeMagneticFoces() {
    Vector2 force = new Vector2(0, 0);

    for (Situation situation : situationManager.getVisible()) {
        for (Element element : situation.getElements()) {
            if (element instanceof Magnetic) {
                Vector2 partial = ((Magnetic) element).getForce(getOrb().getPosition());
                force.add(partial);
            }
        }
    }

    getOrb().getBody().applyLinearImpulse(force, getOrb().getPosition(), true);
}

From source file:com.laex.cg2d.model.joints.BEPrismaticJoint.java

License:Open Source License

@Override
public void computeLocalAnchors(int ptmRatio) {
    getLocalAnchorA().x = (getSource().getBounds().width / ptmRatio) / 2;
    getLocalAnchorA().y = (getSource().getBounds().height / ptmRatio) / 2;

    getLocalAnchorB().x = getLocalAnchorA().x;
    getLocalAnchorB().y = getLocalAnchorA().y;

    /* Compute world anchor for prismastic joint */
    Vector2 a1 = new Vector2(getSource().getBounds().x, getSource().getBounds().y);
    Vector2 a2 = new Vector2(getTarget().getBounds().x, getTarget().getBounds().y);

    Vector2 avg = a1.add(a2).div(2).div(ptmRatio);

    getWorldAnchor().x = avg.x;//from  w  w w  . ja va2 s. co m
    getWorldAnchor().y = avg.y;
}

From source file:com.mygdx.game.model.Polygon.java

/**
 * Instantly moves the polygon by a fixed amount
 * @param displacement the displacement vector
 *//*from   ww  w. ja  va2 s. c  o m*/
public void bump(Vector2 displacement) {
    // move all the vertices by the same displacement
    for (Vector2 vertex : vertices) {
        vertex.add(displacement);
    }
}

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;/* w w  w .  ja  va2  s .  c om*/
        //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);
    }
}

From source file:com.ore.infinium.World.java

License:Open Source License

private void updateCrosshair() {
    //PlayerComponent playerComponent = playerMapper.get(m_mainPlayer);
    //playerComponent

    SpriteComponent spriteComponent = spriteMapper.get(m_blockPickingCrosshair);

    Vector2 mouse = mousePositionWorldCoords();
    Vector2 crosshairPosition = new Vector2(BLOCK_SIZE * MathUtils.floor(mouse.x / BLOCK_SIZE),
            BLOCK_SIZE * MathUtils.floor(mouse.y / BLOCK_SIZE));

    Vector2 crosshairOriginOffset = new Vector2(spriteComponent.sprite.getWidth() * 0.5f,
            spriteComponent.sprite.getHeight() * 0.5f);

    Vector2 crosshairFinalPosition = crosshairPosition.add(crosshairOriginOffset);

    spriteComponent.sprite.setPosition(crosshairFinalPosition.x, crosshairFinalPosition.y);
}