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

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

Introduction

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

Prototype

public Vector2(float x, float y) 

Source Link

Document

Constructs a vector with the given components

Usage

From source file:com.hatstick.fireman.physics.Box2DPhysicsWorld.java

License:Apache License

private void createDestruction() {
    UserData userData = new UserData(numEnterPoints, ModelType.WALL, null);
    PolygonShape brickPoly = new PolygonShape();
    brickPoly.setAsBox(0.5f, 0.5f);//from   w  w  w .  jav a 2 s  .co  m

    BodyDef brickBodyDef = new BodyDef();
    brickBodyDef.type = BodyType.DynamicBody;
    brickBodyDef.position.x = 2;
    brickBodyDef.position.y = 2;
    Body brickBody = world.createBody(brickBodyDef);

    FixtureDef brickFixtureDef = new FixtureDef();
    brickFixtureDef.shape = brickPoly;
    // For now, it seems the best way to tell our renderer what our 
    // object is through userData...
    //brickBody.setUserData(userData);
    Fixture fixture = brickBody.createFixture(brickFixtureDef);
    fixture.setUserData(userData);
    brickPoly.dispose();
    numEnterPoints++;

    xEngine.boom(new Vector2(2.1f, 2.1f), brickBody, this);
}

From source file:com.hatstick.fireman.physics.Box2DPhysicsWorld.java

License:Apache License

public void jumpBody(Body body, float height) {
    body.applyLinearImpulse(new Vector2(0, height), body.getWorldCenter(), true);
}

From source file:com.headdetect.games.superpong.Activities.SinglePlayerPongActivity.java

License:Educational Community License

@Override
public void onCreateScene(OnCreateSceneCallback p) throws Exception {
    mEngine.registerUpdateHandler(new FPSLogger());
    if (MultiTouch.isSupported(this)) {
        mEngine.setTouchController(new MultiTouchController());
        if (!MultiTouch.isSupportedDistinct(this)) {
            Toast.makeText(this,
                    "Warning!\nYour device might have problems to distinguish between separate fingers.",
                    Toast.LENGTH_LONG).show();
        }/* w  ww  .  j  a v  a  2s .  c o  m*/
    } else {
        Toast.makeText(this, "Sorry your device does not support MultiTouch!", Toast.LENGTH_LONG).show();
        finish();
    }

    mScene = new Scene();
    mScene.setBackground(new SpriteBackground(getBackground()));

    mPhysics = new FixedStepPhysicsWorld(50, 2, new Vector2(0, 0), false, 8, 8);
    mPhysics.setContactListener(contactListener);

    topWall = Wall.create(false);
    bottomWall = Wall.create(true);
    mBall = Ball
            .create(Vector2Pool.obtain(HALF_CAMERA_WIDTH - HALF_BALL_RAD, HALF_CAMERA_HEIGHT - HALF_BALL_RAD));
    mPlayer = Player.create("Player", true);
    mComputer = Player.create("Computer", false);
    playerText = Letters.getText(new Vector2(HALF_CAMERA_WIDTH - TEXT_DISTANCE_FROM_CENTER, GUI_PADDING),
            String.valueOf(playerScore), Letters.HORIZONTAL_ALIGN_CENTER);
    computerText = Letters.getText(new Vector2(HALF_CAMERA_WIDTH + TEXT_DISTANCE_FROM_CENTER, GUI_PADDING),
            String.valueOf(computerScore), Letters.HORIZONTAL_ALIGN_CENTER);

    mScene.registerUpdateHandler(mPhysics);
    mScene.registerUpdateHandler(UpdateHandle);
    mScene.setOnSceneTouchListener(sceneOnTouchListener);
    mScene.attachChild(mBall);
    mScene.attachChild(topWall);
    mScene.attachChild(bottomWall);
    mScene.attachChild(mPlayer);
    mScene.attachChild(mComputer);
    mScene.attachChild(playerText);
    mScene.attachChild(computerText);

    // -----------------------------------------
    // - Pause Scene
    // -----------------------------------------

    mPauseScene = new Scene();
    org.andengine.entity.primitive.Rectangle dimShape = new org.andengine.entity.primitive.Rectangle(0, 0,
            CAMERA_WIDTH, CAMERA_HEIGHT, this.getVertexBufferObjectManager());
    dimShape.setColor(0f, 0f, 0f, .8f);
    Text pText = Letters.getText(
            new Vector2(HALF_CAMERA_WIDTH - Letters.measureString(pauseMessage) / 2, HALF_CAMERA_HEIGHT),
            pauseMessage, Letters.HORIZONTAL_ALIGN_CENTER);

    mPauseScene.attachChild(pText);
    mPauseScene.attachChild(dimShape);

    p.onCreateSceneFinished(mScene);
}

From source file:com.headdetect.games.superpong.Activities.SinglePlayerPongActivity.java

License:Educational Community License

private void win(Player p) {
    mBall.resetPos();//from  w  w  w. ja v a 2 s  . co m
    // mScene.clearUpdateHandlers();

    mWinScene = new Scene();
    mWinScene.setBackgroundEnabled(false);

    Text firstLine = Letters
            .getText(new Vector2(HALF_CAMERA_WIDTH - Letters.measureString(winMessageTopLine) / 2,
                    HALF_CAMERA_HEIGHT - 100), winMessageTopLine, Letters.HORIZONTAL_ALIGN_CENTER, .6f);
    Text secondLine = Letters.getText(
            new Vector2(HALF_CAMERA_WIDTH - Letters.measureString(p.getName()) / 2, HALF_CAMERA_HEIGHT),
            p.getName(), Letters.HORIZONTAL_ALIGN_CENTER, .6f);

    mWinScene.attachChild(firstLine);
    mWinScene.attachChild(secondLine);

    if (this.mEngine.isRunning()) {
        this.mScene.setChildScene(mWinScene);
        this.mEngine.stop();
    }

}

From source file:com.hindelid.ld.thirtyfour.Main.java

License:Apache License

@Override
public void create() {
    mShapeRenderer = new ShapeRenderer();
    mHUDDisplay = new HUDDisplay();
    mCamera = new OrthographicCamera();
    mViewPort = new ExtendViewport(Constants.VIEW_SIZE_X, Constants.VIEW_SIZE_Y, mCamera);
    mCurrentViewCord = new Vector2(Constants.VIEW_SIZE_X / 2, Constants.VIEW_SIZE_Y / 2);
    moveAndUpdateCamera();//from  w w w .j a  v  a  2 s . c  o m
    mStartSound = Gdx.audio.newSound(Gdx.files.internal("start.wav"));
    mDeadSound = Gdx.audio.newSound(Gdx.files.internal("dead.wav"));
    mOctoSound = Gdx.audio.newSound(Gdx.files.internal("octo.wav"));
    mFishSound = Gdx.audio.newSound(Gdx.files.internal("fish.wav"));
    resetGame();
}

From source file:com.hindelid.ld.thirtyfour.Main.java

License:Apache License

private void resetGame() {
    for (int i = 0; i < 10; i++) {
        mAvgX[i] = 0;/*from ww  w.j a  v  a 2 s  .co m*/
    }
    mRoot = new TreeBranch(new Vector2(Constants.VIEW_SIZE_X / 2f, 0),
            new Vector2(Constants.VIEW_SIZE_X / 2f, 0.5f), true, 1);
    mNextRoot = null;
    mCamera.zoom = 1.0f;
    TreeBranch.sGlobal.setZero();
    mSpeed = Constants.SPEED;
    mHUDDisplay.reset();
    mDead = false;
    mTotalPoints = 0;

    mStartSound.play();
}

From source file:com.hindelid.ld.thirtyfour.TreeBranch.java

License:Apache License

public TreeBranch(Vector2 aStart, Vector2 aEnd, boolean aActive, int aOrder) {
    mStart = aStart;//from www .ja v  a2 s . com
    mEnd = aEnd;
    mLeefCord1 = new Vector2(aStart.x + (aEnd.x - aStart.x) / 2f, aStart.y + (aEnd.y - aStart.y) / 2f);
    mLeefCord2 = new Vector2(aStart.x + (aEnd.x - aStart.x) * 1.1f / 2f,
            aStart.y + (aEnd.y - aStart.y) * 1.1f / 2f);
    mLeefCord3 = new Vector2(aStart.x + (aEnd.x - aStart.x) * 1.1f / 2f,
            aStart.y + (aEnd.y - aStart.y) * 0.9f / 2f);
    mActive = aActive;
    mTheOneActive = aActive;
    mOrder = aOrder;
}

From source file:com.hindelid.ld.thirtyfour.TreeBranch.java

License:Apache License

public void split() {
    if (!mSplitted) {
        mSplitted = true;//from ww w.j  a va  2 s.co m
        double leftAngle = (Constants.sRandom.nextDouble() + 0.5d) * Math.PI / 4d;
        double rightAngle = (Constants.sRandom.nextDouble() + 0.5d) * Math.PI / 4d;
        double middleAngle = (Constants.sRandom.nextDouble() - 0.5d) * Math.PI / 4d;
        boolean leftActive = false;
        boolean rightActive = false;
        boolean growLeft = true;
        boolean growRight = true;
        mOrder++;
        if (mTheOneActive) {
            mOrder = 1;
            leftActive = Gdx.input.isKeyPressed(Input.Keys.LEFT) || Gdx.input.isKeyPressed(Input.Keys.A);

            if (!leftActive) {
                rightActive = Gdx.input.isKeyPressed(Input.Keys.RIGHT) || Gdx.input.isKeyPressed(Input.Keys.D);
            }
        } else {
            growLeft = Constants.sRandom.nextInt(mOrder) < 2;
            growRight = Constants.sRandom.nextInt(mOrder) < 2;
        }
        if (mTheOneActive && !leftActive && !rightActive) {
            mMiddleBranch = new TreeBranch(mEnd,
                    new Vector2(mEnd.x - (float) Math.sin(middleAngle) * Constants.SHRINKAGE_FACTOR / mOrder,
                            mEnd.y + (float) Math.cos(middleAngle) * Constants.SHRINKAGE_FACTOR / mOrder),
                    mTheOneActive, mOrder);
        }
        if (growLeft) {
            mLeftBranch = new TreeBranch(mEnd,
                    new Vector2(mEnd.x - (float) Math.sin(leftAngle) * Constants.SHRINKAGE_FACTOR / mOrder,
                            mEnd.y + (float) Math.cos(leftAngle) * Constants.SHRINKAGE_FACTOR / mOrder),
                    mActive && leftActive, mOrder);
        }
        if (growRight) {
            mRightBranch = new TreeBranch(mEnd,
                    new Vector2(mEnd.x + (float) Math.sin(rightAngle) * Constants.SHRINKAGE_FACTOR / mOrder,
                            mEnd.y + (float) Math.cos(rightAngle) * Constants.SHRINKAGE_FACTOR / mOrder),
                    mActive && rightActive, mOrder);
        }
        mTheOneActive = false;
    } else {
        if (null != mLeftBranch) {
            mLeftBranch.split();
        }
        if (null != mRightBranch) {
            mRightBranch.split();
        }
        if (null != mMiddleBranch) {
            mMiddleBranch.split();
        }
    }
}

From source file:com.home.voicelab.VoiceLabitynth.java

License:Apache License

@Override
public Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    // Create physics world
    this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), true, 8, 1);

    mScene = new Scene();
    mScene.registerUpdateHandler(this.mPhysicsWorld);

    // Load map from TMX file.
    try {//from w  w  w . ja  va 2  s  . c o  m
        final TMXLoader tmxLoader = new TMXLoader(this.getAssets(), this.mEngine.getTextureManager(),
                TextureOptions.BILINEAR_PREMULTIPLYALPHA /* NEAREST */, this.getVertexBufferObjectManager(),
                new ITMXTilePropertiesListener() {
                    @Override
                    public void onTMXTileWithPropertiesCreated(final TMXTiledMap pTMXTiledMap,
                            final TMXLayer pTMXLayer, final TMXTile pTMXTile,
                            final TMXProperties<TMXTileProperty> pTMXTileProperties) {

                        if (pTMXTileProperties.containsTMXProperty("startPoint", "true")) {
                            startPointX = pTMXTile.getTileX();
                            startPointY = pTMXTile.getTileY();
                        }
                    }
                });
        this.mTMXTiledMap = tmxLoader.loadFromAsset("tmx/labirynth.tmx");

    } catch (final TMXLoadException e) {
        Log.d("TMXLoader", "Problem with TMX loader");
    }

    // Add the non-object layers to the scene
    for (int i = 0; i < this.mTMXTiledMap.getTMXLayers().size(); i++) {
        tmxLayer = this.mTMXTiledMap.getTMXLayers().get(i);
        if (!tmxLayer.getTMXLayerProperties().containsTMXProperty("wall", "true"))
            mScene.attachChild(tmxLayer);
    }

    // Make the camera not exceed the bounds of the TMXEntity.
    this.mBoundChaseCamera.setBounds(0, 0, tmxLayer.getWidth(), tmxLayer.getHeight());
    //this.mBoundChaseCamera.setBounds(0, 0, tmxLayer.getWidth(),
    //      tmxLayer.getHeight());
    this.mBoundChaseCamera.setBoundsEnabled(true);
    //this.mBoundChaseCamera.setBoundsEnabled(true);

    // Read in the unwalkable blocks from the object layer and create boxes for each
    this.createUnwalkableObjects(mTMXTiledMap);

    // Add outer walls
    this.addBounds(tmxLayer.getWidth(), tmxLayer.getHeight());

    // Create the player sprite and add it to the scene.
    player = new AnimatedSprite(startPointX, startPointY, this.mPlayerTextureRegion,
            this.getVertexBufferObjectManager());
    player.setScale(0.9f);

    //this.mBoundChaseCamera.setChaseEntity(player);
    final FixtureDef playerFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0);
    mPlayerBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, player, BodyType.DynamicBody,
            playerFixtureDef);

    mScene.registerUpdateHandler(mPhysicsWorld);

    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(player, mPlayerBody, true, false) {
        @Override
        public void onUpdate(float pSecondsElapsed) {
            super.onUpdate(pSecondsElapsed);
            mBoundChaseCamera.updateChaseEntity();
            //mBoundChaseCamera.updateChaseEntity();
        }
    });

    // Create a rectangle that will always highlight the tile below the feet of the pEntity.
    final Rectangle currentTileRectangle = new Rectangle(0, 0, this.mTMXTiledMap.getTileWidth(),
            this.mTMXTiledMap.getTileHeight(), this.getVertexBufferObjectManager());
    currentTileRectangle.setColor(1, 0, 0, 0.15f);
    mScene.attachChild(currentTileRectangle);

    // Add the digital control
    initDigitalControl();

    mScene.registerUpdateHandler(new IUpdateHandler() {

        @Override
        public void reset() {
        }

        @Override
        public void onUpdate(final float pSecondsElapsed) {

            /* Get the scene-coordinates of the players feet. */
            //            final float[] playerFootCordinates = player
            //                  .convertLocalToSceneCoordinates(0,0);      
            //            /* Get the tile the feet of the player are currently waking on. */
            //            final TMXTile tmxTile = tmxLayer.getTMXTileAt(
            //                  playerFootCordinates[Constants.VERTEX_INDEX_X],
            //                  playerFootCordinates[Constants.VERTEX_INDEX_Y]);
            final TMXTile tmxTile = tmxLayer.getTMXTileAt(player.getX() + player.getWidth() / 2,
                    player.getY() + player.getHeight() / 2);

            if (tmxTile != null) {
                currentTileRectangle.setPosition(tmxTile.getTileX(), tmxTile.getTileY());
                if (tmxTile.getTileRow() != lastCheckTileRow
                        || tmxTile.getTileColumn() != lastCheckTileColumn) {
                    if (tmxTile.getTMXTileProperties(mTMXTiledMap) != null) {
                        if (tmxTile.getTMXTileProperties(mTMXTiledMap).containsTMXProperty("checkPoint",
                                "true")) {
                            mPlayerBody.setLinearVelocity(0, 0);
                            final float widthD2 = player.getWidth() / 2;
                            final float heightD2 = player.getHeight() / 2;
                            final float angle = mPlayerBody.getAngle(); // keeps the body angle
                            final Vector2 v2 = Vector2Pool.obtain(
                                    (tmxTile.getTileX() + widthD2) / PIXEL_TO_METER_RATIO_DEFAULT,
                                    (tmxTile.getTileY() + heightD2) / PIXEL_TO_METER_RATIO_DEFAULT);
                            mPlayerBody.setTransform(v2, angle);
                            Vector2Pool.recycle(v2);

                            lastCheckTileRow = tmxTile.getTileRow();
                            lastCheckTileColumn = tmxTile.getTileColumn();
                            if (player.isAnimationRunning()) {
                                player.stopAnimation();
                                playerDirection = PlayerDirection.NONE;
                            }
                        }
                        if (tmxTile.getTMXTileProperties(mTMXTiledMap).containsTMXProperty("endPoint",
                                "true")) {
                            mBoundChaseCamera.setChaseEntity(finishRectangle);
                            finishRectangle.setSize(CAMERA_WIDTH, CAMERA_HEIGHT);
                            finishRectangle.setVisible(true);
                            finishText.setPosition(CAMERA_WIDTH / 2 - alertTextSize / 2, CAMERA_HEIGHT / 4);
                            finishText.setVisible(true);
                            gameEnd = true;
                        }

                    }
                } else {
                    lastCheckTileRow = tmxTile.getTileRow();
                    lastCheckTileColumn = tmxTile.getTileColumn();
                }

            }
        }
    });

    player.registerUpdateHandler(new PhysicsHandler(player));
    mScene.attachChild(player);

    // create alert rectangle that will show during recognizer work.
    alertRectangle = new Rectangle(0, 0, this.CAMERA_WIDTH, this.CAMERA_HEIGHT,
            this.getVertexBufferObjectManager());
    alertRectangle.setColor(1, 0, 0, 0.5f);
    alertRectangle.setVisible(false);
    mScene.attachChild(alertRectangle);
    font = FontFactory.create(getFontManager(), getTextureManager(), 256, 256,
            Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, Color.argb(128, 255, 255, 255));
    font.load();
    alertText = new Text(0, 0, font, "Recognition", getVertexBufferObjectManager());
    //alertText.setHorizontalAlign(HorizontalAlign.CENTER);
    alertText.setVisible(false);
    mScene.attachChild(alertText);

    helpRectangle = new Rectangle(0, 0, this.CAMERA_WIDTH, this.CAMERA_HEIGHT,
            this.getVertexBufferObjectManager());
    helpRectangle.setColor(0, 0, 0, 0.8f);
    helpRectangle.setVisible(true);
    mScene.attachChild(helpRectangle);
    fontH = FontFactory.create(getFontManager(), getTextureManager(), 256, 256,
            Typeface.create(Typeface.DEFAULT, Typeface.NORMAL), 13, Color.argb(255, 255, 255, 255));
    fontH.load();
    helpText = new Text(0, 0, fontH,
            "Komendy" + '\n' + '\n' + "lewo, prao, gra, d - Sterowanie postaci" + '\n'
                    + "pomoc - Wywietla tekst z pomoc" + '\n' + "mapa - Podgld mapy" + '\n'
                    + "cofnij - Zamuka pomoc lub podgld mapy" + '\n' + "zakocz - Wychodzi z gry" + '\n'
                    + "rozpocznij - Rozpoczyna now gr po przejciu mapy" + '\n' + '\n'
                    + "System rozpoznaje rwnie powysze komendy" + '\n' + "w jzyku angielskim.",
            getVertexBufferObjectManager());
    //alertText.setHorizontalAlign(HorizontalAlign.CENTER);
    helpText.setVisible(true);
    mScene.attachChild(helpText);

    this.mBoundChaseCamera.setChaseEntity(helpRectangle);
    if (!helpVisible) {
        helpRectangle.setVisible(false);
        helpText.setVisible(false);
        this.mBoundChaseCamera.setChaseEntity(player);
    }

    final float densityMultiplier = getBaseContext().getResources().getDisplayMetrics().density;
    final float scaledPx = 32 * densityMultiplier;
    Paint paint = new Paint();
    paint.setTextSize(scaledPx);
    alertTextSize = paint.measureText(" Recognition ");

    finishRectangle = new Rectangle(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, getVertexBufferObjectManager());
    finishRectangle.setColor(0, 0, 1, 0.5f);
    finishRectangle.setVisible(false);
    mScene.attachChild(finishRectangle);
    finishText = new Text(0, 0, font, "Success!", getVertexBufferObjectManager());
    finishText.setPosition(CAMERA_WIDTH / 2 - alertTextSize / 2, CAMERA_HEIGHT / 4);
    finishText.setVisible(false);
    mScene.attachChild(finishText);

    return mScene;
}

From source file:com.home.voicelab.VoiceLabitynth.java

License:Apache License

protected void evaluateDirection(float pValueX, float pValueY) {
    if (pValueY == 1) {
        // Up//from  w  w w.j a v  a  2  s.  c o m
        if (playerDirection != PlayerDirection.UP) {
            player.animate(ANIMATE_DURATION, 0, 2, true);
            playerDirection = PlayerDirection.UP;
            mPlayerBody.setLinearVelocity(0, 0);
            mPlayerBody.applyForce(new Vector2(pValueX * PLAYER_VELOCITY, pValueY * PLAYER_VELOCITY),
                    mPlayerBody.getWorldCenter());
        }
    } else if (pValueY == -1) {
        // Down
        if (playerDirection != PlayerDirection.DOWN) {
            player.animate(ANIMATE_DURATION, 9, 11, true);
            playerDirection = PlayerDirection.DOWN;
            mPlayerBody.setLinearVelocity(0, 0);
            mPlayerBody.applyForce(new Vector2(pValueX * PLAYER_VELOCITY, pValueY * PLAYER_VELOCITY),
                    mPlayerBody.getWorldCenter());
        }
    } else if (pValueX == -1) {
        // Left
        if (playerDirection != PlayerDirection.LEFT) {
            player.animate(ANIMATE_DURATION, 3, 5, true);
            playerDirection = PlayerDirection.LEFT;
            mPlayerBody.setLinearVelocity(0, 0);
            mPlayerBody.applyForce(new Vector2(pValueX * PLAYER_VELOCITY, pValueY * PLAYER_VELOCITY),
                    mPlayerBody.getWorldCenter());
        }
    } else if (pValueX == 1) {
        // Right
        if (playerDirection != PlayerDirection.RIGHT) {
            player.animate(ANIMATE_DURATION, 6, 8, true);
            playerDirection = PlayerDirection.RIGHT;
            mPlayerBody.setLinearVelocity(0, 0);
            mPlayerBody.applyForce(new Vector2(pValueX * PLAYER_VELOCITY, pValueY * PLAYER_VELOCITY),
                    mPlayerBody.getWorldCenter());

        }
    }

}