List of usage examples for com.badlogic.gdx.math Vector2 Vector2
public Vector2(float x, float y)
From source file:com.baldwin.libgdx.commons.SimpleTiledMapHelper.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 www.j av a 2s .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) { /** * 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(0).tiles[(getMap().height - 1) - y][x]; ArrayList<LineSegment> a = tileCollisionJoints.get(Integer.valueOf(tileType)); if (null == a) continue; for (int n = 0; n < a.size(); n++) { LineSegment lineSeg = tileCollisionJoints.get(Integer.valueOf(tileType)).get(n); addOrExtendCollisionLineSegment(x * getMap().tileWidth + lineSeg.start().x, y * getMap().tileHeight - lineSeg.start().y + 32, x * getMap().tileWidth + lineSeg.end().x, y * getMap().tileHeight - lineSeg.end().y + 32, collisionLineSegments); } } } BodyDef groundBodyDef = new BodyDef(); groundBodyDef.type = BodyDef.BodyType.StaticBody; Body groundBody = world.createBody(groundBodyDef); for (LineSegment lineSegment : collisionLineSegments) { EdgeShape environmentShape = new EdgeShape(); environmentShape.set(lineSegment.start().mul(1 / pixelsPerMeter), lineSegment.end().mul(1 / pixelsPerMeter)); 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. */ EdgeShape mapBounds = new EdgeShape(); mapBounds.set(new Vector2(0.0f, 0.0f), new Vector2(getWidth() / pixelsPerMeter, 0.0f)); groundBody.createFixture(mapBounds, 0); mapBounds.set(new Vector2(0.0f, getHeight() / pixelsPerMeter), new Vector2(getWidth() / pixelsPerMeter, getHeight() / pixelsPerMeter)); groundBody.createFixture(mapBounds, 0); mapBounds.set(new Vector2(0.0f, 0.0f), new Vector2(0.0f, getHeight() / pixelsPerMeter)); groundBody.createFixture(mapBounds, 0); mapBounds.set(new Vector2(getWidth() / pixelsPerMeter, 0.0f), new Vector2(getWidth() / pixelsPerMeter, getHeight() / pixelsPerMeter)); groundBody.createFixture(mapBounds, 0); mapBounds.dispose(); }
From source file:com.baldwin.libgdx.commons.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 v a2 s. 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) { /** * 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(0).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 * getMap().tileWidth + lineSeg.start().x, y * getMap().tileHeight - lineSeg.start().y + 32, x * getMap().tileWidth + lineSeg.end().x, y * getMap().tileHeight - lineSeg.end().y + 32, collisionLineSegments); } } } BodyDef groundBodyDef = new BodyDef(); groundBodyDef.type = BodyDef.BodyType.StaticBody; Body groundBody = world.createBody(groundBodyDef); for (LineSegment lineSegment : collisionLineSegments) { EdgeShape environmentShape = new EdgeShape(); environmentShape.set(lineSegment.start().mul(1 / pixelsPerMeter), lineSegment.end().mul(1 / pixelsPerMeter)); 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. */ EdgeShape mapBounds = new EdgeShape(); mapBounds.set(new Vector2(0.0f, 0.0f), new Vector2(getWidth() / pixelsPerMeter, 0.0f)); groundBody.createFixture(mapBounds, 0); mapBounds.set(new Vector2(0.0f, getHeight() / pixelsPerMeter), new Vector2(getWidth() / pixelsPerMeter, getHeight() / pixelsPerMeter)); groundBody.createFixture(mapBounds, 0); mapBounds.set(new Vector2(0.0f, 0.0f), new Vector2(0.0f, getHeight() / pixelsPerMeter)); groundBody.createFixture(mapBounds, 0); mapBounds.set(new Vector2(getWidth() / pixelsPerMeter, 0.0f), new Vector2(getWidth() / pixelsPerMeter, getHeight() / pixelsPerMeter)); groundBody.createFixture(mapBounds, 0); mapBounds.dispose(); }
From source file:com.barconr.games.marblegame.Ball.java
License:Apache License
public Ball(float x, float y) { position = new Vector2(x, y); }
From source file:com.barconr.games.marblegame.Physics2D.java
License:Apache License
public void createFixtures(TiledMap tiledmap) { int layerHeight = 0, layerWidth = 0; float tileWidth = 0, tileHeight = 0; ballsListener = new BallContactListener(); // Load the Tiled map layer and store some relevant variables TiledMapTileLayer tilelayer = (TiledMapTileLayer) (tiledmap.getLayers().get(0)); layerHeight = tilelayer.getHeight(); layerWidth = tilelayer.getWidth();/* ww w. j a v a 2 s .c o m*/ maze = new Maze(layerWidth, layerHeight); tileHeight = tilelayer.getTileHeight(); tileWidth = tilelayer.getTileWidth(); System.out.println("Layer height (tiles) = " + layerHeight + " Layer width (tiles) = " + layerWidth + " Tile height = " + tileHeight + "tile width = " + tileWidth); System.out.println("Tile count = " + tilelayer.getObjects().getCount()); System.out.println("layer height pixels = " + (layerHeight * tileHeight) + "LAYER WIDTH PIXELS : " + (layerWidth * tileWidth)); //Create the box2d world world = new World(new Vector2(0, -9), true); world.setContactListener(ballsListener); // Loop through the grid reference for (int y_pos = 0; y_pos < layerHeight; y_pos++) { for (int x_pos = 0; x_pos < layerWidth; x_pos++) { boolean impassibleBlock = tilelayer.getCell(x_pos, layerHeight - y_pos - 1).getTile() .getProperties().containsKey("block"); // If the tile square contains the reference "start" // Store this as the start position if (tilelayer.getCell(x_pos, layerHeight - y_pos - 1).getTile().getProperties() .containsKey("start")) { if (startPosition == null) { System.out.println("x:" + x_pos * tileWidth + " y:" + y_pos * tileHeight); startPosition = new Vector2(x_pos * tileWidth * Assets.METERS_PER_PIXEL, (50 - y_pos) * tileHeight * Assets.METERS_PER_PIXEL); } } //Create a fixture for the end position if (tilelayer.getCell(x_pos, layerHeight - y_pos - 1).getTile().getProperties() .containsKey("end")) { //Draw box for fixture that is impassible PolygonShape squareShape = new PolygonShape(); BodyDef squareBodyDef = new BodyDef(); squareBodyDef.type = BodyType.StaticBody; //Static body which won't move //Box position squareBodyDef.position.set(new Vector2((x_pos * Assets.METERS_PER_PIXEL * tileWidth), (layerHeight - y_pos - 1) * Assets.METERS_PER_PIXEL * tileHeight)); //Correction for fact Box2Ds squares are half width/height from center point squareBodyDef.position.add(tileWidth / 2 * Assets.METERS_PER_PIXEL, tileHeight / 2 * Assets.METERS_PER_PIXEL); Body squareBody = world.createBody(squareBodyDef); squareShape.setAsBox(tileWidth / 2 * Assets.METERS_PER_PIXEL, tileHeight / 2 * Assets.METERS_PER_PIXEL); FixtureDef fixDefSquare = new FixtureDef(); fixDefSquare.shape = squareShape; fixDefSquare.isSensor = true; fixDefSquare.density = 0.1f; fixDefSquare.restitution = 0.3f; Fixture endFix = squareBody.createFixture(fixDefSquare); endFix.setSensor(true); endFix.setUserData("exit"); } if (impassibleBlock) { //Draw box for fixture that blocks maze.setMazeUnit(new MazeUnit("block"), x_pos, y_pos); //System.out.print("@"); // Draw ascii map in stdout } else { maze.setMazeUnit(new MazeUnit("passage"), x_pos, y_pos); } } //System.out.println(); } // The players ball playerBallBodyDef = new BodyDef(); playerBallBodyDef.type = BodyType.DynamicBody; if (startPosition == null) { playerBallBodyDef.position.set(3f, 3f); } else { playerBallBodyDef.position.set(startPosition.x, startPosition.y); } playerBallBodyDef.allowSleep = false; ballCircleBody = world.createBody(playerBallBodyDef); dynamicCircle = new CircleShape(); dynamicCircle.setRadius(8 / Assets.PIXELS_PER_METER); FixtureDef ballFixtureDef = new FixtureDef(); ballFixtureDef.shape = dynamicCircle; ballFixtureDef.density = 0.1f; ballFixtureDef.friction = 1f; //ballFixtureDef. ballFixtureDef.restitution = 0.5f; //ballCircleBody.setUserData("ball"); Fixture fx = ballCircleBody.createFixture(ballFixtureDef); fx.setUserData("ball"); maze.removeExtraBoxes(); /* PolygonShape squareShape = new PolygonShape(); */ for (int i = 0; i < maze.width; i++) { for (int j = 0; j < maze.height; j++) { if (maze.mazeUnits[j][i].getType().equals("block")) { BodyDef squareBodyDef = new BodyDef(); PolygonShape squareShape = new PolygonShape(); squareBodyDef.type = BodyType.StaticBody; //Static body which won't move //Box position squareBodyDef.position.set(new Vector2((j * Assets.METERS_PER_PIXEL * tileWidth), (layerHeight - i - 1) * Assets.METERS_PER_PIXEL * tileHeight)); //Correction for fact Box2Ds squares are half width/height from center point squareBodyDef.position.add(tileWidth / 2 * Assets.METERS_PER_PIXEL, tileHeight / 2 * Assets.METERS_PER_PIXEL); Body squareBody = world.createBody(squareBodyDef); //Size of box squareShape.setAsBox(tileWidth / 2 * Assets.METERS_PER_PIXEL, tileHeight / 2 * Assets.METERS_PER_PIXEL); FixtureDef fixDefSquare = new FixtureDef(); fixDefSquare.shape = squareShape; fixDefSquare.density = 0.1f; fixDefSquare.restitution = 0.3f; squareBody.createFixture(fixDefSquare); } } } }
From source file:com.barconr.games.marblegame.Physics2D.java
License:Apache License
private Vector2 getAccel() { return new Vector2(Gdx.input.getAccelerometerY() * 7, Gdx.input.getAccelerometerX() * -7); }
From source file:com.belocraft.gameobjects.Bird.java
public Bird(float x, float y, int width, int height) { this.width = width; this.height = height; position = new Vector2(x, y); velocity = new Vector2(0, 0); acceleration = new Vector2(0, 460); boundingCircle = new Circle(); this.originalY = y; isAlive = true;/*from w w w . j a v a 2 s .c o m*/ }
From source file:com.belocraft.gameobjects.Scrollable.java
public Scrollable(float x, float y, int width, int height, float scrollSpeed) { position = new Vector2(x, y); velocity = new Vector2(scrollSpeed, 0); this.width = width; this.height = height; isScrolledLeft = false;/*from w w w . j ava 2 s . co m*/ }
From source file:com.binarytenshi.nopassing.core.CameraHandler.java
public static void resize(int width, int height) { float aspectRatio = (float) width / (float) height; float scale;//from w w w . j a v a 2 s.com Vector2 crop = new Vector2(0f, 0f); if (aspectRatio > ASPECT_RATIO) { scale = (float) height / (float) VIRTUAL_HEIGHT; crop.x = (width - VIRTUAL_WIDTH * scale) / 2f; } else if (aspectRatio < ASPECT_RATIO) { scale = (float) width / (float) VIRTUAL_WIDTH; crop.y = (height - VIRTUAL_HEIGHT * scale) / 2f; } else { scale = (float) width / (float) VIRTUAL_WIDTH; } float w = VIRTUAL_WIDTH * scale; float h = VIRTUAL_HEIGHT * scale; viewport = new Rectangle(crop.x, crop.y, w, h); }
From source file:com.binarytenshi.nopassing.core.environment.StreetHandler.java
public static void highlightAt(int x, int y) { highlight = new Vector2(x, y); }
From source file:com.binarytenshi.nopassing.core.InputHandler.java
private Vector2 getTileVector(int screenX, int screenY) { Rectangle viewport = CameraHandler.getViewport(); Vector3 touchVector = new Vector3(screenX, screenY, 0); CameraHandler.getCamera().unproject(touchVector, viewport.x, viewport.y, viewport.width, viewport.height); return new Vector2((int) (touchVector.x / TextureLib.tileSide), (int) (touchVector.y / TextureLib.tileSide)); }