@Override public void setView (OrthographicCamera camera) { batch.setProjectionMatrix(camera.combined); float width = camera.viewportWidth * camera.zoom; float height = camera.viewportHeight * camera.zoom; float w = width * Math.abs(camera.up.y) + height * Math.abs(camera.up.x); float h = height * Math.abs(camera.up.y) + width * Math.abs(camera.up.x); viewBounds.set(camera.position.x - w / 2, camera.position.y - h / 2, w, h); }
/** Creates a {@link Rectangle} object with the given X and Y coordinates along with a given width and height. * * @param x X coordinate * @param y Y coordinate * @param width Width of the {@link Rectangle} to be created. * @param height Height of the {@link Rectangle} to be created. */ public RectangleMapObject (float x, float y, float width, float height) { super(); rectangle = new Rectangle(x, y, width, height); }
/** @param point The coordinates vector * @return whether the point is contained in the rectangle */ public boolean contains (Vector2 point) { return contains(point.x, point.y); }
/** Fits this rectangle around another rectangle while maintaining aspect ratio. This scales and centers the rectangle to the * other rectangle (e.g. Having a camera translate and scale to show a given area) * @param rect the other rectangle to fit this rectangle around * @return this rectangle for chaining * @see Scaling */ public Rectangle fitOutside (Rectangle rect) { float ratio = getAspectRatio(); if (ratio > rect.getAspectRatio()) { // Wider than tall setSize(rect.height * ratio, rect.height); } else { // Taller than wide setSize(rect.width, rect.width / ratio); } setPosition((rect.x + rect.width / 2) - width / 2, (rect.y + rect.height / 2) - height / 2); return this; }
private boolean isCulled () { // we start by setting the stage coordinates to this // actors coordinates which are relative to its parent // Group. float stageX = getX(); float stageY = getY(); // now we go up the hierarchy and add all the parents' // coordinates to this actors coordinates. Note that // this assumes that neither this actor nor any of its // parents are rotated or scaled! Actor parent = this.getParent(); while (parent != null) { stageX += parent.getX(); stageY += parent.getY(); parent = parent.getParent(); } // now we check if the rectangle of this actor in screen // coordinates is in the rectangle spanned by the camera's // view. This assumes that the camera has no zoom and is // not rotated! actorRect.set(stageX, stageY, getWidth(), getHeight()); camRect.set(camera.position.x - camera.viewportWidth / 2.0f, camera.position.y - camera.viewportHeight / 2.0f, camera.viewportWidth, camera.viewportHeight); visible = camRect.overlaps(actorRect); return !visible; } }
public InteractiveTileObject(PlayScreen screen, MapObject object){ this.object = object; this.screen = screen; this.world = screen.getWorld(); this.map = screen.getMap(); this.bounds = ((RectangleMapObject) object).getRectangle(); BodyDef bdef = new BodyDef(); FixtureDef fdef = new FixtureDef(); PolygonShape shape = new PolygonShape(); bdef.type = BodyDef.BodyType.StaticBody; bdef.position.set((bounds.getX() + bounds.getWidth() / 2) / MarioBros.PPM, (bounds.getY() + bounds.getHeight() / 2) / MarioBros.PPM); body = world.createBody(bdef); shape.setAsBox(bounds.getWidth() / 2 / MarioBros.PPM, bounds.getHeight() / 2 / MarioBros.PPM); fdef.shape = shape; fixture = body.createFixture(fdef); }
public static boolean overlaps (Rectangle r1, Rectangle r2) { return r1.overlaps(r2); }
@Test public void testFromString () { assertEquals(new Rectangle(5f, -4.1f, 0.03f, -0.02f), new Rectangle().fromString("[5.0,-4.1,0.03,-0.02]")); } }
/** Moves this rectangle so that its center point is located at a given position * @param x the position's x * @param y the position's y * @return this for chaining */ public Rectangle setCenter (float x, float y) { setPosition(x - width / 2, y - height / 2); return this; }
koalaRect.set(koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT); int startX, startY, endX, endY; if (koala.velocity.x > 0) { koalaRect.x += koala.velocity.x; for (Rectangle tile : tiles) { if (koalaRect.overlaps(tile)) { koala.velocity.x = 0; break; koalaRect.y += koala.velocity.y; for (Rectangle tile : tiles) { if (koalaRect.overlaps(tile)) {
public static boolean overlaps (Rectangle r1, Rectangle r2) { return r1.overlaps(r2); }
/** Fits this rectangle around another rectangle while maintaining aspect ratio. This scales and centers the rectangle to the * other rectangle (e.g. Having a camera translate and scale to show a given area) * @param rect the other rectangle to fit this rectangle around * @return this rectangle for chaining * @see Scaling */ public Rectangle fitOutside (Rectangle rect) { float ratio = getAspectRatio(); if (ratio > rect.getAspectRatio()) { // Wider than tall setSize(rect.height * ratio, rect.height); } else { // Taller than wide setSize(rect.width, rect.width / ratio); } setPosition((rect.x + rect.width / 2) - width / 2, (rect.y + rect.height / 2) - height / 2); return this; }