List of usage examples for com.badlogic.gdx.math Rectangle contains
public boolean contains(Rectangle rectangle)
From source file:at.therefactory.jewelthief.actors.Enemy.java
License:Open Source License
public void update(Rectangle enemyField) { if (!enemyField.contains(sprite.getBoundingRectangle())) { if (sprite.getBoundingRectangle().x + sprite.getBoundingRectangle().width >= enemyField.x + enemyField.width) {/*from ww w .ja v a 2 s .c om*/ movementInverter.x = -1; } else if (sprite.getBoundingRectangle().x <= enemyField.x) { movementInverter.x = 1; } else if (sprite.getBoundingRectangle().y + sprite.getBoundingRectangle().getHeight() >= enemyField.y + enemyField.height) { movementInverter.y = -1; } else if (sprite.getBoundingRectangle().y <= enemyField.y) { movementInverter.y = 1; } } position.x += speed * movementInverter.x; position.y += speed * movementInverter.y; sprite.setPosition(position.x - sprite.getWidth() / 2, position.y - sprite.getHeight() / 2); }
From source file:com.hindelid.ld.thirtyfour.TreeBranch.java
License:Apache License
public boolean checkCollision(Rectangle aBoundingBox) { return aBoundingBox.contains(mEnd) || (null != mLeftBranch && mLeftBranch.checkCollision(aBoundingBox)) || (null != mRightBranch && mRightBranch.checkCollision(aBoundingBox)) || (null != mMiddleBranch && mMiddleBranch.checkCollision(aBoundingBox)); }
From source file:com.ore.infinium.systems.PowerOverlayRenderSystem.java
License:Open Source License
private Entity entityAtPosition(Vector2 pos) { ImmutableArray<Entity> entities = m_world.engine .getEntitiesFor(Family.all(PowerDeviceComponent.class).get()); SpriteComponent spriteComponent;/*from www.ja va 2 s .com*/ TagComponent tagComponent; for (int i = 0; i < entities.size(); ++i) { tagComponent = tagMapper.get(entities.get(i)); if (tagComponent != null && tagComponent.tag.equals("itemPlacementGhost")) { continue; } spriteComponent = spriteMapper.get(entities.get(i)); Rectangle rectangle = new Rectangle( spriteComponent.sprite.getX() - (spriteComponent.sprite.getWidth() * 0.5f), spriteComponent.sprite.getY() - (spriteComponent.sprite.getHeight() * 0.5f), spriteComponent.sprite.getWidth(), spriteComponent.sprite.getHeight()); if (rectangle.contains(pos)) { return entities.get(i); } } return null; }
From source file:com.prisonbreak.game.entities.Guard.java
public boolean detectPlayer() { Rectangle playerBounding = renderer.getPlayer().getSprite().getBoundingRectangle(); Rectangle detectionArea = getDetectArea(); // NOTE: Use three small rectangles (top, left, right) // -> check whether they belong completely inside the detectiona area // four small rectangles represent four "side" of character Rectangle upper = new Rectangle(playerBounding.getX() + playerBounding.getWidth() / 4, playerBounding.getY() + playerBounding.getHeight() / 2, playerBounding.getWidth() / 2, playerBounding.getHeight() / 2); Rectangle lower = new Rectangle(playerBounding.getX() + playerBounding.getWidth() / 4, playerBounding.getY(), playerBounding.getWidth() / 2, playerBounding.getHeight() / 2); Rectangle left = new Rectangle(playerBounding.getX(), playerBounding.getY() + playerBounding.getHeight() / 4, playerBounding.getWidth() / 2, playerBounding.getHeight() / 2); Rectangle right = new Rectangle(playerBounding.getX() + playerBounding.getWidth() / 2, playerBounding.getY() + playerBounding.getHeight() / 4, playerBounding.getWidth() / 2, playerBounding.getHeight() / 2); // Gdx.app.log("detectionArea: ", detectionArea.toString()); // Gdx.app.log("upper: ", upper.toString()); // Gdx.app.log("lower: ", lower.toString()); // Gdx.app.log("right: ", right.toString()); // Gdx.app.log("left: ", left.toString()); // check detection if (currentDirection.equalsIgnoreCase("up")) { return (detectionArea.contains(left) || detectionArea.contains(right) || detectionArea.contains(lower)); } else if (currentDirection.equalsIgnoreCase("down")) { return (detectionArea.contains(right) || detectionArea.contains(left) || detectionArea.contains(upper)); } else if (currentDirection.equalsIgnoreCase("left")) { return (detectionArea.contains(upper) || detectionArea.contains(lower) || detectionArea.contains(right)); } else if (currentDirection.equalsIgnoreCase("right")) { return (detectionArea.contains(upper) || detectionArea.contains(lower) || detectionArea.contains(left)); }/*w w w. ja v a 2 s .co m*/ // if (detectionArea.contains(playerBounding)) // return true; // // if Player steps in detection area // if (playerBounding.overlaps(detectionArea)) { // Gdx.app.log("Player: ", "inside detection area"); // Gdx.app.log("Player bounding: ", playerBounding.toString()); // Gdx.app.log("Detection area: ", detectionArea.toString()); // // float percent = 0.5f; // // // four bounding points of playerBounding // float xmin = playerBounding.getX(); // float ymin = playerBounding.getY(); // float xmax = xmin + playerBounding.getWidth(); // float ymax = ymin + playerBounding.getHeight(); // // // four bounding points of detectionArea // float xMin = detectionArea.getX(); // float yMin = detectionArea.getY(); // float xMax = xMin + detectionArea.getWidth(); // float yMax = yMin + detectionArea.getHeight(); // // // Guard is facing up direction // if (currentDirection.equalsIgnoreCase("up")) { // if (renderer.getPlayer().getCurrentDirection().equalsIgnoreCase("left")) { // return (xMax - xmin) >= percent * (xmax - xmin); // } // else if (renderer.getPlayer().getCurrentDirection().equalsIgnoreCase("right")) { // return (xmax - xMin) >= percent * (xmax - xmin); // } // else if (renderer.getPlayer().getCurrentDirection().equalsIgnoreCase("down")){ //// Gdx.app.log("yMax = ", "" + yMax); //// Gdx.app.log("ymin = ", "" + ymin); //// Gdx.app.log("yMax - ymin = ", "" + (yMax - ymin)); // // return (yMax - ymin) >= percent * (ymax - ymin); // } // else { // "up" and "none" // // } // } // // Guard is facing down direction (or, "none") // else if (currentDirection.equalsIgnoreCase("down") || // currentDirection.equalsIgnoreCase("none")) { // float deltaLeft = Math.abs(xMax - xmin); // "left" side of Player in the area // float deltaRight = Math.abs(xmax - xMin); // "right" side // float deltaUp = Math.abs(ymax - yMin); // "up" side // // return (deltaLeft >= percent * width) || (deltaRight >= percent * width) || // (deltaUp >= percent * height); // } // // Guard is facing left direction // else if (currentDirection.equalsIgnoreCase("left")) { // float deltaRight = Math.abs(xmax - xMin); // "right" side // float deltaUp = Math.abs(ymax - yMin); // "up" side // float deltaDown = Math.abs(yMax - ymin); // "down" side // // return (deltaRight >= percent * width) || (deltaUp >= percent * height) || // (deltaDown >= percent * height); // } // // Guard is facing right direction // else if (currentDirection.equalsIgnoreCase("right")) { // float deltaLeft = Math.abs(xMax - xmin); // "left" side // float deltaUp = Math.abs(ymax - yMin); // "up" side // float deltaDown = Math.abs(yMax - ymin); // "down" side // // return (deltaLeft >= percent * width) || (deltaUp >= percent * height) || // (deltaDown >= percent * height); // } // } return false; }
From source file:com.redtoorange.game.states.MissionState.java
License:Open Source License
/** * Load the enemies and power-ups into the gameMap. Their position it randomly generated based on the Map's max * height and width. The number of each it determined by the TMX file's properties. * * @param exclusionZone The area that nothing should spawn in, usually the player's spawn area. */// w w w .ja v a 2 s .c o m private void loadObjects(Rectangle exclusionZone) { remainingEnemies = gameMap.getEnemyCount(); //Load the enemies. for (int i = 0; i < gameMap.getEnemyCount(); i++) { Vector2 point = new Vector2(MathUtils.random(2, gameMap.getMaxWidth() - 1), MathUtils.random(2, gameMap.getMaxHeight() - 1)); while (exclusionZone.contains(point)) point = new Vector2(MathUtils.random(2, gameMap.getMaxWidth() - 1), MathUtils.random(2, gameMap.getMaxHeight() - 1)); sceneRoot.addChild(new Enemy(sceneRoot, physicsSystem, point, player)); } //Load ammo power-ups. for (int i = 0; i < gameMap.getAmmoCount(); i++) { Vector2 point = new Vector2(MathUtils.random(2, gameMap.getMaxWidth() - 1), MathUtils.random(2, gameMap.getMaxHeight() - 1)); while (exclusionZone.contains(point)) point = new Vector2(MathUtils.random(2, gameMap.getMaxWidth() - 1), MathUtils.random(2, gameMap.getMaxHeight() - 1)); sceneRoot.addChild(new Ammo(sceneRoot, point, physicsSystem)); } //Load health power-ups. for (int i = 0; i < gameMap.getHealthCount(); i++) { Vector2 point = new Vector2(MathUtils.random(2, gameMap.getMaxWidth() - 1), MathUtils.random(2, gameMap.getMaxHeight() - 1)); while (exclusionZone.contains(point)) point = new Vector2(MathUtils.random(2, gameMap.getMaxWidth() - 1), MathUtils.random(2, gameMap.getMaxHeight() - 1)); sceneRoot.addChild(new Health(sceneRoot, point, physicsSystem)); } }
From source file:com.retrom.volcano.game.World.java
License:Apache License
private void updateCoins(float deltaTime) { boolean coinCrushed = false; boolean powerupCrushed = false; for (Collectable c : collectables_) { if (magnetTime > 0 && !c.isPowerup()) { if (c.state() == Collectable.STATUS_FALLING || c.state() == Collectable.STATUS_IDLE || c.state() == Collectable.STATUS_MAGNETIZED) { c.magnetTo(player.position, deltaTime); }//from w w w . jav a 2 s . c om /// TODO: depend on deltaTime if (Math.random() / 15f < deltaTime) { addEffectsUnder.add(new MagnetTrailParticle(c.position)); } } c.setObstacles(obstacles_); c.update(deltaTime); for (Rectangle rect : floors_.getRects()) { if (c.bounds.overlaps(rect)) { if (c.state() == Collectable.STATUS_IDLE || rect.contains(c.bounds) || c.state() == Collectable.STATUS_MAGNETIZED && rect.contains(c.position)) { c.setState(Collectable.STATUS_CRUSHED); } else if (c.state() == Collectable.STATUS_FALLING) { c.setState(Collectable.STATUS_IDLE); c.velocity.x = c.velocity.y = 0; c.bounds.y = rect.y + rect.height; c.bounds.getCenter(c.position); c.velocity.y = 0; } } } if (player.isAlive() && c.bounds.overlaps(player.bounds)) { if ((c.state() != Collectable.STATUS_FALLING && c.state() != Collectable.STATUS_MAGNETIZED) || c.stateTime() > 0.2) { c.setState(Collectable.STATUS_TAKEN); handleCollectable(c); } } if (c.state() == Collectable.STATUS_CRUSHED) { if (c.isPowerup()) { if (!powerupCrushed) { SoundAssets.playSound(SoundAssets.powerupCrushed); powerupCrushed = true; } addEffects.add(EffectFactory.powerupCrushedEffect(c.type, c.position)); } else { if (!coinCrushed) { SoundAssets.playSound(SoundAssets.coinCrushed); coinCrushed = true; } effects.add(EffectFactory.coinCrushParticle(c.type, c.position)); effects.add(EffectFactory.coinCrushParticle(c.type, c.position)); effects.add(EffectFactory.coinCrushParticle(c.type, c.position)); screenEffects.add(EffectFactory.coinCrushedEffect(c.position)); } } } // Remove crushed coins. for (Iterator<Collectable> it = collectables_.iterator(); it.hasNext();) { Collectable coin = it.next(); if (coin.state() == Collectable.STATUS_CRUSHED || coin.state() == Collectable.STATUS_TAKEN) { it.remove(); } } }
From source file:com.tnf.ptm.screens.controlers.PtmInputManager.java
License:Apache License
public void update(PtmApplication ptmApplication) { boolean mobile = ptmApplication.isMobile(); PtmGame game = ptmApplication.getGame(); // This keeps the mouse within the window, but only when playing the game with the mouse. // All other times the mouse can freely leave and return. if (!mobile// w ww . ja va 2s . c o m && (ptmApplication.getOptions().controlType == GameOptions.CONTROL_MIXED || ptmApplication.getOptions().controlType == GameOptions.CONTROL_MOUSE) && game != null && getTopScreen() != game.getScreens().menuScreen) { if (!Gdx.input.isCursorCatched()) { Gdx.input.setCursorCatched(true); } maybeFixMousePos(); } else { if (Gdx.input.isCursorCatched()) { Gdx.input.setCursorCatched(false); } } updatePointers(); boolean consumed = false; mouseOnUi = false; boolean clickOutsideReacted = false; for (PtmUiScreen screen : screens) { boolean consumedNow = false; List<PtmUiControl> controls = screen.getControls(); for (PtmUiControl control : controls) { control.update(inputPointers, currCursor != null, !consumed, this, ptmApplication); if (control.isOn() || control.isJustOff()) { consumedNow = true; } Rectangle area = control.getScreenArea(); if (area != null && area.contains(mousePos)) { mouseOnUi = true; } } if (consumedNow) { consumed = true; } boolean clickedOutside = false; if (!consumed) { for (InputPointer inputPointer : inputPointers) { boolean onBg = screen.isCursorOnBg(inputPointer); if (inputPointer.pressed && onBg) { clickedOutside = false; consumed = true; break; } if (!onBg && inputPointer.isJustUnPressed() && !clickOutsideReacted) { clickedOutside = true; } } } if (clickedOutside && screen.reactsToClickOutside()) { clickOutsideReacted = true; } if (screen.isCursorOnBg(inputPointers[0])) { mouseOnUi = true; } screen.updateCustom(ptmApplication, inputPointers, clickedOutside); } TutorialManager tutorialManager = game == null ? null : game.getTutMan(); if (tutorialManager != null && tutorialManager.isFinished()) { ptmApplication.finishGame(); } updateCursor(ptmApplication); addRemoveScreens(); updateWarnPerc(); scrolledUp = null; }
From source file:com.uwsoft.editor.gdx.actors.SelectionRectangle.java
License:Apache License
@Override public void act(float delta) { super.act(delta); if (mode != EditingMode.TRANSFORM) return;/* w w w. j a va 2s .c om*/ Vector2 mouseCoords = getMouseLocalCoordinates(); sandbox.setCurrentlyTransforming(null, -1); boolean isOver = false; for (int i = 0; i < 8; i++) { final int currRectIndex = i; Rectangle rect = new Rectangle(miniRects[currRectIndex].getX() - 2, miniRects[currRectIndex].getY() - 2, 8, 8); if (rect.contains(mouseCoords) && !(getHostAsActor() instanceof LabelItem)) { sandbox.setCurrentlyTransforming(getHost(), currRectIndex); isOver = true; switch (currRectIndex) { case LT: sandbox.getSandboxStage().setCursor(Cursor.NW_RESIZE_CURSOR); break; case T: sandbox.getSandboxStage().setCursor(Cursor.N_RESIZE_CURSOR); break; case RT: sandbox.getSandboxStage().setCursor(Cursor.NE_RESIZE_CURSOR); break; case R: sandbox.getSandboxStage().setCursor(Cursor.E_RESIZE_CURSOR); break; case RB: sandbox.getSandboxStage().setCursor(Cursor.SE_RESIZE_CURSOR); break; case B: sandbox.getSandboxStage().setCursor(Cursor.S_RESIZE_CURSOR); break; case LB: sandbox.getSandboxStage().setCursor(Cursor.SW_RESIZE_CURSOR); break; case L: sandbox.getSandboxStage().setCursor(Cursor.W_RESIZE_CURSOR); break; } } } if (!isOver) { sandbox.getSandboxStage().setCursor(Cursor.DEFAULT_CURSOR); } // change size according to zoom }
From source file:es.eucm.ead.editor.view.widgets.groupeditor.GroupEditorDragListener.java
License:Open Source License
/** * @return the directly children of the current edited group that are inside * the given selection rectangle */// w ww . jav a2 s .c om private Array<Actor> getActorsInside(Rectangle selection) { if (selection.width < 0) { selection.x += selection.width; selection.width = Math.abs(selection.width); } if (selection.height < 0) { selection.y += selection.height; selection.height = Math.abs(selection.height); } Vector2 o = new Vector2(); Vector2 t = new Vector2(); Vector2 n = new Vector2(); Vector2 d = new Vector2(); Array<Actor> actors = new Array<Actor>(); for (Actor a : editedGroup.getChildren()) { o.set(0, 0); t.set(a.getWidth(), 0); n.set(0, a.getHeight()); d.set(a.getWidth(), a.getHeight()); a.localToAscendantCoordinates(groupEditor, o); a.localToAscendantCoordinates(groupEditor, t); a.localToAscendantCoordinates(groupEditor, n); a.localToAscendantCoordinates(groupEditor, d); if (selection.contains(o) && selection.contains(t) && selection.contains(n) && selection.contains(d) && a.isTouchable() && a.isVisible()) { actors.add(a); } } return actors; }
From source file:org.catrobat.catroid.sensing.CollisionDetection.java
License:Open Source License
public static double collidesWithEdge(Look look) { int virtualScreenWidth = ProjectManager.getInstance().getCurrentProject().getXmlHeader().virtualScreenWidth; int virtualScreenHeight = ProjectManager.getInstance().getCurrentProject() .getXmlHeader().virtualScreenHeight; Rectangle screen = new Rectangle(-virtualScreenWidth / 2, -virtualScreenHeight / 2, virtualScreenWidth, virtualScreenHeight);/*from w ww. java 2 s . c o m*/ //check if any line of the collision polygons intersects with the screen boundary for (Polygon polygon : look.getCurrentCollisionPolygon()) { for (int i = 0; i < polygon.getTransformedVertices().length - 4; i += 2) { Vector2 firstPoint = new Vector2(polygon.getTransformedVertices()[i], polygon.getTransformedVertices()[i + 1]); Vector2 secondPoint = new Vector2(polygon.getTransformedVertices()[i + 2], polygon.getTransformedVertices()[i + 3]); //if the line crosses the screen boarder, a collision is detected if (screen.contains(firstPoint) ^ screen.contains(secondPoint)) { return 1.0d; } } } return 0d; }