List of usage examples for com.badlogic.gdx.math Vector2 Vector2
public Vector2(float x, float y)
From source file:com.altportalgames.colorrain.utils.TiledMapHelper.java
License:Apache License
/** * Reads a file describing the collision boundaries that should be set * per-tile and adds static bodies to the boxd world. * /* w w w . j a va 2 s . com*/ * @param collisionsFile * @param world * @param pixelsPerMeter * the pixels per meter scale used for this world */ public void loadCollisions(String collisionsFile, World world, float pixelsPerMeter) { System.out.println("tilewidth " + getMap().height); /** * Detect the tiles and dynamically create a representation of the map * layout, for collision detection. Each tile has its own collision * rules stored in an associated file. * * The file contains lines in this format (one line per type of tile): * tileNumber XxY,XxY XxY,XxY * * Ex: * * 3 0x0,31x0 ... 4 0x0,29x0 29x0,29x31 * * For a 32x32 tileset, the above describes one line segment for tile #3 * and two for tile #4. Tile #3 has a line segment across the top. Tile * #1 has a line segment across most of the top and a line segment from * the top to the bottom, 30 pixels in. */ FileHandle fh = Gdx.files.internal(collisionsFile); String collisionFile = fh.readString(); String lines[] = collisionFile.split("\\r?\\n"); HashMap<Integer, ArrayList<LineSegment>> tileCollisionJoints = new HashMap<Integer, ArrayList<LineSegment>>(); /** * Some locations on the map (perhaps most locations) are "undefined", * empty space, and will have the tile type 0. This code adds an empty * list of line segments for this "default" tile. */ tileCollisionJoints.put(Integer.valueOf(0), new ArrayList<LineSegment>()); for (int n = 0; n < lines.length; n++) { String cols[] = lines[n].split(" "); int tileNo = Integer.parseInt(cols[0]); ArrayList<LineSegment> tmp = new ArrayList<LineSegment>(); for (int m = 1; m < cols.length; m++) { String coords[] = cols[m].split(","); String start[] = coords[0].split("x"); String end[] = coords[1].split("x"); tmp.add(new LineSegment(Integer.parseInt(start[0]), Integer.parseInt(start[1]), Integer.parseInt(end[0]), Integer.parseInt(end[1]))); } tileCollisionJoints.put(Integer.valueOf(tileNo), tmp); } ArrayList<LineSegment> collisionLineSegments = new ArrayList<LineSegment>(); for (int y = 0; y < getMap().height; y++) { for (int x = 0; x < getMap().width; x++) { int tileType = getMap().layers.get(1).tiles[(getMap().height - 1) - y][x]; for (int n = 0; n < tileCollisionJoints.get(Integer.valueOf(tileType)).size(); n++) { LineSegment lineSeg = tileCollisionJoints.get(Integer.valueOf(tileType)).get(n); addOrExtendCollisionLineSegment(x * Assets.tileWidth + lineSeg.start().x, y * Assets.tileHeight - lineSeg.start().y + Assets.tileHeight, x * Assets.tileWidth + lineSeg.end().x, y * Assets.tileHeight - lineSeg.end().y + Assets.tileHeight, collisionLineSegments); } } } BodyDef groundBodyDef = new BodyDef(); groundBodyDef.type = BodyDef.BodyType.StaticBody; groundBody = world.createBody(groundBodyDef); for (LineSegment lineSegment : collisionLineSegments) { PolygonShape environmentShape = new PolygonShape(); environmentShape.setAsEdge(lineSegment.start().mul(1 / Assets.PIXELS_PER_METER_X), lineSegment.end().mul(1 / Assets.PIXELS_PER_METER_X)); groundBody.createFixture(environmentShape, 0); environmentShape.dispose(); } /** * Drawing a boundary around the entire map. We can't use a box because * then the world objects would be inside and the physics engine would * try to push them out. */ PolygonShape mapBounds = new PolygonShape(); mapBounds.setAsEdge(new Vector2(0.0f, 0.0f), new Vector2(getWidth() / Assets.PIXELS_PER_METER_X, 0.0f)); groundBody.createFixture(mapBounds, 0); mapBounds.setAsEdge(new Vector2(0.0f, getHeight() / Assets.PIXELS_PER_METER_Y), new Vector2(getWidth() / Assets.PIXELS_PER_METER_X, getHeight() / Assets.PIXELS_PER_METER_Y)); groundBody.createFixture(mapBounds, 0); mapBounds.setAsEdge(new Vector2(0.0f, 0.0f), new Vector2(0.0f, getHeight() / Assets.PIXELS_PER_METER_Y)); groundBody.createFixture(mapBounds, 0); mapBounds.setAsEdge(new Vector2(getWidth() / Assets.PIXELS_PER_METER_X, 0.0f), new Vector2(getWidth() / Assets.PIXELS_PER_METER_X, getHeight() / Assets.PIXELS_PER_METER_Y)); groundBody.createFixture(mapBounds, 0); mapBounds.dispose(); }
From source file:com.andlabs.games.ouyatennis.OuyaTennis.java
License:Apache License
@Override public void resize(int arg0, int arg1) { _logger.info("Resizing to: " + arg0 + "x" + arg1); // calculate new viewport float aspectRatio = (float) arg0 / (float) arg1; float scale = 1f; Vector2 crop = new Vector2(0f, 0f); if (aspectRatio > ASPECT_RATIO) { scale = (float) arg1 / (float) VIRTUAL_HEIGHT; crop.x = (arg0 - VIRTUAL_WIDTH * scale) / 2.0f; } else if (aspectRatio < ASPECT_RATIO) { scale = (float) arg0 / (float) VIRTUAL_WIDTH; crop.y = (arg1 - VIRTUAL_HEIGHT * scale) / 2.0f; } else {// www .j av a2s .com scale = (float) arg0 / (float) VIRTUAL_WIDTH; } float w = (float) VIRTUAL_WIDTH * scale; float h = (float) VIRTUAL_HEIGHT * scale; _viewport = new Rectangle(crop.x, crop.y, w, h); }
From source file:com.android.projet.projetandroid.game.superjumper.World.java
License:Apache License
/** * Generate the good asset depending to markers *//* w w w. j av a2 s.c o m*/ private void generateLevel() { for (int i = 0; i < markersPosition.size(); ++i) { Platform platform; if (markersPosition.get(i).getType() == MarkerType.MOVING) { platform = new Platform(Platform.PLATFORM_TYPE_MOVING, markersPosition.get(i).getPosition().x, markersPosition.get(i).getPosition().y - Platform.PLATFORM_HEIGHT / 3); platforms.add(platform); } else { platform = new Platform(Platform.PLATFORM_TYPE_STATIC, markersPosition.get(i).getPosition().x, markersPosition.get(i).getPosition().y - Platform.PLATFORM_HEIGHT / 3); platforms.add(platform); if (markersPosition.get(i).getType() == MarkerType.START) { this.startPosition = new Vector2(platform.position.x, platform.position.y); } if (markersPosition.get(i).getType() == MarkerType.TRAMPOLINE) { Spring spring = new Spring(platform.position.x, platform.position.y + Spring.SPRING_HEIGHT); springs.add(spring); } if (markersPosition.get(i).getType() == MarkerType.END) { this.castle = new Castle(platform.position.x, platform.position.y + Castle.CASTLE_HEIGHT / 2); } if (markersPosition.get(i).getType() == MarkerType.ENEMY) { Squirrel squirrel = new Squirrel(platform.position.x, platform.position.y + Squirrel.SQUIRREL_HEIGHT + rand.nextFloat() / 2); squirrels.add(squirrel); } if (markersPosition.get(i).getType() == MarkerType.COIN) { Coin coin = new Coin(platform.position.x, platform.position.y + Coin.COIN_HEIGHT + rand.nextFloat() / 2); coins.add(coin); } } } }
From source file:com.android.ringfly.common.CameraHelper.java
License:Apache License
/** Calculates the dimensions of the viewport required to support the given virtual coordinates. * //from www . j a va2s . c o m * @param isStretched true if the viewport should be stretched to fill the entire window. * @param virtualWidth the width of the viewport in virtual units. * @param virtualHeight the height of the viewport in virtual units. * @return the viewport's dimensions. */ public static Vector2 viewportSize(boolean isStretched, float virtualWidth, float virtualHeight) { float viewportWidth; float viewportHeight; if (isStretched) { // Stretch the camera to fill the entire screen. viewportWidth = virtualWidth; viewportHeight = virtualHeight; } else { // Maintain the aspect ratio by letterboxing. float aspect = virtualWidth / virtualHeight; float physicalWidth = Gdx.graphics.getWidth(); float physicalHeight = Gdx.graphics.getHeight(); if (physicalWidth / physicalHeight >= aspect) { // Letterbox left and right. viewportHeight = virtualHeight; viewportWidth = viewportHeight * physicalWidth / physicalHeight; } else { // Letterbox above and below. viewportWidth = virtualWidth; viewportHeight = viewportWidth * physicalHeight / physicalWidth; } } return new Vector2(viewportWidth, viewportHeight); }
From source file:com.android.ringfly.common.CameraHelper.java
License:Apache License
public static Vector2 sizeToDensity(ViewportMode viewportMode, float virtualWidth, float virtualHeight, float density) { float viewportWidth = virtualWidth; float viewportHeight = virtualHeight; float physicalWidth = Gdx.graphics.getWidth(); float physicalHeight = Gdx.graphics.getHeight(); if (viewportMode == ViewportMode.PIXEL_PERFECT) { float widthAtDensity = viewportWidth * density; float heightAtDensity = viewportHeight * density; viewportWidth *= (physicalWidth / widthAtDensity); viewportHeight *= (physicalHeight / heightAtDensity); } else if (viewportMode == ViewportMode.STRETCH_TO_ASPECT) { float aspect = virtualWidth / virtualHeight; if (physicalWidth / physicalHeight >= aspect) { // Letterbox left and right. viewportHeight = virtualHeight; viewportWidth = viewportHeight * physicalWidth / physicalHeight; } else {//from w w w .j a va 2 s . c om // Letterbox above and below. viewportWidth = virtualWidth; viewportHeight = viewportWidth * physicalHeight / physicalWidth; } } return new Vector2(viewportWidth, viewportHeight); }
From source file:com.anythingmachine.tiledMaps.TiledMapHelper.java
License:Apache License
/** * Reads a file describing the collision boundaries that should be set * per-tile and adds static bodies to the boxd world. * /*from w ww .ja va2s . co m*/ * @param collisionsFile * @param world * @param pixelsPerMeter * the pixels per meter scale used for this world */ public void loadCollisions(String collisionsFile, World world, float pixelsPerMeter, int level) { /** * Detect the tiles and dynamically create a representation of the map * layout, for collision detection. Each tile has its own collision * rules stored in an associated file. * * The file contains lines in this format (one line per type of tile): * tileNumber XxY,XxY XxY,XxY * * Ex: * * 3 0x0,31x0 ... 4 0x0,29x0 29x0,29x31 * * For a 32x32 tileset, the above describes one line segment for tile #3 * and two for tile #4. Tile #3 has a line segment across the top. Tile * #1 has a line segment across most of the top and a line segment from * the top to the bottom, 30 pixels in. */ // FileHandle fh = Gdx.files.internal(collisionsFile); // String collisionFile = fh.readString(); // String lines[] = collisionFile.split("\\r?\\n"); // HashMap<Integer, ArrayList<LineSegment>> tileCollisionJoints = new // HashMap<Integer, ArrayList<LineSegment>>(); // /** // * Some locations on the map (perhaps most locations) are "undefined", // * empty space, and will have the tile type 0. This code adds an empty // * list of line segments for this "default" tile. // */ // tileCollisionJoints.put(Integer.valueOf(0), new // ArrayList<LineSegment>()); // for (int n = 0; n < lines.length; n++) { // String cols[] = lines[n].split(" "); // int tileNo = Integer.parseInt(cols[0]); // // ArrayList<LineSegment> tmp = new ArrayList<LineSegment>(); // // for (int m = 1; m < cols.length; m++) { // String coords[] = cols[m].split(","); // // String start[] = coords[0].split("x"); // String end[] = coords[1].split("x"); // // tmp.add(new LineSegment(Integer.parseInt(start[0]), // Integer.parseInt(start[1]), // Integer.parseInt(end[0]), Integer.parseInt(end[1]), "")); // } // // tileCollisionJoints.put(Integer.valueOf(tileNo), tmp); // } ArrayList<LineSegment> collisionLineSegments = new ArrayList<LineSegment>(); for (int l = 0; l < getMap().getLayers().getCount(); l++) { MapLayer nextLayer = getMap().getLayers().get(l); if (!nextLayer.getName().equals("AINODEMAP") && !nextLayer.getName().equals("DOORS")) { TiledMapTileLayer layer = (TiledMapTileLayer) nextLayer; if (layer.getProperties().containsKey("collide")) { for (int y = 0; y < layer.getHeight(); y++) { for (int x = 0; x < layer.getWidth(); x++) { Cell tile = layer.getCell(x, y); if (tile != null) { int tileID = tile.getTile().getId(); int start = 0; String type = ""; if (tile.getTile().getProperties().containsKey("type")) { type = (String) getMap().getTileSets().getTile(tileID).getProperties() .get("type"); } if (layer.getProperties().containsKey("WALLS")) { type = "WALLS"; addOrExtendCollisionLineSegment(x * 32 + 16, y * 32, x * 32 + 16, y * 32 + 32, collisionLineSegments, type); } else if (layer.getProperties().containsKey("STAIRS")) { if (!type.equals("LEFTSTAIRS")) { type = "STAIRS"; addOrExtendCollisionLineSegment(x * 32, y * 32, x * 32 + 32, y * 32 + 32, collisionLineSegments, type); } else { addOrExtendCollisionLineSegment(x * 32, y * 32 + 32, x * 32 + 32, y * 32, collisionLineSegments, type); } } else if (layer.getProperties().containsKey("PLATFORMS")) { type = "PLATFORMS"; addOrExtendCollisionLineSegment(x * 32, y * 32 + 32, x * 32 + 32, y * 32 + 32, collisionLineSegments, type); } } } } } } else { MapObjects objects = nextLayer.getObjects(); for (MapObject o : objects) { RectangleMapObject rect = (RectangleMapObject) o; if (rect.getProperties().containsKey("set") || rect.getProperties().containsKey("to_level")) { Rectangle shape = rect.getRectangle(); BodyDef nodeBodyDef = new BodyDef(); nodeBodyDef.type = BodyDef.BodyType.StaticBody; nodeBodyDef.position.set((shape.x + shape.width * 0.5f) * Util.PIXEL_TO_BOX, (shape.y + shape.height * 0.5f) * Util.PIXEL_TO_BOX); Body nodeBody = GamePlayManager.world.createBody(nodeBodyDef); if (!nextLayer.getName().equals("DOORS")) { String set = (String) rect.getProperties().get("set"); nodeBody.setUserData(new AINode(Integer.parseInt(set))); } else { if (rect.getProperties().containsKey("to_level") && rect.getProperties().containsKey("exitX") && rect.getProperties().containsKey("exitY")) { String to_level = (String) rect.getProperties().get("to_level"); String xPos = (String) rect.getProperties().get("exitX"); String yPos = (String) rect.getProperties().get("exitY"); nodeBody.setUserData(new Door(to_level, xPos, yPos)); } else { nodeBody.setUserData(new Door("9999", "1024", "256")); } } PolygonShape nodeShape = new PolygonShape(); nodeShape.setAsBox(shape.width * 0.5f * Util.PIXEL_TO_BOX, shape.height * 0.5f * Util.PIXEL_TO_BOX); FixtureDef fixture = new FixtureDef(); fixture.shape = nodeShape; fixture.isSensor = true; fixture.density = 0; fixture.filter.categoryBits = Util.CATEGORY_PLATFORMS; fixture.filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; nodeBody.createFixture(fixture); nodeShape.dispose(); } } } } int platnum = 0; for (LineSegment lineSegment : collisionLineSegments) { BodyDef groundBodyDef = new BodyDef(); groundBodyDef.type = BodyDef.BodyType.StaticBody; groundBodyDef.position.set(0, 0); Body groundBody = GamePlayManager.world.createBody(groundBodyDef); if (lineSegment.type.equals("STAIRS") || lineSegment.type.equals("LEFTSTAIRS")) { groundBody.setUserData( new Stairs("stairs_" + level + "_" + platnum, lineSegment.start(), lineSegment.end())); } else if (lineSegment.type.equals("WALLS")) { groundBody.setUserData(new Entity().setType(EntityType.WALL)); } else { Platform plat = new Platform("plat_" + level + "_" + platnum, lineSegment.start(), lineSegment.end()); groundBody.setUserData(plat); if (GamePlayManager.lowestPlatInLevel == null || lineSegment.start().y < GamePlayManager.lowestPlatInLevel.getDownPosY()) { GamePlayManager.lowestPlatInLevel = plat; } } EdgeShape environmentShape = new EdgeShape(); environmentShape.set(lineSegment.start().scl(1 / pixelsPerMeter), lineSegment.end().scl(1 / pixelsPerMeter)); FixtureDef fixture = new FixtureDef(); fixture.shape = environmentShape; fixture.isSensor = true; fixture.density = 1f; fixture.filter.categoryBits = Util.CATEGORY_PLATFORMS; fixture.filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER | Util.CATEGORY_PARTICLES; groundBody.createFixture(fixture); environmentShape.dispose(); } /** * Drawing a boundary around the entire map. We can't use a box because * then the world objects would be inside and the physics engine would * try to push them out. */ TiledMapTileLayer layer = (TiledMapTileLayer) getMap().getLayers().get(3); BodyDef bodydef = new BodyDef(); bodydef.type = BodyType.StaticBody; bodydef.position.set(0, 0); Body body = GamePlayManager.world.createBody(bodydef); // left wall EdgeShape mapBounds = new EdgeShape(); if (level == 1) { mapBounds.set(new Vector2(0.0f, 0.0f), new Vector2(0, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); body.setUserData(new Entity().setType(EntityType.WALL)); Fixture fixture = body.createFixture(mapBounds, 0); Filter filter = new Filter(); filter.categoryBits = Util.CATEGORY_PLATFORMS; filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; fixture.setFilterData(filter); } else { mapBounds.set(new Vector2(0f * Util.PIXEL_TO_BOX, 0.0f), new Vector2(0f, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); body.setUserData(new LevelWall(level - 1)); Fixture fixture = body.createFixture(mapBounds, 0); Filter filter = new Filter(); filter.categoryBits = Util.CATEGORY_PLATFORMS; filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; fixture.setFilterData(filter); } // right wall body = GamePlayManager.world.createBody(bodydef); body.setUserData(new LevelWall(level + 1)); EdgeShape mapBounds2 = new EdgeShape(); mapBounds2.set(new Vector2((layer.getWidth() * 32), 0.0f).scl(Util.PIXEL_TO_BOX), new Vector2((layer.getWidth() * 32), layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); Fixture fixture = body.createFixture(mapBounds2, 0); Filter filter = new Filter(); filter.categoryBits = Util.CATEGORY_PLATFORMS; filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; fixture.setFilterData(filter); // roof body = GamePlayManager.world.createBody(bodydef); body.setUserData(new Platform("roof_" + level, new Vector2(0.0f, layer.getHeight() * 32), new Vector2(layer.getWidth() * 32, layer.getHeight()))); EdgeShape mapBounds3 = new EdgeShape(); mapBounds3.set(new Vector2(0.0f, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX), new Vector2(layer.getWidth() * 32, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); fixture = body.createFixture(mapBounds3, 0); fixture.setFilterData(filter); mapBounds.dispose(); mapBounds2.dispose(); mapBounds3.dispose(); }
From source file:com.apptogo.roperace.custom.MyTouchpad.java
License:Apache License
public float getAngle() { return new Vector2(getKnobX() - getWidth() / 2, getKnobY() - getHeight() / 2).angle(); }
From source file:com.arkanoid.game.model.GameField.java
License:Apache License
public void launchBall() { //ball.getBody().setLinearVelocity(new Vector2(0, -50)); ball.getBody().applyLinearImpulse(new Vector2(0, -10000), ball.getBody().getPosition(), true); }
From source file:com.arkanoid.game.model.GameField.java
License:Apache License
public void vausMove(float moveX) { Vector2 velocity = new Vector2(moveX, 0); if ((this.vaus.getBody().getPosition().x - this.vaus.width / 2) <= 0) { velocity = new Vector2(100, 0); }/*from w w w. j av a 2 s . c o m*/ if ((this.vaus.getBody().getPosition().x + this.vaus.width / 2) >= WORLD_WIDTH) { velocity = new Vector2(-100, 0); } this.vaus.getBody().setLinearVelocity(velocity); }
From source file:com.axatrikx.solor.view.LevelScreen.java
License:Apache License
/** * /*from w w w . j ava 2 s . c om*/ */ private void initObjects() { player = new Player(this, getPlayerSpawnPosition()); platforms = new Array<BasePlatform>(); // init platforms platforms.add(new BasePlatform(this, Shape.CIRCLE, Color.RED, new Vector2(120, 300))); platforms.add(new BasePlatform(this, Shape.TRIANGE, Color.GREEN, new Vector2(250, 100))); platforms.add(new BasePlatform(this, Shape.TRIANGE, Color.YELLOW, new Vector2(880, 100))); platforms.add(new BasePlatform(this, Shape.CIRCLE, Color.GREEN, new Vector2(180, 530))); platforms.add(new BasePlatform(this, Shape.CIRCLE, Color.RED, new Vector2(380, 300))); backgroundTexture = new Texture(Gdx.files.internal("images/background.png")); backgroundTexture1 = backgroundTexture; backgroundTexture2 = backgroundTexture; backgroundTexture3 = backgroundTexture; textureW = backgroundTexture.getWidth(); textureH = backgroundTexture.getHeight(); texture2X = texture1X + textureW; texture2Y = texture1Y + textureH; }