List of usage examples for com.badlogic.gdx.math Rectangle set
public Rectangle set(float x, float y, float width, float height)
From source file:MyGdxGame.java
License:Apache License
private void updateKoala(float deltaTime) { if (deltaTime == 0) return;// w ww .ja v a 2 s. c om koala.stateTime += deltaTime; // check input and apply to velocity & state if ((Gdx.input.isKeyPressed(Keys.SPACE) || isTouched(0.5f, 1)) && koala.grounded) { koala.velocity.y += Koala.JUMP_VELOCITY; koala.state = Koala.State.Jumping; koala.grounded = false; } if (Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A) || isTouched(0, 0.25f)) { koala.velocity.x = -Koala.MAX_VELOCITY; if (koala.grounded) koala.state = Koala.State.Walking; koala.facesRight = false; } if (Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D) || isTouched(0.25f, 0.5f)) { koala.velocity.x = Koala.MAX_VELOCITY; if (koala.grounded) koala.state = Koala.State.Walking; koala.facesRight = true; } // apply gravity if we are falling koala.velocity.add(0, GRAVITY); // clamp the velocity to the maximum, x-axis only if (Math.abs(koala.velocity.x) > Koala.MAX_VELOCITY) { koala.velocity.x = Math.signum(koala.velocity.x) * Koala.MAX_VELOCITY; } // clamp the velocity to 0 if it's < 1, and set the state to standign if (Math.abs(koala.velocity.x) < 1) { koala.velocity.x = 0; if (koala.grounded) koala.state = Koala.State.Standing; } // multiply by delta time so we know how far we go // in this frame koala.velocity.scl(deltaTime); // perform collision detection & response, on each axis, separately // if the koala is moving right, check the tiles to the right of it's // right bounding box edge, otherwise check the ones to the left Rectangle koalaRect = rectPool.obtain(); koalaRect.set(koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT); int startX, startY, endX, endY; if (koala.velocity.x > 0) { startX = endX = (int) (koala.position.x + Koala.WIDTH + koala.velocity.x); } else { startX = endX = (int) (koala.position.x + koala.velocity.x); } startY = (int) (koala.position.y); endY = (int) (koala.position.y + Koala.HEIGHT); getTiles(startX, startY, endX, endY, tiles); koalaRect.x += koala.velocity.x; for (Rectangle tile : tiles) { if (koalaRect.overlaps(tile)) { koala.velocity.x = 0; break; } } koalaRect.x = koala.position.x; // if the koala is moving upwards, check the tiles to the top of it's // top bounding box edge, otherwise check the ones to the bottom if (koala.velocity.y > 0) { startY = endY = (int) (koala.position.y + Koala.HEIGHT + koala.velocity.y); } else { startY = endY = (int) (koala.position.y + koala.velocity.y); } startX = (int) (koala.position.x); endX = (int) (koala.position.x + Koala.WIDTH); getTiles(startX, startY, endX, endY, tiles); koalaRect.y += koala.velocity.y; for (Rectangle tile : tiles) { if (koalaRect.overlaps(tile)) { // we actually reset the koala y-position here // so it is just below/above the tile we collided with // this removes bouncing :) if (koala.velocity.y > 0) { koala.position.y = tile.y - Koala.HEIGHT; // we hit a block jumping upwards, let's destroy it! TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get("walls"); layer.setCell((int) tile.x, (int) tile.y, null); } else { koala.position.y = tile.y + tile.height; // if we hit the ground, mark us as grounded so we can jump koala.grounded = true; } koala.velocity.y = 0; break; } } rectPool.free(koalaRect); // unscale the velocity by the inverse delta time and set // the latest position koala.position.add(koala.velocity); koala.velocity.scl(1 / deltaTime); // Apply damping to the velocity on the x-axis so we don't // walk infinitely once a key was pressed koala.velocity.x *= Koala.DAMPING; }
From source file:MyGdxGame.java
License:Apache License
private void getTiles(int startX, int startY, int endX, int endY, Array<Rectangle> tiles) { TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get("walls"); rectPool.freeAll(tiles);// ww w. j a v a 2 s. c om tiles.clear(); for (int y = startY; y <= endY; y++) { for (int x = startX; x <= endX; x++) { Cell cell = layer.getCell(x, y); if (cell != null) { Rectangle rect = rectPool.obtain(); rect.set(x, y, 1, 1); tiles.add(rect); } } } }
From source file:com.badlogic.gdx.tests.superkoalio.SuperKoalio.java
License:Apache License
private void updateKoala(float deltaTime) { if (deltaTime == 0) return;/* w w w .j av a 2 s . co m*/ koala.stateTime += deltaTime; // check input and apply to velocity & state if ((Gdx.input.isKeyPressed(Keys.SPACE) || isTouched(0.75f, 1)) && koala.grounded) { koala.velocity.y += Koala.JUMP_VELOCITY; koala.state = Koala.State.Jumping; koala.grounded = false; } if (Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A) || isTouched(0, 0.25f)) { koala.velocity.x = -Koala.MAX_VELOCITY; if (koala.grounded) koala.state = Koala.State.Walking; koala.facesRight = false; } if (Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D) || isTouched(0.25f, 0.5f)) { koala.velocity.x = Koala.MAX_VELOCITY; if (koala.grounded) koala.state = Koala.State.Walking; koala.facesRight = true; } // apply gravity if we are falling koala.velocity.add(0, GRAVITY); // clamp the velocity to the maximum, x-axis only if (Math.abs(koala.velocity.x) > Koala.MAX_VELOCITY) { koala.velocity.x = Math.signum(koala.velocity.x) * Koala.MAX_VELOCITY; } // clamp the velocity to 0 if it's < 1, and set the state to standign if (Math.abs(koala.velocity.x) < 1) { koala.velocity.x = 0; if (koala.grounded) koala.state = Koala.State.Standing; } // multiply by delta time so we know how far we go // in this frame koala.velocity.scl(deltaTime); // perform collision detection & response, on each axis, separately // if the koala is moving right, check the tiles to the right of it's // right bounding box edge, otherwise check the ones to the left Rectangle koalaRect = rectPool.obtain(); koalaRect.set(koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT); int startX, startY, endX, endY; if (koala.velocity.x > 0) { startX = endX = (int) (koala.position.x + Koala.WIDTH + koala.velocity.x); } else { startX = endX = (int) (koala.position.x + koala.velocity.x); } startY = (int) (koala.position.y); endY = (int) (koala.position.y + Koala.HEIGHT); getTiles(startX, startY, endX, endY, tiles); koalaRect.x += koala.velocity.x; for (Rectangle tile : tiles) { if (koalaRect.overlaps(tile)) { koala.velocity.x = 0; break; } } koalaRect.x = koala.position.x; // if the koala is moving upwards, check the tiles to the top of it's // top bounding box edge, otherwise check the ones to the bottom if (koala.velocity.y > 0) { startY = endY = (int) (koala.position.y + Koala.HEIGHT + koala.velocity.y); } else { startY = endY = (int) (koala.position.y + koala.velocity.y); } startX = (int) (koala.position.x); endX = (int) (koala.position.x + Koala.WIDTH); getTiles(startX, startY, endX, endY, tiles); koalaRect.y += koala.velocity.y; for (Rectangle tile : tiles) { if (koalaRect.overlaps(tile)) { // we actually reset the koala y-position here // so it is just below/above the tile we collided with // this removes bouncing :) if (koala.velocity.y > 0) { koala.position.y = tile.y - Koala.HEIGHT; // we hit a block jumping upwards, let's destroy it! TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(1); layer.setCell((int) tile.x, (int) tile.y, null); } else { koala.position.y = tile.y + tile.height; // if we hit the ground, mark us as grounded so we can jump koala.grounded = true; } koala.velocity.y = 0; break; } } rectPool.free(koalaRect); // unscale the velocity by the inverse delta time and set // the latest position koala.position.add(koala.velocity); koala.velocity.scl(1 / deltaTime); // Apply damping to the velocity on the x-axis so we don't // walk infinitely once a key was pressed koala.velocity.x *= Koala.DAMPING; }
From source file:com.badlogic.gdx.tests.superkoalio.SuperKoalio.java
License:Apache License
private void getTiles(int startX, int startY, int endX, int endY, Array<Rectangle> tiles) { TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get(1); rectPool.freeAll(tiles);/*from ww w . ja v a 2s .c om*/ tiles.clear(); for (int y = startY; y <= endY; y++) { for (int x = startX; x <= endX; x++) { Cell cell = layer.getCell(x, y); if (cell != null) { Rectangle rect = rectPool.obtain(); rect.set(x, y, 1, 1); tiles.add(rect); } } } }
From source file:com.esotericsoftware.spine.superspineboy.Model.java
License:Open Source License
/** Returns rectangles for the tiles within the specified area. */ Array<Rectangle> getCollisionTiles(int startX, int startY, int endX, int endY) { Pools.freeAll(tiles, true);/*from ww w .j a v a2s . c om*/ tiles.clear(); for (int y = startY; y <= endY; y++) { for (int x = startX; x <= endX; x++) { Cell cell = collisionLayer.getCell(x, y); if (cell != null) { Rectangle rect = Pools.obtain(Rectangle.class); rect.set(x, y, 1, 1); tiles.add(rect); } } } return tiles; }
From source file:com.kotcrab.vis.editor.util.gdx.ParticleUtils.java
License:Apache License
public static Rectangle calculateBoundingRectangle(ParticleEffect effect, Rectangle bounds, boolean additive) { Rectangle tempBounds = new Rectangle(); if (additive == false) bounds.set(0, 0, 0, 0); for (int i = 0; i < effect.getEmitters().size; i++) { calculateBoundingRectangle(effect.getEmitters().get(i), tempBounds); if (isZero(tempBounds) == false) { if (isZero(bounds)) bounds.set(tempBounds);// ww w.j a v a2 s. co m else bounds.merge(tempBounds); } } return bounds; }
From source file:com.kotcrab.vis.editor.util.gdx.ParticleUtils.java
License:Apache License
/** * Uses reflection (yep) to calculate current bounding rectangle for emitter. Should be uses instead of emitter.getBoundingBox() because * it often returns BoundingBox with Infinity as values making it useless. *//*from w w w .j a v a 2s .c o m*/ public static Rectangle calculateBoundingRectangle(ParticleEmitter emitter, Rectangle bounds) { try { Field fieldParticles = emitter.getClass().getDeclaredField("particles"); fieldParticles.setAccessible(true); Field fieldActive = emitter.getClass().getDeclaredField("active"); fieldActive.setAccessible(true); Particle[] particles = (Particle[]) fieldParticles.get(emitter); boolean[] active = (boolean[]) fieldActive.get(emitter); if (particles.length == 0) return bounds.set(0, 0, 0, 0); boolean activeFound = false; int startIndex = 0; //find first active particle and set first bounding box for (; startIndex < particles.length; startIndex++) { if (active[startIndex]) { bounds.set(particles[startIndex].getBoundingRectangle()); activeFound = true; break; } } if (activeFound == false) return bounds.set(0, 0, 0, 0); //merge other active particles for (int i = startIndex; i < particles.length; i++) { if (active[i]) { Particle particle = particles[i]; bounds.merge(particle.getBoundingRectangle()); } } return bounds; } catch (NoSuchFieldException | IllegalAccessException e) { throw new IllegalStateException(e); } }
From source file:com.mygdx.game.DesertGame.java
License:Apache License
private void updateKoala(float deltaTime) { if (deltaTime == 0) return;/*w w w . j av a2 s . c o m*/ wanderer.stateTime += deltaTime; // check input and apply to velocity & state if (Gdx.input.isKeyPressed(Keys.Q)) { renderer.setMap(map[1]); } if (Gdx.input.isKeyPressed(Keys.UP) || Gdx.input.isKeyPressed(Keys.W)) { wanderer.facing = Wanderer.Facing.North; wanderer.velocity.y += Wanderer.MAX_VELOCITY / 2; } if (Gdx.input.isKeyPressed(Keys.DOWN) || Gdx.input.isKeyPressed(Keys.S)) { wanderer.facing = Wanderer.Facing.South; wanderer.velocity.y -= Wanderer.MAX_VELOCITY / 2; } if (Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A)) { wanderer.facing = Wanderer.Facing.West; wanderer.velocity.x = -Wanderer.MAX_VELOCITY; //if (koala.grounded) koala.state = Koala.State.Walking; //koala.facesRight = false; } if (Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D)) { wanderer.facing = Wanderer.Facing.East; wanderer.velocity.x = Wanderer.MAX_VELOCITY; //if (koala.grounded) koala.state = Koala.State.Walking; //koala.facesRight = true; } // clamp the velocity to the maximum if (Math.abs(wanderer.velocity.x) > Wanderer.MAX_VELOCITY) { wanderer.velocity.x = Math.signum(wanderer.velocity.x) * Wanderer.MAX_VELOCITY; } if (Math.abs(wanderer.velocity.y) > Wanderer.MAX_VELOCITY) { wanderer.velocity.y = Math.signum(wanderer.velocity.y) * Wanderer.MAX_VELOCITY; } // clamp the velocity to 0 if it's < 1, and set the state to standign if (Math.abs(wanderer.velocity.x) < 1) { wanderer.velocity.x = 0; if (wanderer.grounded) wanderer.state = Wanderer.State.Standing; } if (Math.abs(wanderer.velocity.y) < 1) { wanderer.velocity.y = 0; if (wanderer.grounded) wanderer.state = Wanderer.State.Standing; } // multiply by delta time so we know how far we go // in this frame wanderer.velocity.scl(deltaTime); // perform collision detection & response, on each axis, separately // if the koala is moving right, check the tiles to the right of it's // right bounding box edge, otherwise check the ones to the left Rectangle koalaRect = rectPool.obtain(); koalaRect.set(wanderer.position.x, wanderer.position.y, Wanderer.WIDTH, Wanderer.HEIGHT); int startX, startY, endX, endY; if (wanderer.velocity.x > 0) { startX = endX = (int) (wanderer.position.x + Wanderer.WIDTH + wanderer.velocity.x); } else { startX = endX = (int) (wanderer.position.x + wanderer.velocity.x); } startY = (int) (wanderer.position.y); endY = (int) (wanderer.position.y + Wanderer.HEIGHT); getTiles(startX, startY, endX, endY, tiles); koalaRect.x += wanderer.velocity.x; for (Rectangle tile : tiles) { if (koalaRect.overlaps(tile)) { wanderer.velocity.x = 0; break; } } koalaRect.x = wanderer.position.x; // if the koala is moving upwards, check the tiles to the top of it's // top bounding box edge, otherwise check the ones to the bottom if (wanderer.velocity.y > 0) { startY = endY = (int) (wanderer.position.y + Wanderer.HEIGHT + wanderer.velocity.y); } else { startY = endY = (int) (wanderer.position.y + wanderer.velocity.y); } startX = (int) (wanderer.position.x); endX = (int) (wanderer.position.x + Wanderer.WIDTH); getTiles(startX, startY, endX, endY, tiles); koalaRect.y += wanderer.velocity.y; for (Rectangle tile : tiles) { if (koalaRect.overlaps(tile)) { // we actually reset the koala y-position here // so it is just below/above the tile we collided with // this removes bouncing :) if (wanderer.velocity.y > 0) { wanderer.position.y = tile.y - Wanderer.HEIGHT; // we hit a block jumping upwards, let's destroy it! //TiledMapTileLayer layer = (TiledMapTileLayer)map.getLayers().get(1); //layer.setCell((int)tile.x, (int)tile.y, null); } else { wanderer.position.y = tile.y + tile.height; // if we hit the ground, mark us as grounded so we can jump wanderer.grounded = true; } wanderer.velocity.y = 0; break; } } rectPool.free(koalaRect); // unscale the velocity by the inverse delta time and set // the latest position wanderer.position.add(wanderer.velocity); wanderer.velocity.scl(1 / deltaTime); // Apply damping to the velocity on the x-axis so we don't // walk infinitely once a key was pressed wanderer.velocity.x *= Wanderer.DAMPING; wanderer.velocity.y *= Wanderer.DAMPING; }
From source file:com.mygdx.game.DesertGame.java
License:Apache License
private void getTiles(int startX, int startY, int endX, int endY, Array<Rectangle> tiles) { TiledMapTileLayer layer = (TiledMapTileLayer) map[0].getLayers().get(1); rectPool.freeAll(tiles);//from ww w . jav a2 s . c o m tiles.clear(); for (int y = startY; y <= endY; y++) { for (int x = startX; x <= endX; x++) { Cell cell = layer.getCell(x, y); if (cell != null) { Rectangle rect = rectPool.obtain(); rect.set(x, y, 1, 1); tiles.add(rect); } } } }
From source file:com.stercore.code.net.dermetfan.utils.libgdx.math.GeometryUtils.java
License:Apache License
/** @param aabb the rectangle to set as AABB of the given vertices * @param vertices the vertices */ public static Rectangle setToAABB(Rectangle aabb, float[] vertices) { return aabb.set(minX(vertices), minY(vertices), width(vertices), height(vertices)); }