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

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

Introduction

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

Prototype

@Override
    public Vector2 sub(Vector2 v) 

Source Link

Usage

From source file:org.saltosion.pixelprophecy.gui.nodes.GUINode.java

License:Open Source License

/**
 * Returns the pixel-perfect location of the node
 * @return //w ww.  j  a v a 2 s  . c  o  m
 */
public Vector2 getWorldLocation() {
    Vector2 loc = localTranslation.cpy();
    Vector2 alignmentOffset = alignment.cpy().scl(getSize()).scl(.5f);
    if (hasParent()) {
        loc.scl(parent.getSize().cpy().scl(.5f)).add(parent.getWorldLocation());
    }
    return loc.sub(alignmentOffset);
}

From source file:org.saltosion.pixelprophecy.input.GameInputAdapter.java

License:Open Source License

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    Vector2 screenSize = new Vector2(Globals.VIEWPORT_HEIGHT * Globals.ASPECT_RATIO, Globals.VIEWPORT_HEIGHT);
    Vector2 pos = new Vector2(screenX * screenSize.x / Gdx.graphics.getWidth(),
            (Gdx.graphics.getHeight() - screenY) * screenSize.y / Gdx.graphics.getHeight());
    OrthographicCamera cam = gameState.getGameCamera();
    pos.add(cam.position.x, cam.position.y);
    pos.sub(screenSize.scl(.5f));
    System.out.println(pos);/*from   ww w  .ja  va  2s. co  m*/
    return true;
}

From source file:releasethekraken.entity.seacreature.EntityShark.java

@Override
public void update() {
    super.update();

    //Move towards enemies

    if (this.world.getWorldTime() % ReleaseTheKraken.TICK_RATE == 0) //Acquire a target every second
        this.target = this.world.getClosestTarget(this, EntityPirate.class, 20, true);

    Vector2 targetPos;

    if (this.target != null)
        targetPos = this.target.getPos(); //Move towards the target
    else//from  w  ww .j  av  a 2s .co  m
        targetPos = this.world.getPathTargetPos(this); //Follow the path

    //Move
    Vector2 difference = targetPos.sub(this.getPos());
    difference.nor().scl(this.moveForce);
    this.physBody.applyForce(difference, this.getPos(), true);

    float angleToTarget = targetPos.sub(this.getPos()).angle();

    //Convert to -180 to 180
    while (angleToTarget > 180)
        angleToTarget -= 360;
    while (angleToTarget < -180)
        angleToTarget += 360;

    //Get the angle difference
    float angle = angleToTarget - (this.physBody.getAngle() * MathUtils.radiansToDegrees);

    //Convert to -180 to 180
    while (angle > 180)
        angle -= 360;
    while (angle < -180)
        angle += 360;

    //Gdx.app.log(this.getClass().getSimpleName(), String.format("target: %20.20s, targetPos: %20.20s, angleToTarget: %10.10s, relativeAngle: %10.10s", this.target, targetPos, angleToTarget, angle));

    //Calculate torque to apply
    float torque = (angle > 0 ? 2000F : -2000F);

    //Rotate towards velocity
    this.physBody.applyTorque(torque, true);

    //The range at which the attack animation starts playing
    float attackRange = 6;

    //Change animation to attack animation if the distance is smaller than attackRange
    if (this.target != null && this.physBody.getPosition().sub(this.target.getPos()).len() < attackRange)
        this.currentAnimation = GameAssets.entitySharkAttackMoveAnimation;
    else
        this.currentAnimation = GameAssets.entitySharkMoveAnimation;
}

From source file:releasethekraken.entity.seacreature.kraken.EntityKraken.java

@Override
public void update() {
    super.update();

    this.hasTarget = false;

    //Find out if any of the tenticles are targeting stuff
    for (EntityKrakenTenticle tenticle : this.tenticles) {
        EntitySeaCreature tenticlePart = tenticle;

        while (tenticlePart != null) {
            if (tenticlePart instanceof EntityKrakenTenticle) {
                //If any of the tenticles has a target, remember it and break out of the loop
                if (((EntityKrakenTenticle) tenticlePart).getTarget() != null) {
                    this.hasTarget = true;
                    break;
                }//from   w  w w .  j  av  a 2 s  .  c o  m
                tenticlePart = ((EntityKrakenTenticle) tenticlePart).getChild(); //Go to next segment
            } else if (tenticlePart instanceof EntityKrakenGripper) {
                //If any of the tenticles has a target, remember it and break out of the loop
                if (((EntityKrakenGripper) tenticlePart).getTarget() != null) {
                    this.hasTarget = true;
                    break;
                }
                tenticlePart = null; //The gripper should be the last segment
            }
        }
    }

    if (!this.hasTarget) //Only follow the path if there is no target
    {
        Vector2 targetPos = this.world.getPathTargetPos(this);
        Vector2 difference = targetPos.sub(this.getPos());
        difference.nor().scl(this.moveForce);
        this.physBody.applyForce(difference, this.getPos(), true);
    } else //Move towards the target
    {
        Entity target = this.world.getClosestTarget(this, EntityPirate.class, 40, true);

        if (target != null) {
            Vector2 targetPos = target.getPos();
            Vector2 difference = targetPos.sub(this.getPos());
            difference.nor().scl(this.moveForce);
            this.physBody.applyForce(difference, this.getPos(), true);
        }
    }

    //Get the angle difference
    float angle = this.getVel().angle() - (this.physBody.getAngle() * MathUtils.radiansToDegrees);

    //Convert to -180 to 180
    while (angle > 180)
        angle -= 180;
    while (angle < -180)
        angle += 180;

    //Calculate torque to apply
    float torque = (angle > 0 ? 500F : -500F);

    //Rotate towards velocity
    this.physBody.applyTorque(torque, true);
}

From source file:releasethekraken.entity.seacreature.kraken.EntityKrakenGripper.java

@Override
public void update() {
    super.update();

    //Move towards enemies

    if (this.world.getWorldTime() % ReleaseTheKraken.TICK_RATE == 0) //Acquire a target every second
        this.target = this.world.getClosestTarget(this, EntityPirate.class, 25, true);

    if (this.target != null) {
        Vector2 targetPos = this.target.getPos();
        Vector2 difference = targetPos.sub(this.getPos());
        difference.nor().scl(this.moveForce);
        this.physBody.applyForce(difference, this.getPos(), true);
    }/*from  w w w . j  av a  2s.co  m*/

    //Get the angle difference
    float angle = this.getVel().angle() - (this.physBody.getAngle() * MathUtils.radiansToDegrees);

    //Convert to -180 to 180
    while (angle > 180)
        angle -= 180;
    while (angle < -180)
        angle += 180;

    //Calculate torque to apply
    float torque = (angle > 0 ? 500F : -500F);

    //Rotate towards velocity
    this.physBody.applyTorque(torque, true);
}

From source file:releasethekraken.entity.seacreature.kraken.EntityKrakenTenticle.java

@Override
public void update() {
    super.update();

    //Move towards enemies

    if (this.world.getWorldTime() % ReleaseTheKraken.TICK_RATE == 0) //Acquire a target every second
        this.target = this.world.getClosestTarget(this, EntityPirate.class, 15, true);

    if (this.target != null) {
        Vector2 targetPos = this.target.getPos();
        Vector2 difference = targetPos.sub(this.getPos());
        difference.nor().scl(this.moveForce);
        this.physBody.applyForce(difference, this.getPos(), true);
    }/*ww  w  . j  av  a  2 s . c o  m*/

    //Get the angle difference
    float angle = this.getVel().angle() - (this.physBody.getAngle() * MathUtils.radiansToDegrees);

    //Convert to -180 to 180
    while (angle > 180)
        angle -= 180;
    while (angle < -180)
        angle += 180;

    //Calculate torque to apply
    float torque = (angle > 0 ? 500F : -500F);

    //Rotate towards velocity
    this.physBody.applyTorque(torque, true);
}

From source file:skyranger.game.InputController.java

License:Apache License

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    //if right mouse button pressed we act as mouse look
    if (this.screen.mouseLook) {
        Vector3 cameraV3 = this.screen.camera.unproject(new Vector3(screenX, screenY, 0));

        Vector2 cameraV2 = new Vector2(cameraV3.x, cameraV3.y);

        // Take the vector that goes from body origin to mouse in camera
        // space//www. j  a v  a2 s .  c o m
        Vector2 bodyPos = this.screen.player.getBody().getPosition();
        Vector2 newCamVector = cameraV2.sub(bodyPos);

        // Now you can set the angle;
        this.screen.player.getBody().setTransform(this.screen.player.getBody().getPosition(),
                newCamVector.angleRad());

    }

    //if left shift pressed and RMB not pressed and we do some mouse drag we move selected object
    if ((this.isLeftShift) && (!this.screen.mouseLook) && (prevDragPos.x > 0) && (prevDragPos.y > 0)) {

        Vector3 dragPos = new Vector3(screenX, screenY, 0);

        this.screen.camera.unproject(dragPos);
        this.screen.camera.unproject(prevDragPos);

        Vector3 delta = new Vector3(dragPos.x - prevDragPos.x, dragPos.y - prevDragPos.y, 0);

        IBox2dObject obj = this.clickCallback.getObject();

        Vector2 objPos = obj.getPosition();

        Vector3 newpos = new Vector3(objPos.x + delta.x, objPos.y + delta.y, 0);

        SwtEvents.changeObjectPosition(obj.getId(), new Vector2(newpos.x, newpos.y));

        this.clickCallback.getObject()
                .setAngle(this.clickCallback.getObject().getBody().getAngle() * MathUtils.radiansToDegrees);

        GameMouseEvent.mouseClickOnObject(obj);

        GameMouseEvent.mouseMoved(new Vector2(screenX, screenY));

    }

    prevDragPos = new Vector3(screenX, screenY, 0);

    return false;
}