List of usage examples for com.badlogic.gdx.math Polygon getBoundingRectangle
public Rectangle getBoundingRectangle()
From source file:Tabox2D.java
License:Open Source License
private Tabody generateRegularPoly(String name, String type, float x, float y, float rad) { // Scale proportions: x /= meterSize;/*from w ww . j a va2 s . com*/ y /= meterSize; rad /= meterSize; PolygonShape polygonShape; BodyDef defPoly = new BodyDef(); setType(defPoly, type); // Generate points: List<Vector2> pts = new ArrayList<Vector2>(); Vector2 p0 = new Vector2(0, rad); float conv = MathUtils.degreesToRadians; float angleInDeg = polyInfo.get(name + "_angle"); float cos = MathUtils.cos(conv * angleInDeg); float sin = MathUtils.sin(conv * angleInDeg); for (int i = 0; i < polyInfo.get(name); i++) { pts.add(new Vector2(p0.x, p0.y)); p0.set(p0.x, p0.y); float newX = p0.x * cos - p0.y * sin; float newY = p0.x * sin + p0.y * cos; p0.x = newX; p0.y = newY; } // Get bounding box: float[] rawPoints = new float[pts.size() * 2]; int pointIndex = 0; for (int i = 0; i < rawPoints.length - 1; i += 2) { rawPoints[i] = pts.get(pointIndex).x; rawPoints[i + 1] = pts.get(pointIndex).y; pointIndex++; } Polygon polyForBox = new Polygon(); polyForBox.setVertices(rawPoints); Rectangle boundingRect = polyForBox.getBoundingRectangle(); float boxX = boundingRect.x; float boxY = boundingRect.y; float boxW = boundingRect.getWidth(); float boxH = boundingRect.getHeight(); Vector2 aabbCenter = new Vector2(boxX + boxW / 2, boxY + boxH / 2); defPoly.position.set(x, y); Tabody regularPoly = new Tabody(); regularPoly.body = world.createBody(defPoly); //regularPoly.body.setFixedRotation(true); polygonShape = new PolygonShape(); //polygonShape.setAsBox(w / 2, h / 2); for (int i = 0; i < rawPoints.length - 1; i += 2) { rawPoints[i] -= aabbCenter.x; rawPoints[i + 1] -= aabbCenter.y; } //rawPoints[0] += 0.5; polygonShape.set(rawPoints); FixtureDef fixtureBox = new FixtureDef(); fixtureBox.shape = polygonShape; fixtureBox.density = 1; fixtureBox.friction = 1; fixtureBox.restitution = 0; //////////////////////////////////////// regularPoly.w = boxW * meterSize;//radius * 2 * meterSize; regularPoly.h = boxH * meterSize;//radius * 2 * meterSize; regularPoly.fixture = fixtureBox; regularPoly.bodyType = "poly"; //////////////////////////////////////// regularPoly.body.createFixture(fixtureBox); polygonShape.dispose(); tabodies.add(regularPoly); return regularPoly; }
From source file:Tabox2D.java
License:Open Source License
/** * Returns the bounding box of the given polygon * @param ptsCombined The points as Vector2 list * @return A Rectangle object/*from w ww . j av a 2 s . co m*/ */ private Rectangle boundingBoxOf(List<Vector2> ptsCombined) { float[] rawPtsCombined = new float[ptsCombined.size() * 2]; int ptsCombinedIndex = 0; for (int i = 0; i < rawPtsCombined.length - 1; i += 2) { rawPtsCombined[i] = ptsCombined.get(ptsCombinedIndex).x; rawPtsCombined[i + 1] = ptsCombined.get(ptsCombinedIndex).y; ptsCombinedIndex++; } Polygon polygon = new Polygon(); polygon.setVertices(rawPtsCombined); Rectangle boundingRect = polygon.getBoundingRectangle(); return boundingRect; }
From source file:Tabox2D.java
License:Open Source License
/** * Returns the bounding box of the given polygon * @param ptsCombined The points as a float[] object * @return A Rectangle object//from ww w . j a va 2 s.c om */ private Rectangle boundingBoxOf(float[] ptsCombined) { Polygon polygon = new Polygon(); polygon.setVertices(ptsCombined); Rectangle boundingRect = polygon.getBoundingRectangle(); return boundingRect; }
From source file:com.kotcrab.vis.runtime.entity.TextEntity.java
License:Apache License
private void calculateBoundingRectangle() { Polygon polygon = new Polygon(new float[] { 0, 0, textLayout.width, 0, textLayout.width, textLayout.height, 0, textLayout.height });// w ww . j av a2 s . com polygon.setPosition(x, y); polygon.setRotation(rotation); polygon.setScale(scaleX, scaleY); polygon.setOrigin(originX, originY); boundingRectangle = polygon.getBoundingRectangle(); }
From source file:es.eucm.ead.engine.assets.drawables.shapes.GdxBezierShape.java
License:Open Source License
protected Pixmap generatePixmap() { ArrayList<Float> shape = new ArrayList<Float>(); float x0, y0, x1, y1, x2, y2, x3, y3; EAdList<Integer> pointsList = descriptor.getPoints(); x0 = pointsList.get(0);/*from ww w . j a v a 2 s .c o m*/ y0 = pointsList.get(1); shape.add(x0); shape.add(y0); int pointIndex = 2; while (pointIndex < pointsList.size()) { int length = pointsList.get(pointIndex++); switch (length) { case 1: x1 = pointsList.get(pointIndex++); y1 = pointsList.get(pointIndex++); lineTo(x1, y1, shape); x0 = x1; y0 = y1; break; case 2: x1 = pointsList.get(pointIndex++); y1 = pointsList.get(pointIndex++); x2 = pointsList.get(pointIndex++); y2 = pointsList.get(pointIndex++); quadTo(x0, y0, x1, y1, x2, y2, shape); x0 = x2; y0 = y2; break; case 3: x1 = pointsList.get(pointIndex++); y1 = pointsList.get(pointIndex++); x2 = pointsList.get(pointIndex++); y2 = pointsList.get(pointIndex++); x3 = pointsList.get(pointIndex++); y3 = pointsList.get(pointIndex++); curveTo(x0, y0, x1, y1, x2, y2, x3, y3, shape); x0 = x3; y0 = y3; break; default: } } // TODO Probably this can be improved EAdPaint p = descriptor.getPaint(); if (p == null) { p = ColorFill.WHITE; } float f[] = new float[shape.size()]; for (int i = 0; i < shape.size(); i++) { f[i] = shape.get(i); } Polygon polygon = new Polygon(f); Rectangle rectangle = polygon.getBoundingRectangle(); int borderWidth = p.getBorderWidth(); int x = (int) rectangle.x; int y = (int) rectangle.y; int width = (int) (rectangle.x + rectangle.width); int height = (int) (rectangle.y + rectangle.height); Pixmap pixmap = new Pixmap(width + borderWidth * 2, height + borderWidth * 2, Pixmap.Format.RGBA8888); pixmapContains = new Pixmap(width + borderWidth * 2, height + borderWidth * 2, Pixmap.Format.RGBA8888); pixmapContains.setColor(0, 0, 0, 1); pixmap.setColor(0, 0, 0, 0); pixmap.fill(); if (p.getFill() instanceof ColorFill) { ColorFill c = (ColorFill) p.getFill(); pixmap.setColor(c.getRed() / 255.0f, c.getGreen() / 255.0f, c.getBlue() / 255.0f, c.getAlpha() / 255.0f); } else if (p.getFill() instanceof LinearGradientFill) { LinearGradientFill gradient = (LinearGradientFill) p.getFill(); usingGradient = true; this.initGradientParams(gradient.getColor1(), gradient.getX0(), gradient.getY0(), gradient.getColor2(), gradient.getX1(), gradient.getY1()); } else { pixmap.setColor(0, 0, 0, 1); } for (int i = x; i < width; i++) { for (int j = y; j < height; j++) { if (polygon.contains(i, j)) { if (usingGradient) { this.setColor(pixmap, borderWidth + i, borderWidth + j); } pixmap.drawPixel(borderWidth + i, borderWidth + j); pixmapContains.drawPixel(borderWidth + i, borderWidth + j); } } } usingGradient = false; if (p.getBorder() != null) { if (p.getBorder() instanceof ColorFill) { ColorFill c = (ColorFill) p.getBorder(); pixmap.setColor(c.getRed() / 255.0f, c.getGreen() / 255.0f, c.getBlue() / 255.0f, c.getAlpha() / 255.0f); } else if (p.getBorder() instanceof LinearGradientFill) { LinearGradientFill gradient = (LinearGradientFill) p.getBorder(); usingGradient = true; initGradientParams(gradient.getColor1(), gradient.getX0(), gradient.getY0(), gradient.getColor2(), gradient.getX1(), gradient.getY1()); } float previousX = 0; float previousY = 0; float currentX; float currentY; for (int k = 0; k < shape.size(); k += 2) { currentX = shape.get(k); currentY = shape.get(k + 1); if (k >= 2) { for (int i = 1; i <= borderWidth; i++) { if (usingGradient) { this.setColor(pixmap, (int) previousX + i, (int) previousY + i); } pixmap.drawLine((int) previousX + i, (int) previousY + i, (int) currentX + i, (int) currentY + i); pixmapContains.drawLine((int) previousX + i, (int) previousY + i, (int) currentX + i, (int) currentY + i); } } previousX = currentX; previousY = currentY; } for (int i = 1; i <= borderWidth; i++) { pixmap.drawLine((int) previousX + i, (int) previousY + i, (int) shape.get(0).intValue() + i, (int) shape.get(1).intValue() + i); pixmapContains.drawLine((int) previousX + i, (int) previousY + i, (int) shape.get(0).intValue() + i, (int) shape.get(1).intValue() + i); } } usingGradient = false; return pixmap; }
From source file:org.catrobat.catroid.content.Look.java
License:Open Source License
public Rectangle getHitbox() { float x = getXInUserInterfaceDimensionUnit() - getWidthInUserInterfaceDimensionUnit() / 2; float y = getYInUserInterfaceDimensionUnit() - getHeightInUserInterfaceDimensionUnit() / 2; float width = getWidthInUserInterfaceDimensionUnit(); float height = getHeightInUserInterfaceDimensionUnit(); float[] vertices; if (getRotation() == 0) { vertices = new float[] { x, y, x, y + height, x + width, y + height, x + width, y }; } else {/*w w w. j a va2s. c om*/ PointF center = new PointF(x + width / 2f, y + height / 2f); PointF upperLeft = rotatePointAroundPoint(center, new PointF(x, y), getRotation()); PointF upperRight = rotatePointAroundPoint(center, new PointF(x, y + height), getRotation()); PointF lowerRight = rotatePointAroundPoint(center, new PointF(x + width, y + height), getRotation()); PointF lowerLeft = rotatePointAroundPoint(center, new PointF(x + width, y), getRotation()); vertices = new float[] { upperLeft.x, upperLeft.y, upperRight.x, upperRight.y, lowerRight.x, lowerRight.y, lowerLeft.x, lowerLeft.y }; } Polygon p = new Polygon(vertices); return p.getBoundingRectangle(); }
From source file:org.catrobat.catroid.sensing.CollisionDetection.java
License:Open Source License
public static double collidesWithFinger(Look look) { /*The touching points are expanded to circles with Constants.COLLISION_WITH_FINGER_TOUCH_RADIUS to simulate the real touching area of the finger (which is not only a point, but a small area) To improve performance first check if the circle is contained in the bounding box of that polygon If that's the case, check if any line segment of the polygon intersects with the circle, to evaluate a collision. If there is no intersection, but the circle is contained in an uneven number of polygons it means that there still is a collision Example:/*from ww w. j ava 2 s . c o m*/ _______ | ___ x| point "o" is not colliding, while point "x" is colliding | | o | | | |___| | |_______| */ ArrayList<PointF> touchingPoints = TouchUtil.getCurrentTouchingPoints(); Vector2 start = new Vector2(); Vector2 end = new Vector2(); Vector2 center = new Vector2(); float touchRadius = Constants.COLLISION_WITH_FINGER_TOUCH_RADIUS; for (PointF point : touchingPoints) { center.set(point.x, point.y); int containedIn = 0; for (Polygon polygon : look.getCurrentCollisionPolygon()) { Rectangle boundingRectangle = polygon.getBoundingRectangle(); boundingRectangle.x -= touchRadius; boundingRectangle.y -= touchRadius; boundingRectangle.width += touchRadius * 2; boundingRectangle.height += touchRadius * 2; if (boundingRectangle.contains(point.x, point.y)) { float[] vertices = polygon.getTransformedVertices(); int f = 0; while (f < polygon.getVertices().length - 3) { start.x = vertices[f++]; start.y = vertices[f++]; end.x = vertices[f++]; end.y = vertices[f++]; if (Intersector.intersectSegmentCircle(start, end, center, touchRadius * touchRadius)) { return 1d; } } start.x = vertices[vertices.length - 2]; start.y = vertices[vertices.length - 1]; end.x = vertices[0]; end.y = vertices[1]; if (Intersector.intersectSegmentCircle(start, end, center, touchRadius * touchRadius)) { return 1d; } if (polygon.contains(point.x, point.y)) { containedIn++; } } } if (containedIn % 2 != 0) { return 1d; } } return 0d; }
From source file:org.catrobat.catroid.stage.StageListener.java
License:Open Source License
public void drawDebugCollisionPolygons() { boolean drawPolygons = true; boolean drawBoundingBoxes = false; boolean drawPolygonPoints = false; boolean drawTouchingAreas = true; Color colorPolygons = Color.MAGENTA; Color colorBoundingBoxes = Color.MAROON; Color colorPolygonPoints = Color.BLACK; Color colorTouchingAreas = Color.RED; int lineWidth = 5; Gdx.gl20.glLineWidth(lineWidth / camera.zoom); collisionPolygonDebugRenderer.setAutoShapeType(true); collisionPolygonDebugRenderer.begin(); for (Sprite sprite : sprites.subList(1, sprites.size())) { Polygon[] polygonsForSprite = sprite.look.getCurrentCollisionPolygon(); if (polygonsForSprite != null) { for (Polygon polygonToDraw : polygonsForSprite) { if (drawPolygons) { collisionPolygonDebugRenderer.setColor(colorPolygons); collisionPolygonDebugRenderer.polygon(polygonToDraw.getTransformedVertices()); }//from ww w . j a va2s . c om if (drawBoundingBoxes) { Rectangle r = polygonToDraw.getBoundingRectangle(); collisionPolygonDebugRenderer.setColor(colorBoundingBoxes); collisionPolygonDebugRenderer.rect(r.getX(), r.getY(), r.getWidth(), r.getHeight(), Color.CYAN, Color.CYAN, Color.CYAN, Color.CYAN); } if (drawPolygonPoints) { collisionPolygonDebugRenderer.setColor(colorPolygonPoints); float[] points = polygonToDraw.getTransformedVertices(); for (int i = 0; i < points.length; i += 2) { collisionPolygonDebugRenderer.circle(points[i], points[i + 1], 10); } } } if (drawTouchingAreas) { ArrayList<PointF> touchingPoints = TouchUtil.getCurrentTouchingPoints(); collisionPolygonDebugRenderer.setColor(colorTouchingAreas); for (PointF point : touchingPoints) { collisionPolygonDebugRenderer.circle(point.x, point.y, Constants.COLLISION_WITH_FINGER_TOUCH_RADIUS); } } } } collisionPolygonDebugRenderer.end(); }
From source file:pl.kotcrab.libgdx.util.Text.java
License:Apache License
private void calculateBoundingRectangle() { Polygon polygon = new Polygon(new float[] { 0, 0, textBounds.width, 0, textBounds.width, textBounds.height, 0, textBounds.height });//w ww . j a v a2s .c o m polygon.setPosition(gx, gy); polygon.setRotation(rotation); polygon.setScale(scaleX, scaleY); polygon.setOrigin(originX, originY); boundingRectangle = polygon.getBoundingRectangle(); }