List of usage examples for com.badlogic.gdx.math Vector2 Vector2
public Vector2(float x, float y)
From source file:com.evoluzion.Organismo.java
License:Open Source License
public void escannearObjetos() { if (sentir == true) { for (int i = 0; i < aObjetos.size; i++) { aObjetos.removeIndex(i);//from ww w . java 2 s . c o m } //limpia la lista par un nuevo escaneo for (int i = 0; i < aPredadores.size; i++) { aPredadores.removeIndex(i); } //limpia la lista par un nuevo escaneo if (carnivoro == false) { for (Qenergia qe : m.aqe) { if (qe.visible == true) { float qX = qe.posicion.x; float qY = qe.posicion.y; float Dx = posicion.x - qX; float Dy = posicion.y - qY; float h = (float) Math.sqrt((Dy * Dy) + (Dx * Dx)); if (h < radioConsiente) { aObjetos.add(new Vector2(qX, qY)); } } } } if (carnivoro == true) { for (int i = 0; i < m.aorg.size; i++) { Organismo or = m.aorg.get(i); if (this != or && !identificador.equals(or.identificador) && capacidad > or.capacidad) { //if (!or.getIdentificador().equals(or2.getIdentificador())){ float qX = or.posicion.x; float qY = or.posicion.y; float Dx = posicion.x - qX; float Dy = posicion.y - qY; float h = (float) Math.sqrt((Dy * Dy) + (Dx * Dx)); if (h < radioConsiente) { aObjetos.add(new Vector2(qX, qY)); } // System.out.println("presa detectada"); } } } } //detectar peligro for (int i = 0; i < m.aorg.size; i++) { Organismo or = m.aorg.get(i); if (this != or && or.carnivoro == true && capacidad < or.capacidad) { float qX = or.posicion.x; float qY = or.posicion.y; float Dx = posicion.x - qX; float Dy = posicion.y - qY; float h = (float) Math.sqrt((Dy * Dy) + (Dx * Dx)); // System.out.println(adn.getSize()); if (h < radioConsiente) { aPredadores.add(new Vector2(qX, qY)); } // System.out.println("predadores detectados: "); } } }
From source file:com.evoluzion.Organismo.java
License:Open Source License
public void morir() { direccion.x = 0;/*from w w w. j av a 2 s . c o m*/ direccion.y = 0; for (int i = 0; i < m.aqe.size; i++) { Qenergia qe = m.aqe.get(i); float posx = 0; float posy = 0; if (qe.visible == false) { if (biomasa > m.Qbiomasa) { posx = (float) (Math.random() * (posicion.x - (posicion.x - 50)) + (posicion.x - 50)); posy = (float) (Math.random() * (posicion.y - (posicion.y - 50)) + (posicion.y - 50)); qe.visible = true; // qe.setMasa(m.Qbiomasa); qe.posicion = new Vector2(posx, posy); qe.update(); biomasa = biomasa - qe.masa; } if (biomasa <= m.Qbiomasa) { posx = (float) (Math.random() * (posicion.x - (posicion.x - 50)) + (posicion.x - 50)); posy = (float) (Math.random() * (posicion.y - (posicion.y - 50)) + (posicion.y - 50)); qe.visible = true; // qe.setMasa(biomasa); qe.posicion = new Vector2(posx, posy); qe.update(); biomasa = 0; } if (biomasa <= 0) { i = m.aqe.size; } } } m.aorg.removeValue(this, true); // System.out.println("mori"+ m.aorg.size); }
From source file:com.evoluzion.Organismo.java
License:Open Source License
public void dividirse() { if (segundos >= tiempoMitosis / 1000 && m.aorg.size < m.maximo) { if ((100 * biomasa) / capacidad > 50) { int biom = biomasa; int bio = biom / 2; int bio2 = biom - bio; float ener = energia / 2; float ener2 = energia - ener; Genoma gen2 = new Genoma();//nuevo gen para las celulas hijas Genoma gen = new Genoma();//nuevo gen para las celulas hijas //el nuevo gen sale de copiar el gen de la celula madre // System.out.println(adn.getSize()); if (m.mutarColor == true) { gen2.color = new StringBuffer(replicar((this.adn.color))); }/* w w w .j av a 2s. c o m*/ if (m.mutarSize == true) { gen2.ancho = new StringBuffer(replicar((this.adn.ancho))); } if (m.mutarSize == true) { gen2.alto = new StringBuffer(replicar((this.adn.alto))); } if (m.mutarSpeed == true) { gen2.speed = new StringBuffer(replicar((this.adn.speed))); } if (m.mutarTemp == true) { gen2.toleranciaTemp = new StringBuffer(replicar((this.adn.toleranciaTemp))); } if (m.mutarPredador == true) { gen2.predador = new StringBuffer(replicar((this.adn.predador))); } if (m.mutarSentir == true) { gen2.sentir = new StringBuffer(replicar((this.adn.sentir))); } if (m.mutarCazar == true) { gen2.cazar = new StringBuffer(replicar((this.adn.cazar))); } if (m.mutarEscapar == true) { gen2.escapar = new StringBuffer(replicar((this.adn.escapar))); } if (m.mutarRadioconsiente == true) { gen2.radioConsiente = new StringBuffer(replicar(this.adn.radioConsiente)); } if (m.mutarTasaMut == true) { gen2.tasaMutacion = new StringBuffer(replicar(this.adn.tasaMutacion)); } if (m.mutarLongevidad == true) { gen2.longevidad = new StringBuffer(replicar(this.adn.longevidad)); } if (m.mutarResistencia == true) { gen2.resistenciaATB = new StringBuffer(replicar(this.adn.resistenciaATB)); } if (m.mutarColor == false) { gen2.color = new StringBuffer(adn.color.toString()); } if (m.mutarSize == false) { gen2.ancho = new StringBuffer(adn.ancho.toString()); } if (m.mutarSize == false) { gen2.alto = new StringBuffer(adn.alto.toString()); } if (m.mutarTemp == false) { gen2.toleranciaTemp = new StringBuffer(adn.toleranciaTemp.toString()); } if (m.mutarSpeed == false) { gen2.speed = new StringBuffer(adn.speed.toString()); } if (m.mutarPredador == false) { gen2.predador = new StringBuffer(adn.predador.toString()); } if (m.mutarSentir == false) { gen2.sentir = new StringBuffer(adn.sentir.toString()); } if (m.mutarCazar == false) { gen2.cazar = new StringBuffer(adn.cazar.toString()); } if (m.mutarEscapar == false) { gen2.escapar = new StringBuffer(adn.escapar.toString()); } if (m.mutarRadioconsiente == false) { gen2.radioConsiente = new StringBuffer(adn.radioConsiente.toString()); } if (m.mutarTasaMut == false) { gen2.tasaMutacion = new StringBuffer(adn.tasaMutacion.toString()); } if (m.mutarLongevidad == false) { gen2.longevidad = new StringBuffer(adn.longevidad.toString()); } if (m.mutarResistencia == false) { gen2.resistenciaATB = new StringBuffer(adn.resistenciaATB.toString()); } if (m.mutarColor == true) { gen.color = new StringBuffer(replicar((this.adn.color))); } if (m.mutarSize == true) { gen.ancho = new StringBuffer(replicar((this.adn.ancho))); } if (m.mutarSize == true) { gen.alto = new StringBuffer(replicar((this.adn.alto))); } if (m.mutarSpeed == true) { gen.speed = new StringBuffer(replicar((this.adn.speed))); } if (m.mutarTemp == true) { gen.toleranciaTemp = new StringBuffer(replicar((this.adn.toleranciaTemp))); } if (m.mutarPredador == true) { gen.predador = new StringBuffer(replicar((this.adn.predador))); } if (m.mutarSentir == true) { gen.sentir = new StringBuffer(replicar((this.adn.sentir))); } if (m.mutarCazar == true) { gen.cazar = new StringBuffer(replicar((this.adn.cazar))); } if (m.mutarEscapar == true) { gen.escapar = new StringBuffer(replicar((this.adn.escapar))); } if (m.mutarRadioconsiente == true) { gen.radioConsiente = new StringBuffer(replicar(this.adn.radioConsiente)); } if (m.mutarTasaMut == true) { gen.tasaMutacion = new StringBuffer(replicar(this.adn.tasaMutacion)); } if (m.mutarLongevidad == true) { gen.longevidad = new StringBuffer(replicar(this.adn.longevidad)); } if (m.mutarResistencia == true) { gen.resistenciaATB = new StringBuffer(replicar(this.adn.resistenciaATB)); } if (m.mutarColor == false) { gen.color = new StringBuffer(adn.color.toString()); } if (m.mutarSize == false) { gen.ancho = new StringBuffer(adn.ancho.toString()); } if (m.mutarSize == false) { gen.alto = new StringBuffer(adn.alto.toString()); } if (m.mutarSpeed == false) { gen.speed = new StringBuffer(adn.speed.toString()); } if (m.mutarTemp == false) { gen2.toleranciaTemp = new StringBuffer(adn.toleranciaTemp.toString()); } if (m.mutarPredador == false) { gen.predador = new StringBuffer(adn.predador.toString()); } if (m.mutarSentir == false) { gen.sentir = new StringBuffer(adn.sentir.toString()); } if (m.mutarCazar == false) { gen.cazar = new StringBuffer(adn.cazar.toString()); } if (m.mutarEscapar == false) { gen.escapar = new StringBuffer(adn.escapar.toString()); } if (m.mutarRadioconsiente == false) { gen.radioConsiente = new StringBuffer(adn.radioConsiente.toString()); } if (m.mutarTasaMut == false) { gen.tasaMutacion = new StringBuffer(adn.tasaMutacion.toString()); } if (m.mutarLongevidad == false) { gen.longevidad = new StringBuffer(adn.longevidad.toString()); } if (m.mutarResistencia == false) { gen.resistenciaATB = new StringBuffer(adn.resistenciaATB.toString()); } // System.out.println(gen2.getSize()); Vector2 pos = new Vector2((float) posicion.x + ((ancho) + 3) / 2, (float) posicion.y); Vector2 dir = new Vector2((float) Math.random() * 20, (float) Math.random() * 20); if (dir.x < 10) { dir.x = dir.x * (-1); } if (dir.x > 10) { dir.x = dir.x - 10; } if (dir.y < 10) { dir.y = dir.y * (-1); } if (dir.y > 10) { dir.y = dir.y - 10; } StringBuffer sb = new StringBuffer(nombre.toString()); StringBuffer sb2 = new StringBuffer(nombre.toString()); Organismo or = new Organismo(gen, pos, sb, m); or.direccion = dir; or.energia = ener; or.biomasa = bio; or.marcado = 1 * marcado; or.transferred = transferred; m.aorg.add(or); Vector2 pos2 = new Vector2((float) posicion.x - ((ancho) + 3) / 2, (float) posicion.y); Vector2 dir2 = new Vector2((float) Math.random() * 20, (float) Math.random() * 20); if (dir2.x < 10) { dir2.x = dir2.x * (-1); } if (dir2.x > 10) { dir2.x = dir2.x - 10; } if (dir2.y < 10) { dir2.y = dir2.y * (-1); } if (dir2.y > 10) { dir2.y = dir2.y - 10; } Organismo or2 = new Organismo(gen2, pos2, sb2, m); or2.direccion = dir2; or2.energia = ener2; or2.biomasa = bio2; or2.marcado = 1 * marcado; or2.transferred = transferred; m.aorg.add(or2); if (!this.identificador.equals(or.identificador)) { // System.out.println("nombre antes: "+ or.nombre); // or.nombre.append(letras[m.index]); m.index++; //System.out.println("nombre despues: " + or.nombre+ "index"+ index); if (m.index > 25) { m.index = 0; } // } if (!this.identificador.equals(or2.identificador)) { or2.nombre.append(letras[m.index]); m.index++; if (m.index > 25) { m.index = 0; } // } m.aorg.removeValue(this, true); } } }
From source file:com.example.jumpertutorial.JumperTutorial.java
License:Apache License
@Override public void create() { /**//from w w w. ja va2 s .c o m * If the viewport's size is not yet known, determine it here. */ if (screenWidth == -1) { screenWidth = Gdx.graphics.getWidth(); screenHeight = Gdx.graphics.getHeight(); } tiledMapHelper = new TiledMapHelper(); tiledMapHelper = new TiledMapHelper(); tiledMapHelper.setPackerDirectory("data/packer"); tiledMapHelper.loadMap("data/world/level1/level.tmx"); tiledMapHelper.prepareCamera(screenWidth, screenHeight); /** * Load up the overall texture and chop it in to pieces. In this case, * piece. */ overallTexture = new Texture(Gdx.files.internal("data/sprite.png")); overallTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); jumperSprite = new Sprite(overallTexture, 0, 0, 21, 37); spriteBatch = new SpriteBatch(); /** * You can set the world's gravity in its constructor. Here, the gravity * is negative in the y direction (as in, pulling things down). */ world = new World(new Vector2(0.0f, -10.0f), true); BodyDef jumperBodyDef = new BodyDef(); jumperBodyDef.type = BodyDef.BodyType.DynamicBody; jumperBodyDef.position.set(1.0f, 3.0f); jumper = world.createBody(jumperBodyDef); /** * Boxes are defined by their "half width" and "half height", hence the * 2 multiplier. */ PolygonShape jumperShape = new PolygonShape(); jumperShape.setAsBox(jumperSprite.getWidth() / (2 * PIXELS_PER_METER), jumperSprite.getHeight() / (2 * PIXELS_PER_METER)); /** * The character should not ever spin around on impact. */ jumper.setFixedRotation(true); /** * The density and friction of the jumper were found experimentally. * Play with the numbers and watch how the character moves faster or * slower. */ FixtureDef jumperFixtureDef = new FixtureDef(); jumperFixtureDef.shape = jumperShape; jumperFixtureDef.density = 1.0f; jumperFixtureDef.friction = 5.0f; jumper.createFixture(jumperFixtureDef); jumperShape.dispose(); tiledMapHelper.loadCollisions("data/collisions.txt", world, PIXELS_PER_METER); debugRenderer = new Box2DDebugRenderer(); lastRender = System.nanoTime(); }
From source file:com.example.jumpertutorial.JumperTutorial.java
License:Apache License
@Override public void render() { long now = System.nanoTime(); /**/*from w w w . j a v a 2s .c o m*/ * Detect requested motion. */ boolean moveLeft = false; boolean moveRight = false; boolean doJump = false; if (Gdx.input.isKeyPressed(Input.Keys.DPAD_RIGHT)) { moveRight = true; } else { for (int i = 0; i < 2; i++) { if (Gdx.input.isTouched(i) && Gdx.input.getX() > Gdx.graphics.getWidth() * 0.80f) { moveRight = true; } } } if (Gdx.input.isKeyPressed(Input.Keys.DPAD_LEFT)) { moveLeft = true; } else { for (int i = 0; i < 2; i++) { if (Gdx.input.isTouched(i) && Gdx.input.getX() < Gdx.graphics.getWidth() * 0.20f) { moveLeft = true; } } } if (Gdx.input.isKeyPressed(Input.Keys.DPAD_UP)) { doJump = true; } else { for (int i = 0; i < 2; i++) { if (Gdx.input.isTouched(i) && Gdx.input.getY() < Gdx.graphics.getHeight() * 0.20f) { doJump = true; } } } /** * Act on that requested motion. * * This code changes the jumper's direction. It's handled separately * from the jumping so that the player can jump and move simultaneously. * The horizontal figure was arrived at experimentally -- try other * values to experience different speeds. * * The impulses are applied to the center of the jumper. */ if (moveRight) { jumper.applyLinearImpulse(new Vector2(0.05f, 0.0f), jumper.getWorldCenter()); if (jumperFacingRight == false) { jumperSprite.flip(true, false); } jumperFacingRight = true; } else if (moveLeft) { jumper.applyLinearImpulse(new Vector2(-0.05f, 0.0f), jumper.getWorldCenter()); if (jumperFacingRight == true) { jumperSprite.flip(true, false); } jumperFacingRight = false; } /** * The jumper dude can only jump while on the ground. There are better * ways to detect ground contact, but for our purposes it is sufficient * to test that the vertical velocity is zero (or close to it). As in * the above code, the vertical figure here was found through * experimentation. It's enough to get the guy off the ground. * * As before, impulse is applied to the center of the jumper. */ if (doJump && Math.abs(jumper.getLinearVelocity().y) < 1e-9) { jumper.applyLinearImpulse(new Vector2(0.0f, 0.8f), jumper.getWorldCenter()); } /** * Have box2d update the positions and velocities (and etc) of all * tracked objects. The second and third argument specify the number of * iterations of velocity and position tests to perform -- higher is * more accurate but is also slower. */ world.step(Gdx.app.getGraphics().getDeltaTime(), 3, 3); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); /** * A nice(?), blue backdrop. */ Gdx.gl.glClearColor(0, 0.5f, 0.9f, 0); /** * The camera is now controlled primarily by the position of the main * character, and secondarily by the map boundaries. */ tiledMapHelper.getCamera().position.x = PIXELS_PER_METER * jumper.getPosition().x; /** * Ensure that the camera is only showing the map, nothing outside. */ if (tiledMapHelper.getCamera().position.x < Gdx.graphics.getWidth() / 2) { tiledMapHelper.getCamera().position.x = Gdx.graphics.getWidth() / 2; } if (tiledMapHelper.getCamera().position.x >= tiledMapHelper.getWidth() - Gdx.graphics.getWidth() / 2) { tiledMapHelper.getCamera().position.x = tiledMapHelper.getWidth() - Gdx.graphics.getWidth() / 2; } if (tiledMapHelper.getCamera().position.y < Gdx.graphics.getHeight() / 2) { tiledMapHelper.getCamera().position.y = Gdx.graphics.getHeight() / 2; } if (tiledMapHelper.getCamera().position.y >= tiledMapHelper.getHeight() - Gdx.graphics.getHeight() / 2) { tiledMapHelper.getCamera().position.y = tiledMapHelper.getHeight() - Gdx.graphics.getHeight() / 2; } tiledMapHelper.getCamera().update(); tiledMapHelper.render(); /** * Prepare the SpriteBatch for drawing. */ spriteBatch.setProjectionMatrix(tiledMapHelper.getCamera().combined); spriteBatch.begin(); jumperSprite.setPosition(PIXELS_PER_METER * jumper.getPosition().x - jumperSprite.getWidth() / 2, PIXELS_PER_METER * jumper.getPosition().y - jumperSprite.getHeight() / 2); jumperSprite.draw(spriteBatch); /** * "Flush" the sprites to screen. */ spriteBatch.end(); /** * Draw this last, so we can see the collision boundaries on top of the * sprites and map. */ debugRenderer.render(world, tiledMapHelper.getCamera().combined.scale(JumperTutorial.PIXELS_PER_METER, JumperTutorial.PIXELS_PER_METER, JumperTutorial.PIXELS_PER_METER)); now = System.nanoTime(); if (now - lastRender < 30000000) { // 30 ms, ~33FPS try { Thread.sleep(30 - (now - lastRender) / 1000000); } catch (InterruptedException e) { } } lastRender = now; }
From source file:com.finalproject.Model.Zombie.java
public Zombie(float x, float y, float width, float height) { super(x, y, width, height); velocity = new Vector2(0, 0); acceleration = new Vector2(0, 0); //initialize the booleans of the directions the zombie is facing isFacingNE = false;//from w w w. j av a 2 s .co m isFacingNW = false; isFacingSE = false; isFacingSW = false; //initialize the state time stateTime = 0; }
From source file:com.flaiker.reaktio.entities.DragSquareEntity.java
License:Open Source License
public DragSquareEntity(float x, float y, Direction direction, boolean selfSolving) { super(new TextureRegion(new Texture(Gdx.files.internal("DragSquareEntity.png"))), x, y, WIDTH, HEIGHT, selfSolving);/*from w w w . j a va 2 s .com*/ startPos = new Vector2(x, y); this.direction = direction; switch (direction) { case LEFT: sprite.rotate90(true); sprite.rotate90(true); break; case RIGHT: break; case UP: sprite.rotate90(false); break; case DOWN: sprite.rotate90(true); break; } }
From source file:com.forerunnergames.peril.client.settings.PlayMapSettings.java
License:Open Source License
public static Vector2 referenceToActualPlayMapScaling(final Vector2 playMapReferenceSize) { Arguments.checkIsNotNull(playMapReferenceSize, "playMapReferenceSize"); Arguments.checkLowerExclusiveBound(playMapReferenceSize.x, 0, "playMapReferenceSize.x"); Arguments.checkLowerExclusiveBound(playMapReferenceSize.y, 0, "playMapReferenceSize.y"); return new Vector2(ACTUAL_WIDTH / playMapReferenceSize.x, ACTUAL_HEIGHT / playMapReferenceSize.y); }
From source file:com.forerunnergames.peril.client.settings.PlayMapSettings.java
License:Open Source License
public static Vector2 actualToReferencePlayMapScaling(final Vector2 playMapReferenceSize) { Arguments.checkIsNotNull(playMapReferenceSize, "playMapReferenceSize"); return new Vector2(playMapReferenceSize.x / ACTUAL_WIDTH, playMapReferenceSize.y / ACTUAL_HEIGHT); }
From source file:com.forerunnergames.peril.client.ui.screens.game.play.modes.classic.playmap.actors.DefaultPlayMapFactory.java
License:Open Source License
@Override public PlayMap create(final PlayMapMetadata playMapMetadata) { Arguments.checkIsNotNull(playMapMetadata, "playMapMetadata"); if (playMapMetadata.equals(PlayMapMetadata.NULL)) return PlayMap.NULL; // @formatter:off final BitmapFont font = new BitmapFont(); final CountryImageDataRepository countryImageDataRepository = countryImageDataRepositoryFactory .create(playMapMetadata);// w w w.java2s . com final Image backgroundImage = playMapBackgroundImageLoader.get(playMapMetadata); final Vector2 playMapReferenceSize = new Vector2(backgroundImage.getWidth(), backgroundImage.getHeight()); final PlayMapInputDetection playMapInputDetection = playMapInputDetectionFactory.create(playMapMetadata, playMapReferenceSize); final HoveredTerritoryText hoveredTerritoryText = new HoveredTerritoryText(playMapInputDetection, mouseInput, font); final ImmutableMap.Builder<String, Country> countryNamesToActorsBuilder = ImmutableMap.builder(); countryImagesFactory.create(playMapMetadata, countryAtlasesLoader.get(playMapMetadata)); // @formatter:on final CountryImagesRepository countryImagesRepository = new DefaultCountryImagesRepository( countryImagesFactory.getPrimary(playMapMetadata), countryImagesFactory.getSecondary(playMapMetadata)); final CountryFactory countryFactory = new CountryFactory(countryImageDataRepository, countryImagesRepository, playMapReferenceSize); for (final String countryName : countryImageDataRepository.getCountryNames()) { countryNamesToActorsBuilder.put(countryName, countryFactory.create(countryName, font)); } final PlayMap playMap = new DefaultPlayMap(countryNamesToActorsBuilder.build(), playMapInputDetection, hoveredTerritoryText, backgroundImage, playMapMetadata); hoveredTerritoryText.setPlayMap(playMap); return playMap; }