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

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

Introduction

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

Prototype

public float angle() 

Source Link

Usage

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

public static void applyImpulseOnEnemy(Entity entity, Vector2 impulse) {
    if (EntityUtil.isEnemy(entity)) {
        float angle = impulse.angle();
        float rotateAngle = angle > 90 ? 180 - angle : -angle;
        Vector2 apply = impulse.rotate(rotateAngle);
        applyImpulse(entity, apply);/*from   w  w  w .  ja  v  a  2 s .  c o  m*/
    }
}

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

public static void applyForceOnEnemy(Entity entity, Vector2 force) {
    if (EntityUtil.isEnemy(entity)) {
        float angle = force.angle();
        float rotateAngle = angle > 90 ? 180 - angle : -angle;
        Vector2 apply = force.rotate(rotateAngle);
        applyForce(entity, apply);//  w  w w.  j  av a  2s.  co  m
    }
}

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

License:Open Source License

public Hook(GameWorld gameWorld, Player.HookShooter shooter, Vector2 direction, float launchSpeed) {
    this.gameWorld = gameWorld;
    this.shooter = shooter;

    Vector2 dir = direction.cpy();

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(shooter.getPlayer().getLaunchPosition());
    bodyDef.angle = (float) Math.toRadians(dir.angle());
    bodyDef.bullet = true;// w  w w. ja v  a  2s  .  com
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.linearVelocity.set(dir.nor().scl(launchSpeed));

    body = shooter.getPlayer().getWorld().createBody(bodyDef);

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(0.15f, 0.1f, new Vector2(0.15f, 0), 0);

    body.createFixture(shape, 0.01f).setSensor(true);

    shape.dispose();

    body.setUserData(this);

    // maybe we can move all the definitions to fields?
}

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.ja va 2  s .  c om
    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.o2d.pkayjava.editor.view.stage.tools.TransformTool.java

License:Apache License

@Override
public void anchorDown(NormalSelectionFollower follower, int anchor, float x, float y) {
    Sandbox sandbox = Sandbox.getInstance();

    commandBuilder.begin(follower.getEntity());

    TransformComponent transformComponent = ComponentRetriever.get(follower.getEntity(),
            TransformComponent.class);
    DimensionsComponent dimensionsComponent = ComponentRetriever.get(follower.getEntity(),
            DimensionsComponent.class);
    if (anchor == NormalSelectionFollower.ROTATION_LT || anchor == NormalSelectionFollower.ROTATION_RT
            || anchor == NormalSelectionFollower.ROTATION_RB || anchor == NormalSelectionFollower.ROTATION_LB) {

        // get mouse stage coordinates

        Vector2 mousePoint = sandbox.screenToWorld(x, y);
        Vector2 originPoint = new Vector2(transformComponent.getX() + transformComponent.getOriginX(),
                transformComponent.getY() + transformComponent.getOriginY());
        mousePoint.sub(originPoint);//from   w w  w  .j a  va2  s  . c o m

        lastTransformAngle = mousePoint.angle();
        lastEntityAngle = transformComponent.getRotation();

    }

}

From source file:com.o2d.pkayjava.editor.view.stage.tools.TransformTool.java

License:Apache License

private void defaultAnchorDraggedLogic(Vector2 mousePointStage, int anchor, Entity entity) {
    TransformComponent transformComponent = ComponentRetriever.get(entity, TransformComponent.class);
    DimensionsComponent dimensionsComponent = ComponentRetriever.get(entity, DimensionsComponent.class);

    float newX = transformComponent.getX();
    float newY = transformComponent.getY();
    float newWidth = dimensionsComponent.width * transformComponent.getScaleX();
    float newHeight = dimensionsComponent.height * transformComponent.getScaleY();

    float tmpAdjustmentX = transformComponent.getOriginX() * (transformComponent.getScaleX() - 1);
    float tmpAdjustmentY = transformComponent.getOriginY() * (transformComponent.getScaleY() - 1);

    final float cos = MathUtils.cosDeg(transformComponent.getRotation());
    final float sin = MathUtils.sinDeg(transformComponent.getRotation());

    float difX = (transformComponent.getX() - mousePointStage.x);
    float difY = (transformComponent.getY() - mousePointStage.y);

    difX = (difX * cos + difY * sin);/*w  ww. j  a  v a  2s .  c om*/
    difY = (difX * -sin + difY * cos);

    switch (anchor) {
    case NormalSelectionFollower.L:
        newWidth = dimensionsComponent.width + difX * 2;
        break;
    case NormalSelectionFollower.R:
        newWidth = tmpAdjustmentX - difX;
        break;
    case NormalSelectionFollower.B:
        newHeight = dimensionsComponent.height + difY * 2;
        break;
    case NormalSelectionFollower.T:
        newHeight = tmpAdjustmentY - difY;
        break;
    case NormalSelectionFollower.LT:
        newWidth = dimensionsComponent.width + difX * 2;
        newHeight = tmpAdjustmentY - difY;
        break;
    case NormalSelectionFollower.RT:
        newWidth = tmpAdjustmentX - difX;
        newHeight = tmpAdjustmentY - difY;
        break;
    case NormalSelectionFollower.RB:
        newWidth = tmpAdjustmentX - difX;
        newHeight = dimensionsComponent.height + difY * 2;
        break;
    case NormalSelectionFollower.LB:
        newWidth = dimensionsComponent.width + difX * 2;
        newHeight = dimensionsComponent.height + difY * 2;
        break;
    }

    // This was making sure for proportional sizing
    if (Gdx.input.isKeyPressed(Input.Keys.SHIFT_LEFT)) {
        float enclosingRectSize = Math.max(newWidth, newHeight);
        if (dimensionsComponent.width >= dimensionsComponent.height) {
            newWidth = enclosingRectSize;
            newHeight = (newWidth / dimensionsComponent.width) * dimensionsComponent.height;
        }
        if (dimensionsComponent.height > dimensionsComponent.width) {
            newHeight = enclosingRectSize;
            newWidth = (newHeight / dimensionsComponent.height) * dimensionsComponent.width;
        }

    }

    // Rotating
    if (anchor >= NormalSelectionFollower.ROTATION_LT && anchor <= NormalSelectionFollower.ROTATION_LB) {
        Vector2 originPoint = new Vector2(transformComponent.getX() + transformComponent.getOriginX(),
                transformComponent.getY() + transformComponent.getOriginY());
        mousePointStage.sub(originPoint);
        float currentAngle = mousePointStage.angle();
        float angleDiff = currentAngle - lastTransformAngle;
        float newRotation = lastEntityAngle + angleDiff;
        transformComponent.setRotation(newRotation);

        commandBuilder.setRotation(newRotation);
    }

    float newScaleX = newWidth / dimensionsComponent.width;
    float newScaleY = newHeight / dimensionsComponent.height;

    commandBuilder.setScale(newScaleX, newScaleY);
    commandBuilder.setPos(newX, newY);

    transformComponent.setScaleX(newScaleX);
    transformComponent.setScaleY(newScaleY);
    transformComponent.setX(newX);
    transformComponent.setY(newY);
}

From source file:com.octa.topdown.entities.Player.java

License:Open Source License

@Override
public boolean update(float dTime) {
    knock.x = 0;/*w w  w.  ja  v a  2s  . c o m*/
    knock.y = 0;
    this.delta.x = 0;
    this.delta.y = 0;

    // Handle user input
    if (InputTracker.isPressed(InputTracker.DOWN)) {
        this.delta.y = -this.speed * dTime;
    }
    if (InputTracker.isPressed(InputTracker.UP)) {
        this.delta.y = this.speed * dTime;
    }
    if (InputTracker.isPressed(InputTracker.RIGHT)) {
        this.delta.x = this.speed * dTime;
    }
    if (InputTracker.isPressed(InputTracker.LEFT)) {
        this.delta.x = -this.speed * dTime;
    }

    if (InputTracker.isJustReleased(InputTracker.UP) || InputTracker.isJustReleased(InputTracker.DOWN)) {
        this.delta.y = 0;
    }
    if (InputTracker.isJustReleased(InputTracker.LEFT) || InputTracker.isJustReleased(InputTracker.RIGHT)) {
        this.delta.x = 0;
    }
    if (InputTracker.isJustReleased(InputTracker.SPACE)) {
        arm = arm == gun ? stick : gun;
    }

    // Animate sprite if moving
    if (this.delta.x != 0 || this.delta.y != 0) {
        updateAnim();
    }

    // Cast a vector from the player's position in the screen(always the center)
    // to the mouse pointer. Then, get that vector's angle and set it to the sprite's
    // rotation, so the sprite "faces" the mouse.
    float midX = Gdx.graphics.getWidth() / 2;
    float midY = Gdx.graphics.getHeight() / 2;
    float mouseX = InputTracker.getMousePos().x;
    float mouseY = Gdx.graphics.getHeight() - InputTracker.getMousePos().y;

    Vector2 dir = new Vector2(mouseX - midX, mouseY - midY);
    dir.rotate90(-1);
    this.setRotation(dir.angle());

    // Update player's position
    this.setX(this.getX() + this.delta.x);
    this.setY(this.getY() + this.delta.y);

    if (Gdx.input.isTouched()) {
        float px = this.getX() + (this.getWidth() / 2);
        float py = this.getY() + (this.getHeight() / 2);

        px -= SinCosTable.getSin((int) this.getRotation() + 20) * 23;
        py += SinCosTable.getCos((int) this.getRotation() + 20) * 23;

        if (!arm.isMeele()) {
            if (ammo > 0) {
                ammo--;
                arm.shoot(px, py, getRotation());
            }
        } else {
            arm.shoot(px, py, getRotation());
        }
    }

    updateAABB();
    stick.setAABB(AABB);
    return true;
}

From source file:com.octa.topdown.entities.Zombie.java

License:Open Source License

@Override
public boolean update(float dTime) {
    this.delta.x = this.delta.y = 0;
    knock.x = 0;//www . jav a  2s.c  o  m
    knock.y = 0;

    if (this.hp <= 0)
        return false;

    if (objective != null && chaseObj) {

        Vector2 dir = new Vector2(objective.x - this.getX(), objective.y - this.getY());
        dir.rotate90(-1);
        this.setRotation(dir.angle());

        sin = SinCosTable.getSin((int) this.getRotation());
        cos = SinCosTable.getCos((int) this.getRotation());

        delta.x = -sin * speed * dTime;
        delta.y = cos * speed * dTime;

        this.setX(this.getX() + this.delta.x);
        this.setY(this.getY() + this.delta.y);
    }

    if (this.delta.x != 0 || this.delta.y != 0)
        updateAnim();

    updateAABB();
    return true;
}

From source file:com.tnf.ptm.common.PtmMath.java

License:Apache License

/**
 * @return angle of a vector. if not precise, approximation is returned.
 * (1, 0) is right and 0 degrees/*from  w  w  w  . j  a  va2 s. c o  m*/
 * (0, 1) is down and 90 degrees
 * (-1, 0) is left and 180 degrees
 * (0, -1) is up and -90 degrees
 */
public static float angle(Vector2 v, boolean precise) {
    if (precise) {
        return v.angle();
    } else {
        return MathUtils.atan2(v.y, v.x) * radDeg;
    }
}

From source file:com.uwsoft.editor.view.stage.tools.TransformTool.java

License:Apache License

@Override
public void anchorDown(NormalSelectionFollower follower, int anchor, float x, float y) {
    Sandbox sandbox = Sandbox.getInstance();
    TransformComponent transformComponent = ComponentRetriever.get(follower.getEntity(),
            TransformComponent.class);
    DimensionsComponent dimensionsComponent = ComponentRetriever.get(follower.getEntity(),
            DimensionsComponent.class);
    if (anchor == NormalSelectionFollower.ROTATION_LT || anchor == NormalSelectionFollower.ROTATION_RT
            || anchor == NormalSelectionFollower.ROTATION_RB || anchor == NormalSelectionFollower.ROTATION_LB) {

        // get mouse stage coordinates

        Vector2 mousePoint = sandbox.screenToWorld(x, y);
        Vector2 originPoint = new Vector2(transformComponent.x + transformComponent.originX,
                transformComponent.y + transformComponent.originY);
        mousePoint.sub(originPoint);//  w  w w  .  jav a2 s .c o  m

        lastTransformAngle = mousePoint.angle();
        lastEntityAngle = transformComponent.rotation;

    }

}