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

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

Introduction

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

Prototype

@Override
    public Vector2 cpy() 

Source Link

Usage

From source file:com.wotf.game.classes.Items.AirStrike.java

/**
 * {@inheritDoc}//from   www. j  a va2 s  .c  om
 */
@Override
public void activate(Vector2 position, Vector2 mousePos, Vector2 Wind, double grav) {
    for (int i = 0; i <= 5; i++) { //spawn rockets and add to scene  
        Projectile missile = new Projectile(super.getProjectileSprite(), super.getParticle());

        Vector2 projectilePosition = position.cpy();
        projectilePosition.x = 0 + (i * 100);
        projectilePosition.y = ((GameStage) this.getStage()).getHeight();

        mousePos.x += (i * 100);
        //fire: fire from, fire towards, power, wind, gravity, radius, damage
        missile.fire(projectilePosition, mousePos, super.getPower(), Vector2.Zero, grav, super.getBlastRadius(),
                super.getDamage());
        missile.updateShot();
        ((GameStage) this.getStage()).addActor(missile);
    }
}

From source file:com.wotf.game.classes.Items.Nuke.java

/**
 * {@inheritDoc}/*from   w  w w . j  av  a 2s .c om*/
 */
@Override
public void activate(Vector2 position, Vector2 mousePos, Vector2 Wind, double grav) {
    //spawn bullet and add to scene
    Projectile bullet = super.getBullet();

    //set new fire from position
    Vector2 projectilePosition = position.cpy();
    projectilePosition.y = ((GameStage) this.getStage()).getHeight() + 1;
    projectilePosition.x = mousePos.x;

    bullet.fire(projectilePosition, mousePos, super.getPower(), Vector2.Zero, 8.0, super.getBlastRadius(),
            super.getDamage());
    bullet.updateShot();
    ((GameStage) this.getStage()).addActor(bullet);
}

From source file:com.xemplar.games.android.nerdshooter.entities.Entity.java

License:Open Source License

public void setCheckPoint(Vector2 point) {
    this.spawnPoint = point.cpy();
    this.spawnPoint.add(0.025F, 0);

    if (this.hasInventory()) {
        this.inventory.hitCheckPoint();
    }/* w  w w. ja  v a  2s . c  o  m*/
}

From source file:fr.plafogaj.game.artefact.Artifact.java

License:Open Source License

public Artifact(String fileTexture, Vector2 pos, String sound) {
    m_texture = Game.ASSET_MANAGER.get(fileTexture);
    m_position = pos.cpy();
    m_size = new Vector2(m_texture.getWidth() * TiledMapConfig.TILE_UNIT_SCALE,
            m_texture.getHeight() * TiledMapConfig.TILE_UNIT_SCALE);
    m_rectOverlaps = new Rectangle(m_position.x, m_position.y, m_size.x, m_size.y);
    m_sound = Game.ASSET_MANAGER.get(sound);
}

From source file:fr.plafogaj.game.weapon.longRange.LongRange.java

License:Open Source License

public void moveAngle(boolean down) {
    Vector2 force = m_forceDirection.cpy();
    float angle = (m_forceDirection.x > 0 && down) || (m_forceDirection.x < 0 && !down)
            ? m_velocityAngleMove * -1/*from  w ww .j a  va 2  s . c  om*/
            : m_velocityAngleMove;
    float cosAngle = (float) Math.cos(angle), sinAngle = (float) Math.sin(angle);
    float x = force.x, y = force.y;
    force.x = x * cosAngle - sinAngle * y;

    if ((m_forceDirection.x > 0 && force.x < 0) || (m_forceDirection.x < 0 && force.x > 0))
        return;
    force.y = x * sinAngle + y * cosAngle;
    m_forceDirection = force.cpy();
}

From source file:headmade.arttag.Guard.java

License:Apache License

public void update(ArtTagScreen artTag, float delta) {
    if (body == null) {
        return;//from   w  w w.j a  v a2  s. co  m
    }
    isRunning = isAlert || isCautious;
    moveSpeed = isRunning ? maxMoveSpeed * runFactor
            : isSuspicious ? maxMoveSpeed * 0.75f : isHeardPlayer ? 0.5f : maxMoveSpeed;
    final Vector2 oldMoveVec = targetMoveVec.cpy();

    Vector2 targetPoint = null;
    Float distanceToPlayer = null;
    if (isLightOn) {
        for (final Fixture fixture : playerInView) {
            targetPoint = fixture.getBody().getWorldCenter().cpy();
            final Vector2 diffVec = body.getWorldCenter().cpy().sub(targetPoint);
            boolean seesPlayer = false;
            distanceToPlayer = diffVec.len();
            if (distanceToPlayer < 1f) {
                seesPlayer = true;
            } else {
                final Vector2 outOfBodyVec = diffVec.scl(fixture.getShape().getRadius() * 1.01f);
                targetPoint.add(outOfBodyVec);
                seesPlayer = light.contains(targetPoint.x, targetPoint.y);
            }
            // Gdx.app.log(TAG, light.contains(targetPoint.x, targetPoint.y) + " diffVec.length " + diffVec.len());
            if (seesPlayer) {
                // Gdx.app.log(TAG, "Guard sees player");
                playerLastSeenVec = fixture.getBody().getWorldCenter().cpy();
                if (!isAlert) {
                    Assets.instance.playSound(AssetSounds.whosThere);
                }
                isAlert = true;
                Player.instance.isSpotted = true;
                reactionTime = 0f;
            } else {
                if (isAlert) {
                    Assets.instance.playSound(AssetSounds.huh);
                    reactionTime = MAX_REACTION_TIME;
                    isCautious = true;
                }
                isAlert = false;
            }
        }
    }

    // handle hearing
    if (Player.instance.isRunning && (Player.instance.isMoveDown || Player.instance.isMoveUp
            || Player.instance.isMoveLeft || Player.instance.isMoveRight) && null != Player.instance.body) {
        if (distanceToPlayer == null) {
            distanceToPlayer = body.getWorldCenter().cpy().sub(Player.instance.body.getWorldCenter()).len();
        }
        if (distanceToPlayer < MAX_LIGHT_CONE_LENGTH * 0.9f) {
            sumDeltaSinceHeardPlayer = 0;

            final RayCastCallback callback = new RayCastCallback() {
                @Override
                public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
                    // Gdx.app.log(TAG, "reportRayFixture " + point + " normal " + normal);
                    if (fixture.isSensor()) {
                        // Gdx.app.log(TAG, "Raycast ignoring sensor");
                        return 1;
                    }
                    if (body.equals(fixture.getBody())) {
                        // this is the guards body so there is nothing inbetween them
                        // Gdx.app.log(TAG, "Guard CAN see the point where the noise happened" + Player.instance.body.getWorldCenter());
                        // playerLastHeardVisibleVec = Player.instance.body.getWorldCenter().cpy();
                        return 1;
                    }
                    // Gdx.app.log(TAG, "Fall through");
                    isHearingObstructed = true;
                    return 0;
                }
            };
            try {
                isSuspicious = true;
                playerLastHeardVec = Player.instance.body.getWorldCenter().cpy();
                isHearingObstructed = false;
                // Gdx.app.log(TAG, "###################################");
                // Gdx.app.log(TAG, "Guard " + body.getWorldCenter());
                // Gdx.app.log(TAG, "Player " + playerLastHeardVec);
                artTag.world.rayCast(callback, playerLastHeardVec, body.getWorldCenter());
                if (!isHearingObstructed) {
                    playerLastHeardVisibleVec = Player.instance.body.getWorldCenter().cpy();
                }
            } catch (final Exception e) {
                Gdx.app.error(TAG, "Error Raycasting :(", e);
            }
        } else {
            sumDeltaSinceHeardPlayer += delta;
        }
    } else {
        sumDeltaSinceHeardPlayer += delta;
    }
    isHeardPlayer = playerLastHeardVisibleVec != null || sumDeltaSinceHeardPlayer < 3;

    if (isTouchingPlayerLightCone && Player.instance.isLightOn) {
        // isAlert = true;
    }

    // handle backToPath
    if (isAlert || isCautious || (isSuspicious && playerLastHeardVisibleVec != null)) {
        if (lastVisibleVecBackToPath == null) {
            lastVisibleVecBackToPath = body.getWorldCenter();
        }

        Vector2 checkPoint = path.get(currentPathIndex);
        if (backToPath.size > 0) {
            checkPoint = backToPath.get(backToPath.size - 1);
        }
        if (BODY_RADIUS < checkPoint.dst(body.getWorldCenter())) {
            // not touching checkpoint
            // Gdx.app.log(TAG, "Goard not touching checkpoint");
            final RayCastCallback callback = new RayCastCallback() {
                @Override
                public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
                    // Gdx.app.log(TAG, "reportRayFixture adds new backToPathPoint" + lastVisibleVecBackToPath);
                    backToPath.add(lastVisibleVecBackToPath.cpy());
                    return 0;
                }
            };
            try {
                artTag.world.rayCast(callback, body.getWorldCenter(), checkPoint);
            } catch (final Exception e) {
                Gdx.app.error(TAG, "Error Raycasting :(", e);
            }
        }
        lastVisibleVecBackToPath = body.getWorldCenter();
    }

    // determine targetPoint
    if (isAlert && playerInView.size > 0 && Player.instance.body != null) {
        targetPoint = Player.instance.body.getWorldCenter();
    } else if (isCautious && playerLastSeenVec != null) {
        targetPoint = playerLastSeenVec;
        if (BODY_RADIUS / 10 > targetPoint.dst2(body.getPosition())) {
            // Lost player
            Assets.instance.playSound(AssetSounds.hm);
            Gdx.app.log(TAG, "Guard no longer cautious");
            isCautious = false;
            isSuspicious = true;
        }
    } else if (isSuspicious && playerLastHeardVisibleVec != null) {
        targetPoint = playerLastHeardVisibleVec;
        // Gdx.app.log(TAG, "Guard going to playerLastHeardVisibleVec");
        if (BODY_RADIUS / 10 > targetPoint.dst2(body.getPosition())) {
            // Lost player
            Assets.instance.playSound(AssetSounds.hm);
            Gdx.app.log(TAG, "Guard no longer suspicious");
            isSuspicious = false;
            playerLastHeardVisibleVec = null;
        }
    } else {
        lastVisibleVecBackToPath = null;
        if (backToPath.size > 0) {
            // following Path back to path
            targetPoint = backToPath.get(backToPath.size - 1);
            if (BODY_RADIUS / 10 > targetPoint.dst(body.getPosition())) {
                // Gdx.app.log(TAG, "Guard reached target back to path point " + targetPoint);
                backToPath.pop();
            }
        } else {
            // following path
            isSuspicious = false;
            targetPoint = path.get(currentPathIndex);
            if (BODY_RADIUS > targetPoint.dst(body.getPosition())) {
                // Gdx.app.log(TAG, "Guard reached target point " + targetPoint);
                currentPathIndex++;
                if (currentPathIndex >= path.size) {
                    currentPathIndex = 0;
                }
                targetPoint = path.get(currentPathIndex);
                // Gdx.app.log(TAG, "New target point " + targetPoint);
            }
        }

    }

    targetMoveVec = targetPoint.cpy().sub(body.getPosition());
    targetMoveVec.nor().scl(moveSpeed);

    if (MathUtils.isEqual(0f, oldMoveVec.angle(targetMoveVec))) {
        sumDeltaSinceMoveChange += delta;
    } else {
        // movment direction changed
        sumDeltaSinceMoveChange = 0f;
    }

    final float alpha = reactionTime > 0 ? MathUtils.clamp(sumDeltaSinceMoveChange / reactionTime, 0.1f, 1f)
            : 1f;
    body.setLinearVelocity(targetMoveVec);

    final Vector2 bodyRotVec = new Vector2(1f, 0f);
    bodyRotVec.setAngleRad(body.getAngle());
    float angleDiff;
    if (!isAlert && !isSuspicious && isHeardPlayer && null == playerLastHeardVisibleVec
            && null != playerLastHeardVec) {
        // look at last heard
        angleDiff = bodyRotVec.angleRad(playerLastHeardVec.cpy().sub(body.getWorldCenter()).rotate90(-1));
    } else {
        angleDiff = bodyRotVec.angleRad(targetMoveVec.cpy().rotate90(-1));
    }
    final float rotByRad = MathUtils.clamp(angleDiff, -(MAX_ROTATION_SPEED * delta) / reactionTime,
            MAX_ROTATION_SPEED * delta / reactionTime);
    // Gdx.app.log(TAG, "angleDiff: " + angleDiff + " rotByRad: " + rotByRad + " bodyRotVec: " + bodyRotVec + " - targetMoveVec:
    // "
    // + targetMoveVec);

    // is moving?
    if (!MathUtils.isEqual(targetMoveVec.len2(), 0f)) {
        if (Player.instance.body != null) {
            final float dist = body.getPosition().dst(Player.instance.body.getPosition());
            float volume = isRunning ? STEP_VOLUME * 2 : STEP_VOLUME;
            if (dist > 1) {
                volume = volume / dist;
            }
            sound.setVolume(stepSoundId, volume);
            sound.setPitch(stepSoundId, isRunning ? runFactor : 1f);
            body.setTransform(body.getPosition(), body.getAngle() + rotByRad);
        }
    } else {
        sound.setVolume(stepSoundId, 0f);
        sound.setPitch(stepSoundId, 1f);
    }

    light.setActive(isLightOn);
}

From source file:headmade.arttag.Player.java

License:Apache License

public void update(ArtTagScreen artTag, float delta) {
    deltaInLevel += delta;/* w w w .j a va 2 s .com*/

    if (body == null) {
        return;
    }

    if (hintText != null) {
        artTag.setInstruction(hintText);
    }

    final float moveSpeed = isRunning ? walkSpeed * runFactor : walkSpeed;
    final Vector2 oldMoveVec = targetMoveVec.cpy();
    targetMoveVec.x = 0f;
    targetMoveVec.y = 0f;
    if (isMoveDown) {
        targetMoveVec.add(0, -moveSpeed);
    }
    if (isMoveUp) {
        targetMoveVec.add(0, moveSpeed);
    }
    if (isMoveLeft) {
        targetMoveVec.add(-moveSpeed, 0);
    }
    if (isMoveRight) {
        targetMoveVec.add(moveSpeed, 0);
    }
    targetMoveVec.nor().scl(moveSpeed);

    if (MathUtils.isEqual(0f, oldMoveVec.angle(targetMoveVec))) {
        sumDeltaSinceMoveChange += delta;
    } else {
        // movment direction changed
        sumDeltaSinceMoveChange = 0f;
    }

    final float alpha = reactionTime > 0 ? MathUtils.clamp(sumDeltaSinceMoveChange / reactionTime, 0.1f, 1f)
            : 1f;
    // Gdx.app.log(TAG, "oldMoveVec " + oldMoveVec + " targetMoveVec" + targetMoveVec + "sumDeltaSinceMoveChange "
    // + sumDeltaSinceMoveChange + " - alpha " + alpha);
    // moveVec.interpolate(targetMoveVec, alpha, Interpolation.linear);
    body.setLinearVelocity(targetMoveVec);

    final Vector2 bodyRotVec = new Vector2(1f, 0f);
    bodyRotVec.setAngleRad(body.getAngle());
    final float angleDiff = bodyRotVec.angleRad(targetMoveVec.cpy().rotate90(-1));
    final float rotByRad = MathUtils.clamp(angleDiff, -(MAX_ROTATION_SPEED * delta) / reactionTime,
            MAX_ROTATION_SPEED * delta / reactionTime);
    // Gdx.app.log(TAG, "angleDiff: " + angleDiff + " rotByRad: " + rotByRad + " bodyRotVec: " + bodyRotVec + " - targetMoveVec:
    // "
    // + targetMoveVec);

    // is player moving?
    if (!MathUtils.isEqual(targetMoveVec.len2(), 0f)) {
        getStepSound().setVolume(stepSoundId, isRunning ? STEP_VOLUME * 2 : STEP_VOLUME);
        getStepSound().setPitch(stepSoundId, isRunning ? runFactor : 1f);
        body.setTransform(body.getPosition(), body.getAngle() + rotByRad);
    } else {
        getStepSound().setVolume(stepSoundId, 0f);
        getStepSound().setPitch(stepSoundId, 1f);
    }

    playerLight.setActive(isLightOn);

    imageAlpha = 0f;
    if (isLightOn) {
        for (final Fixture fixture : artInView) {
            final Vector2 point = fixture.getBody().getWorldCenter();
            final boolean isInLight = Player.instance.playerLight.contains(point.x, point.y);
            if (isInLight || isTouchingArt) {
                final float distance = point.cpy().dst(body.getWorldCenter());
                final float newAlpha = 1 - distance / (playerLightLength * PLAYERLIGHT_CONE_LENGTH_FACTOR);
                if (newAlpha > imageAlpha) {
                    // Gdx.app.log(TAG, "distance " + distance + " alpha " + newAlpha);
                    imageAlpha = newAlpha;
                    if (!((Art) fixture.getBody().getUserData()).equals(artTag.getCurrentArt())) {
                        artTag.setCurrentArt((Art) fixture.getBody().getUserData());
                    }
                    if (artTag.getCurrentArt().isScanned()) {
                        artTag.setInstruction(MSG_SCAN_3);
                    } else {
                        artTag.setInstruction(MSG_SCAN);
                    }
                }
            }
        }
    }

    if (inventory.size >= carryCacity) {
        artTag.setInstruction(MSG_INVENTORY_FULL);
    }
    if (isTouchingExit) {
        artTag.setInstruction(MSG_DOOR);
    }

    if (isScanning) {
        artTag.setInstruction(MSG_SCAN_2);
        if (!isTouchingArt || !isLightOn) {
            // abort scan
            isScanning = false;
            sumDeltaScan = 0f;
            // TODO cancel scan sound
        } else {
            scanProgress = Math.round(sumDeltaScan / scanTime * new Float(SCANNING_PROGRESS.length - 1));// (scanProgress + 1) %
            // SCANNING_PROGRESS.length;
            artTag.setResult(" Scanning " + SCANNING_PROGRESS[scanProgress]);
            sumDeltaScan += delta;
            if (sumDeltaScan > scanTime) {
                artTag.getCurrentArt().onScanFinished(artTag);
                artTag.setResult(artTag.getCurrentArt().resultText());
                isScanning = false;
                sumDeltaScan = 0f;
                // TODO cancel scan sound
                artTag.getCurrentArt().setScanned(true);
                artTag.setInstruction(MSG_SCAN_3);
                TagService.instance.tag(artTag.getCurrentArt(), artTag.jobDescription);
            }
        }
    }
    isAbleToScan = !isScanning && isTouchingArt && artTag.getCurrentArt() != null
            && !artTag.getCurrentArt().isScanned();
    isAbleToSteal = isTouchingArt && artTag.getCurrentArt() != null && artTag.getCurrentArt().isScanned()
            && inventory.size < carryCacity;
}

From source file:headmade.arttag.utils.MapUtils.java

License:Apache License

private static void createConeLight(ArtTagScreen artTagScreen, PolygonMapObject mapObject, float unitScale) {
    final Polygon poly = mapObject.getPolygon();
    Float objRot = 0f;/*from ww  w .  j av a 2s.c  om*/
    if (null != mapObject.getProperties()) {
        objRot = mapObject.getProperties().get("rotation", Float.class);
    }
    if (objRot == null) {
        objRot = 0f;
    }
    final float[] vertices = poly.getVertices();// getTransformedVertices();
    if (vertices.length < 6) {
        Gdx.app.error(TAG, "Invalid Polygon for conelight. It has less than 3 vertices " + mapObject);
        return;
    }
    final Array<Vector2> vecs = new Array<Vector2>();
    for (int i = 0; i < 6; i += 2) {
        vecs.add(new Vector2(vertices[i] * unitScale, vertices[i + 1] * unitScale));
    }

    final Color color = getColor(mapObject);
    final Vector2 halfBetweenV1AndV2 = vecs.get(2).cpy().add(vecs.get(1).cpy().sub(vecs.get(2)).scl(0.5f));
    final float length = vecs.get(2).dst(vecs.first());
    final float angle = Math.abs(vecs.get(1).angle(vecs.get(2)));
    final float rotation = halfBetweenV1AndV2.cpy().sub(vecs.first()).angle() - objRot;
    // final float rotation = poly.getRotation();
    // Gdx.app.log(TAG, "rotation " + rotation + " length: " + length + " angle:" + angle);
    final ConeLight light = new ConeLight(artTagScreen.rayHandler, ArtTag.gameSettings.rays, color, length,
            unitScale * poly.getX(), unitScale * poly.getY(), rotation, angle);
    light.setSoftnessLength(0.5f);
    light.setContactFilter(ArtTag.CAT_LIGHT, ArtTag.GROUP_LIGHT, ArtTag.MASK_LIGHT);

    artTagScreen.lights.add(light);
}

From source file:MeshBoneUtil.CreatureManager.java

License:Open Source License

public String ProcessContactBone(Vector2 pt_in, float radius, MeshBone bone_in) {
    String ret_name = "";
    Vector3 diff_vec = bone_in.getWorldEndPt().cpy().sub(bone_in.getWorldStartPt());

    Vector2 cur_vec = new Vector2(diff_vec.x, diff_vec.y);
    float cur_length = (float) cur_vec.len();

    Vector2 unit_vec = cur_vec.cpy();
    unit_vec.nor();//from   w  w  w  .  ja  va 2s  . co  m

    Vector2 norm_vec = new Vector2(unit_vec.y, unit_vec.x);

    Vector2 src_pt = new Vector2(bone_in.getWorldStartPt().x, bone_in.getWorldStartPt().y);
    Vector2 rel_vec = pt_in.cpy().sub(src_pt);
    float proj = (float) rel_vec.dot(unit_vec);

    if ((proj >= 0) && (proj <= cur_length)) {
        float norm_proj = (float) rel_vec.dot(norm_vec);
        if (norm_proj <= radius) {
            return bone_in.getKey();
        }
    }

    Vector<MeshBone> cur_children = bone_in.getChildren();
    for (MeshBone cur_child : cur_children) {
        ret_name = ProcessContactBone(pt_in, radius, cur_child);
        if (!(ret_name.equals(""))) {
            break;
        }
    }

    return ret_name;
}

From source file:org.destinationsol.game.StarPort.java

License:Apache License

private static Vector2 adjustDesiredPos(SolGame game, StarPort myPort, Vector2 desired) {
    Vector2 newPos = desired;/*from  www.j a  v a 2 s . c  om*/
    List<SolObject> objs = game.getObjMan().getObjs();
    for (SolObject o : objs) {
        if (o instanceof StarPort && o != myPort) {
            StarPort sp = (StarPort) o;
            // Check if the positions overlap
            Vector2 fromPos = sp.getPosition();
            Vector2 distVec = SolMath.distVec(fromPos, desired);
            float distance = SolMath.hypotenuse(distVec.x, distVec.y);
            if (distance <= (float) StarPort.SIZE) {
                distVec.scl((StarPort.SIZE + .5f) / distance);
                newPos = fromPos.cpy().add(distVec);
                Vector2 d2 = SolMath.distVec(fromPos, newPos);
                SolMath.free(d2);
            }
            SolMath.free(distVec);
        }
    }
    return newPos;
}