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

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

Introduction

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

Prototype

@Override
    public Vector2 nor() 

Source Link

Usage

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  ww .j  a v a 2 s.co 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);
    }//  w w w .jav  a2 s  . com

    //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);
    }/*from www  .  j  a v  a 2  s. com*/

    //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);
}