List of usage examples for com.badlogic.gdx.math Vector2 rotateRad
public Vector2 rotateRad(float radians)
From source file:es.eucm.ead.engine.utils.ShapeToCollider.java
License:Open Source License
/** * Builds a circumscribed polygon for a {@code circle} that can be used as a * collider./*w w w .ja va2 s. co m*/ * * The number of sides of the polygon is specified through param * {@code nSides}. * * The algorithm to calculate the minimum nSides polygon that covers a * circle (circumscribed) is as follows: * * (To see what a circumscribed polygon is, visit: <a href= * "http://www.vitutor.com/geometry/plane/circumscribed_polygons.html" * >http://www.vitutor.com/geometry/plane/circumscribed_polygons.html</a>) * * 1) Distance from circle's center to each vertex (equidistant) is * calculated (d). * * 2) A vector with length d is calculated. * * 3) Vector is added to the circle's center to get one vertex. * * 4) The vector is rotated "a" degrees. "a" is calculated by dividing 360 * by the number of sides of the polygon. * * (3) and (4) are repeated until all the vertices are calculated. * * Note: d=r/cos(a/2), where: r: circle's radius a: angle * * See <a href="https://github.com/e-ucm/ead/wiki/Shape-renderer">This wiki * page</a> for more details */ public static Polygon buildCircleCollider(Circle circle, int nSides) { Polygon polygon = new Polygon(); float vertices[] = new float[nSides * 2]; // The radius float r = circle.getRadius(); // The center of the circle. Since we work on a coordinate system where // the origin is in the bottom-left corner of the element, the center is // located in (radius, radius). float cx = r, cy = r; // How much we must rotate the radius vector (normal to the circle) float angleInc = 360.0F / nSides; double halfAngleIncRad = Math.PI / nSides; // Distance from center to each of the vertices float d = (float) (r / Math.cos(halfAngleIncRad)); // Initialization of the vector used to calculate each vertex. Vector2 centerToVertex = new Vector2(); centerToVertex.set(0, d); centerToVertex.rotateRad((float) halfAngleIncRad); for (int i = 0; i < nSides; i++) { // Calculate vertex vertices[2 * i] = cx + centerToVertex.x; vertices[2 * i + 1] = cy + centerToVertex.y; // Rotate for the next vertex centerToVertex.rotate(angleInc); } polygon.setVertices(vertices); return polygon; }
From source file:org.ams.core.Util.java
License:Open Source License
/** * create a polygon with the shape of a circle *//*from w ww. j a va 2 s . c o m*/ public static Array<Vector2> makeCircle(Vector2 origin, float radius) { Vector2 v2 = new Vector2(); Vector2 v3 = new Vector2(); float verticeCount = (int) (36 + radius * 8); v2.set(radius, 0); // calculate number of segments and length of each segment float radiansPerSegment = 0; float segmentLength = 0; while (segmentLength < 0.05F && verticeCount > 5) { verticeCount--; radiansPerSegment = (float) (2 * Math.PI / verticeCount); v3.x = radius; v3.y = 0; v3.rotateRad(radiansPerSegment); segmentLength = v2.dst(v3); } Array<Vector2> vertices = new Array<Vector2>(true, (int) verticeCount, Vector2.class); for (int i = 0; i < verticeCount; i++) { v2.x = radius; v2.y = 0; v2.rotateRad(i * radiansPerSegment); v2.x += origin.x; v2.y += origin.y; vertices.add(new Vector2(v2)); } return vertices; }
From source file:org.ams.paintandphysics.things.PPCircle.java
License:Open Source License
private void updateVertices() { verticesMustBeUpdated = false;/*from ww w. j a va 2s .c om*/ Array<Vector2> vertices = new Array<Vector2>(); float step = MathUtils.PI2 / (float) vertexCount; for (float i = 0; i < vertexCount; i++) { Vector2 v = new Vector2(radius, 0); v.rotateRad(step * i); vertices.add(v); } for (OutlinePolygon outlinePolygon : basic.outlinePolygons) { outlinePolygon.setVertices(vertices); } if (basic.texturePolygon != null) { basic.texturePolygon.setVertices(vertices); } if (basic.physicsThing != null) { ((Circle) basic.physicsThing).setRadius(radius); } }
From source file:org.ams.prettypaint.OutlinePolygon.java
License:Open Source License
@Override public Array<Vector2> getVerticesRotatedScaledAndTranslated(float rotation, float scale, float transX, float transY) { for (int i = 0; i < vertices.size; i++) { Vector2 w = verticesRotatedAndTranslated.items[i]; w.set(vertices.items[i]);/*ww w . j av a 2s.c o m*/ w.rotateRad(rotation); w.scl(scale); w.add(transX, transY); } return verticesRotatedAndTranslated; }
From source file:org.ams.prettypaint.TexturePolygon.java
License:Open Source License
/** * With texture angle you can rotate the texture without rotating the edges of the polygon. * * @param textureAngle the angle of the texture in radians. *//*from w w w . j a va2 s . co m*/ public void setTextureAngle(float textureAngle) { Vector2 v = new Vector2(); for (int i = 0; i < polygonRegions.size; i++) { PolygonRegion toReplace = polygonRegions.get(i); float[] vertices = toReplace.getVertices(); for (int j = 0; j < vertices.length;) { v.set(vertices[j], vertices[j + 1]); v.rotateRad(this.textureAngle - textureAngle); vertices[j] = v.x; vertices[j + 1] = v.y; j += 2; } PolygonRegion replacement = new PolygonRegion(toReplace.getRegion(), vertices, toReplace.getTriangles()); polygonRegions.set(i, replacement); } this.textureAngle = textureAngle; }
From source file:org.ams.prettypaint.TexturePolygon.java
License:Open Source License
/** Set the texture to be upright at the polygons current angle. */ public void setTextureUprightForCurrentAngle() { Vector2 v = new Vector2(); for (int i = 0; i < polygonRegions.size; i++) { PolygonRegion toReplace = polygonRegions.get(i); float[] vertices = toReplace.getVertices(); for (int j = 0; j < vertices.length;) { v.set(vertices[j], vertices[j + 1]); v.rotateRad(textureAngle + angleRad); vertices[j] = v.x;/*from w w w . j a v a 2s . co m*/ vertices[j + 1] = v.y; j += 2; } PolygonRegion replacement = new PolygonRegion(toReplace.getRegion(), vertices, toReplace.getTriangles()); polygonRegions.set(i, replacement); } textureAngle = -angleRad; }
From source file:org.ams.prettypaint.TexturePolygon.java
License:Open Source License
/** * This method lets you align the texture so that if two or more different {@link #TexturePolygon}'s are overlapping * the texture still looks seamless. They must have the same scale and textureScale for this to work. * * @param extraTranslationX Extra translation allows you to move the texture around within the polygon. * Use the same value for all the {@link #TexturePolygon}'s you wish to align. * @param extraTranslationY Extra translation allows you to move the texture around within the polygon. * Use the same value for all the {@link #TexturePolygon}'s you wish to align. * @return this for chaining.//from w ww .j av a 2 s .c o m */ public TexturePolygon alignTexture(float extraTranslationX, float extraTranslationY) { Vector2 v = new Vector2(position); v.rotateRad(-textureAngle - angleRad); v.add(extraTranslationX, extraTranslationY); setTextureTranslation(v); return this; }
From source file:org.ams.testapps.paintandphysics.cardhouse.Tips.java
License:Open Source License
/** * Instantly show a tip on the screen.//from ww w .j a v a 2 s . c o m * * @param key String for remembering whether to display this tips in the feature. * @param text The tips. * @param arrowTo If not null an arrow will point to this location. */ private void showTip(final String key, final String text, final ChangingVector arrowTo) { if (debug) debug("Showing tip with key " + key + "."); final Array<OutlinePolygon> arrow = new Array<OutlinePolygon>(); stage.getActors().removeValue(window, true); // update this tip on resize Tips.this.onResize = new Runnable() { @Override public void run() { arrows.removeAll(arrow, true); stage.getActors().removeValue(window, true); showTip(key, text, arrowTo); } }; // prepare text table Label label = new Label(text, skin); label.setColor(Color.BLACK); Table textTable = new Table(); textTable.add(label); // prepare button table TextButton hide = new TextButton("Hide", skin); hide.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { stage.getActors().removeValue(window, true); arrows.removeAll(arrow, true); showNextTip(); } }); TextButton ok = new TextButton("Got It", skin); ok.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { stage.getActors().removeValue(window, true); arrows.removeAll(arrow, true); preferences.putBoolean(key, true); preferences.flush(); showNextTip(); } }); Table buttonTable = new Table(); buttonTable.add(hide).pad(SceneUtil.getPreferredPadding(stage)); buttonTable.add(ok).pad(SceneUtil.getPreferredPadding(stage)); // put it all in table Table mainTable = new Table(); mainTable.add(textTable).row(); mainTable.add(buttonTable); // prepare arrow if (arrowTo != null) { float sw = stage.getWidth(); float sh = stage.getHeight(); // figure out where to start the arrow Vector2 arrowFrom = new Vector2(mainTable.getPrefWidth() * 0.5f, 0); Vector2 v = new Vector2(arrowTo.get()).sub(sw * 0.5f, sh * 0.5f); arrowFrom.rotateRad(v.angleRad()); arrowFrom.add(sw * 0.5f, sh * 0.5f); arrowFrom.scl(1, -1).add(0, sh); // convert to world coordinates Vector2 screenCoordinates = stage.stageToScreenCoordinates(arrowFrom); Vector2 from = CoordinateHelper.getWorldCoordinates(arrowCamera, screenCoordinates.x, screenCoordinates.y); // we know where to end the arrow, just convert to world coordinates screenCoordinates = stage.stageToScreenCoordinates(new Vector2(arrowTo.get()).scl(1, -1).add(0, sh)); Vector2 to = CoordinateHelper.getWorldCoordinates(arrowCamera, screenCoordinates.x, screenCoordinates.y); // make and save arrow arrow.addAll(makeArrow(from, to)); arrows.addAll(arrow); } window = new Window("", skin); window.setBackground(new TextureRegionDrawable(skin.getRegion("gray"))); window.add(mainTable).center(); window.setSize(mainTable.getPrefWidth(), mainTable.getPrefHeight()); // center on stage float x = stage.getWidth() * 0.5f; float y = stage.getHeight() * 0.5f; window.setPosition(x, y, Align.center); stage.addActor(window); }
From source file:org.ams.testapps.paintandphysics.cardhouse.TurnCircle.java
License:Open Source License
/** Create vertices forming the outline of a circle. */ private Array<Vector2> makeCircle(Vector2 center, float radius) { Array<Vector2> vertices = new Array<Vector2>(); int n = 100;/*from ww w. j av a 2 s . c o m*/ for (int i = 0; i < n; i++) { Vector2 vertex = new Vector2(radius, 0); vertex.rotateRad(i * MathUtils.PI2 / n); vertex.add(center); vertices.add(vertex); } return vertices; }
From source file:org.ams.testapps.prettypaint.CircleAndBackground.java
License:Open Source License
@Override public void create() { Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA); Gdx.input.setInputProcessor(this); float w = Gdx.graphics.getWidth() * 0.5f; float h = Gdx.graphics.getHeight() * 0.5f; camera = new OrthographicCamera(10, 10 * (h / w)); batch = new PrettyPolygonBatch(); // create the vertices used for the circle float radius = 2f; Vector2 v = new Vector2(radius, 0); float angle = MathUtils.PI2 / 300; for (float rad = 0; rad <= MathUtils.PI2; rad += angle) { v.rotateRad(angle); circleVertices.add(new Vector2(v)); }/*from w w w .java 2 s . c o m*/ // prepare circle outlinePolygon = new OutlinePolygon(); outlinePolygon.setColor(Color.BLACK); outlinePolygon.setClosedPolygon(false); // prepare shadow around circle shadowPolygon = new OutlinePolygon(); shadowPolygon.setColor(new Color(0, 0, 0, 0.4f)); shadowPolygon.setHalfWidth(outlinePolygon.getHalfWidth() * 3); shadowPolygon.setClosedPolygon(false); // prepare the vertices for background and frame float hw = camera.viewportWidth * 0.5f; float hh = camera.viewportHeight * 0.5f; Array<Vector2> cameraBounds = new Array<Vector2>(); cameraBounds.add(new Vector2(-hw, -hh)); cameraBounds.add(new Vector2(hw, -hh)); cameraBounds.add(new Vector2(hw, hh)); cameraBounds.add(new Vector2(-hw, hh)); // prepare background texture = new Texture("images/for packing/backgrounds-dark/escheresque_ste.png"); texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); background = new TexturePolygon(); background.setTextureRegion(new TextureRegion(texture)); background.setScale(0.96f); background.setVertices(cameraBounds); }