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

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

Introduction

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

Prototype

@Override
    public float dst(Vector2 v) 

Source Link

Usage

From source file:at.juggle.games.counting.CountingGameModel.java

License:Apache License

public InputResult handleTouch(Vector3 p) {
    Vector2 t = new Vector2(p.x, p.y);
    layout.setText(buttonFont, Integer.toString(numberOfSprites));

    if (goodBallons.length < 1)
        return InputResult.Change;
    if (t.y < posButtonsY + layout.height && !answerIsGiven) { // it's a button press
        // check for the answers:
        for (int i = 0; i < answers.length; i++) {
            String answer = Integer.toString(answers[i]);
            layout.setText(buttonFont, answer);
            float posX = CountingGame.GAME_WIDTH / 4 * (i + 1) - layout.width / 2;
            if (Math.abs(posX - t.x) < layout.width) { // got it!
                if (answers[i] == numberOfSprites)
                    answerIsGiven = true;
            }/*w ww.  jav  a  2s  . co m*/
        }
        return answerIsGiven ? InputResult.Button : InputResult.Nothing;
    } else if (answerIsGiven) { // check for the balloons ...
        int toRemove = -1;
        float dist = Float.MAX_VALUE;
        for (int i = 0; i < goodBallons.length; i++) {
            Sprite sp = goodBallons[i];
            Vector2 s = new Vector2(sp.getX() + sp.getWidth() / 2, sp.getY() + sp.getHeight() / 2);

            // take the nearest one to remove ...
            if (t.dst(s) < Math.min(goodBallons[i].getWidth(), dist)) { // it's a collision
                toRemove = i;
                dist = t.dst(s);
            }
        }
        if (toRemove > -1) {
            Balloon[] temp = new Balloon[goodBallons.length - 1];
            int count = 0;
            for (int i = 0; i < goodBallons.length; i++) {
                if (i != toRemove)
                    temp[count++] = goodBallons[i];
            }
            goodBallons = temp;
            dirty = true;
            return InputResult.Pop;
        }
    } else {
        return InputResult.Nothing;
    }
    return InputResult.Nothing;
}

From source file:at.juggle.games.counting.SortingGameModel.java

License:Apache License

public InputResult handleTouch(Vector3 p) {
    Vector2 t = new Vector2(p.x, p.y);
    layout.setText(buttonFont, Integer.toString(numberOfSprites));

    if (goodBallons.length < 1)
        return InputResult.Change;
    if (true) { // check for the balloons ...
        int toRemove = -1;
        float dist = Float.MAX_VALUE;
        for (int i = 0; i < goodBallons.length; i++) {
            Sprite sp = goodBallons[i];/*from  w w w.ja v a  2  s  . c o m*/
            Vector2 s = new Vector2(sp.getX() + sp.getWidth() / 2, sp.getY() + sp.getHeight() / 2);

            // take the nearest one to remove ...
            if (t.dst(s) < Math.min(goodBallons[i].getWidth(), dist)) { // it's a collision
                if (currentAnswer == goodBallons[i].getNumber()) {
                    toRemove = i;
                    dist = t.dst(s);
                    currentAnswer++;
                }
            }
        }
        if (toRemove > -1) {
            Balloon[] temp = new Balloon[goodBallons.length - 1];
            int count = 0;
            for (int i = 0; i < goodBallons.length; i++) {
                if (i != toRemove)
                    temp[count++] = goodBallons[i];
            }
            goodBallons = temp;
            dirty = true;
            return InputResult.Pop;
        }
    } else {
        return InputResult.Nothing;
    }
    return InputResult.Nothing;
}

From source file:com.bladecoder.engine.actions.GotoAction.java

License:Apache License

/**
 * If 'player' is far from 'actor', we bring it close. If 'player' is closed
 * from 'actor' do nothing./*from   w ww  . j a  v a 2  s  .  co  m*/
 * 
 * TODO: DOESN'T WORK NOW
 * 
 * @param player
 * @param actor
 */
@SuppressWarnings("unused")
private void goNear(CharacterActor player, BaseActor actor, ActionCallback cb) {
    Rectangle rdest = actor.getBBox().getBoundingRectangle();

    // Vector2 p0 = new Vector2(player.getSprite().getX(),
    // player.getSprite().getY());
    Vector2 p0 = new Vector2(player.getX(), player.getY());

    // calculamos el punto ms cercano al objeto
    Vector2 p1 = new Vector2(rdest.x, rdest.y); // izquierda
    Vector2 p2 = new Vector2(rdest.x + rdest.width, rdest.y); // derecha
    Vector2 p3 = new Vector2(rdest.x + rdest.width / 2, rdest.y); // centro
    float d1 = p0.dst(p1);
    float d2 = p0.dst(p2);
    float d3 = p0.dst(p3);
    Vector2 pf;

    if (d1 < d2 && d1 < d3) {
        pf = p1;
    } else if (d2 < d1 && d2 < d3) {
        pf = p2;
    } else {
        pf = p3;
    }

    player.goTo(pf, cb);
}

From source file:com.bladecoder.engine.anim.WalkTween.java

License:Apache License

private void walkToNextStep(CharacterActor target) {
    Vector2 p0 = walkingPath.get(currentStep);
    Vector2 pf = walkingPath.get(currentStep + 1);

    target.startWalkAnim(p0, pf);/*  w w  w.j a  v a2 s. co  m*/

    float s0 = target.getScene().getFakeDepthScale(p0.y);
    float sf = target.getScene().getFakeDepthScale(pf.y);

    //      float segmentDuration = p0.dst(pf)
    //            / (EngineAssetManager.getInstance().getScale() * speed);

    // t = dst/((vf+v0)/2)
    float segmentDuration = p0.dst(pf) / (EngineAssetManager.getInstance().getScale() * speed * (s0 + sf) / 2);

    segmentDuration *= (s0 > sf ? s0 / sf : sf / s0);

    InterpolationMode i = InterpolationMode.LINEAR;

    if (Math.abs(s0 - sf) > .25)
        i = s0 > sf ? InterpolationMode.POW2OUT : InterpolationMode.POW2IN;

    if (currentStep == walkingPath.size() - 2 && walkCb != null) {
        start(target, Type.NO_REPEAT, 1, pf.x, pf.y, segmentDuration, InterpolationMode.LINEAR, i, walkCb);
    } else {
        start(target, Type.NO_REPEAT, 1, pf.x, pf.y, segmentDuration, InterpolationMode.LINEAR, i, null);
    }
}

From source file:com.bladecoder.engine.model.CharacterActor.java

License:Apache License

/**
 * Walking Support// ww  w  . j  a v a 2  s .c  o m
 * 
 * @param pf
 *            Final position to walk
 * @param cb
 *            The action callback
 */
public void goTo(Vector2 pf, ActionCallback cb) {
    EngineLogger.debug(MessageFormat.format("GOTO {0},{1}", pf.x, pf.y));

    Vector2 p0 = new Vector2(bbox.getX(), bbox.getY());

    ArrayList<Vector2> walkingPath = null;

    //
    if (p0.dst(pf) < 2.0f) {
        setPosition(pf.x, pf.y);

        // call the callback
        if (cb != null)
            ActionCallbackQueue.add(cb);

        return;
    }

    if (scene.getPolygonalNavGraph() != null) {
        walkingPath = scene.getPolygonalNavGraph().findPath(p0.x, p0.y, pf.x, pf.y);
    }

    if (walkingPath == null || walkingPath.size() == 0) {
        // call the callback even when the path is empty
        if (cb != null)
            ActionCallbackQueue.add(cb);

        return;
    }

    posTween = new WalkTween();

    ((WalkTween) posTween).start(this, walkingPath, walkingSpeed, cb);
}

From source file:com.jmolina.orb.var.Utils.java

License:Open Source License

/**
 * Calcula la distancia entre 2 puntos//from w w w .  java 2 s . com
 */
public static float distance(Vector2 pointA, Vector2 pointB) {
    return pointA.dst(pointB);
}

From source file:com.kotcrab.vis.editor.module.physicseditor.input.EditionInputProcessor.java

License:Apache License

@Override
public boolean mouseMoved(InputEvent event, float x, float y) {
    RigidBodyModel model = screen.getSelectedModel();
    if (model == null)
        return false;

    // Nearest point computation

    Vector2 p = cameraModule.screenToWorld(x, y);
    screen.nearestPoint = null;//w  w w.  j a  va  2 s .com
    float dist = 0.025f * cameraModule.getCamera().zoom;

    for (Vector2 v : getAllPoints()) {
        if (v.dst(p) < dist)
            screen.nearestPoint = v;
    }

    return false;
}

From source file:com.kotcrab.vis.editor.module.scene.entitymanipulator.tool.PolygonTool.java

License:Apache License

private boolean isPointOnLine(Vector2 start, Vector2 end, float x, float y, float epsilon) {
    tmpVector2.set(x, y);//from   w w w  .j a v a  2s . c o  m
    float d1 = start.dst(tmpVector2) + end.dst(tmpVector2);
    float d2 = start.dst(end);
    return (Math.abs(d1 - d2) <= epsilon);
}

From source file:com.mygdx.environments.EnvNull.EnvNull.java

public void fallingUpdate() {
    if (diveFC.complete) {

        /************************
        COMPLETE THE DIVE/*www  .j  a  va  2s .c  om*/
        ************************/

        //set player to center of section
        EnvironmentManager.player.getBody()
                .setTransform(new Vector2((currentSection.getPos().x + currentSection.getWidth() / 2) / PPM,
                        (currentSection.getPos().y + currentSection.getHeight() / 2) / PPM), 0);
        EnvironmentManager.player.getBody().setLinearVelocity(new Vector2(0, 0));

        //zoom out to normal
        if (fallDown) {
            currentPlayerZoom = currentPlayerZoom >= PLAYER_LAYER_ZOOM ? PLAYER_LAYER_ZOOM
                    : currentPlayerZoom + 0.065f;
        } else {

            //fallUp
            currentPlayerZoom = currentPlayerZoom <= PLAYER_LAYER_ZOOM ? PLAYER_LAYER_ZOOM
                    : currentPlayerZoom + 0.065f;

        }

        //complete current fall, resume play
        if (currentPlayerZoom == PLAYER_LAYER_ZOOM) {

            //BUG FIX (4/22/16) Player body movement on dive complete
            EnvironmentManager.player.getBody().setLinearVelocity(new Vector2(0, 0));

            sm.setState(1);

            currentTopZoom = TOP_LAYER_ZOOM;
            currentPlayerZoom = PLAYER_LAYER_ZOOM;

            diveIn = false;
            diveMovement = 0.1f;

        }

    } else {
        /***************************************
        FALL ZOOM INTO CURRENT SECTION
        ***************************************/

        for (LayerManager lm : layerManagers) {
            lm.updatePitZoom(fallDown);
        }

        //move player towards center of pit section
        Vector2 sectionPos = new Vector2((currentSection.getPos().x + currentSection.getWidth() / 2) / PPM,
                (currentSection.getPos().y + currentSection.getHeight() / 2) / PPM);
        //prevSection
        Vector2 dir = sectionPos.cpy().sub(prevSectionPosition).nor();
        float dist = sectionPos.dst(EnvironmentManager.player.getBody().getPosition());

        //move player to center of currentSection
        EnvironmentManager.player.getBody().setTransform(prevSectionPosition.add(dir.scl(diveMovement * dist)),
                0);
        diveMovement = diveMovement >= 1 ? 1 : diveMovement / 0.99f;

        if (fallDown) {
            //zoom for sections falling FROM
            currentTopZoom = currentTopZoom <= 0.03f ? 0.03f : currentTopZoom - 0.055f;

            //needed for player sprite going out of view at end of dive
            if (!diveIn) {
                currentPlayerZoom = currentPlayerZoom <= 0.80f ? 0.80f : currentPlayerZoom - 0.0075f;
                if (currentPlayerZoom == 0.8f) {
                    diveIn = true;
                }
            } else {
                currentPlayerZoom = currentPlayerZoom >= 1.0f ? 1.0f : currentPlayerZoom + 0.016f;
            }

        } else {

            //fallUp
            currentTopZoom = currentTopZoom >= 0.03f ? 0.03f : currentTopZoom + 0.055f;

            //needed for player sprite going out of view at end of dive
            if (!diveIn) {
                currentPlayerZoom = currentPlayerZoom >= 0.80f ? 0.80f : currentPlayerZoom + 0.0075f;
                if (currentPlayerZoom == 0.8f) {
                    diveIn = true;
                }
            } else {
                currentPlayerZoom = currentPlayerZoom <= 1.0f ? 1.0f : currentPlayerZoom - 0.016f;
            }

        }

    }

}

From source file:com.mygdx.utilities.SoundObject_Sfx.java

public void update(Vector2 v) {
    if (pos == null)
        return;/*from ww  w . ja v a 2s  .  c  o m*/

    float dv = v.dst(pos);
    vol = 1 - dv / prox < 0 ? 0 : SoundManager.SFX_VOL * (1 - dv / prox);
    this.setVolume(vol);

    System.out.println("@SoundObject_Sfx id " + id + " vol: " + vol + " pos: " + pos.x + "," + pos.y);
}