/** Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up. * @param radians the angle in radians * @param reference center Vector2 */ public Vector2 rotateAroundRad (Vector2 reference, float radians) { return this.sub(reference).rotateRad(radians).add(reference); }
/** Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up. * @param degrees the angle in degrees * @param reference center Vector2 */ public Vector2 rotateAround (Vector2 reference, float degrees) { return this.sub(reference).rotate(degrees).add(reference); }
/** Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up. * @param degrees the angle in degrees * @param reference center Vector2 */ public Vector2 rotateAround (Vector2 reference, float degrees) { return this.sub(reference).rotate(degrees).add(reference); }
/** Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up. * @param radians the angle in radians * @param reference center Vector2 */ public Vector2 rotateAroundRad (Vector2 reference, float radians) { return this.sub(reference).rotateRad(radians).add(reference); }
private void stageToLocalAmount (Vector2 amount) { actor.stageToLocalCoordinates(amount); amount.sub(actor.stageToLocalCoordinates(tmpCoords2.set(0, 0))); } });
private void stageToLocalAmount (Vector2 amount) { actor.stageToLocalCoordinates(amount); amount.sub(actor.stageToLocalCoordinates(tmpCoords2.set(0, 0))); } });
/** Computes the barycentric coordinates v,w for the specified point in the triangle. * <p> * If barycentric.x >= 0 && barycentric.y >= 0 && barycentric.x + barycentric.y <= 1 then the point is inside the triangle. * <p> * If vertices a,b,c have values aa,bb,cc then to get an interpolated value at point p: * * <pre> * GeometryUtils.barycentric(p, a, b, c, barycentric); * float u = 1.f - barycentric.x - barycentric.y; * float x = u * aa.x + barycentric.x * bb.x + barycentric.y * cc.x; * float y = u * aa.y + barycentric.x * bb.y + barycentric.y * cc.y; * </pre> * * @return barycentricOut */ static public Vector2 toBarycoord (Vector2 p, Vector2 a, Vector2 b, Vector2 c, Vector2 barycentricOut) { Vector2 v0 = tmp1.set(b).sub(a); Vector2 v1 = tmp2.set(c).sub(a); Vector2 v2 = tmp3.set(p).sub(a); float d00 = v0.dot(v0); float d01 = v0.dot(v1); float d11 = v1.dot(v1); float d20 = v2.dot(v0); float d21 = v2.dot(v1); float denom = d00 * d11 - d01 * d01; barycentricOut.x = (d11 * d20 - d01 * d21) / denom; barycentricOut.y = (d00 * d21 - d01 * d20) / denom; return barycentricOut; }
/** Computes the barycentric coordinates v,w for the specified point in the triangle. * <p> * If barycentric.x >= 0 && barycentric.y >= 0 && barycentric.x + barycentric.y <= 1 then the point is inside the triangle. * <p> * If vertices a,b,c have values aa,bb,cc then to get an interpolated value at point p: * * <pre> * GeometryUtils.barycentric(p, a, b, c, barycentric); * float u = 1.f - barycentric.x - barycentric.y; * float x = u * aa.x + barycentric.x * bb.x + barycentric.y * cc.x; * float y = u * aa.y + barycentric.x * bb.y + barycentric.y * cc.y; * </pre> * * @return barycentricOut */ static public Vector2 toBarycoord (Vector2 p, Vector2 a, Vector2 b, Vector2 c, Vector2 barycentricOut) { Vector2 v0 = tmp1.set(b).sub(a); Vector2 v1 = tmp2.set(c).sub(a); Vector2 v2 = tmp3.set(p).sub(a); float d00 = v0.dot(v0); float d01 = v0.dot(v1); float d11 = v1.dot(v1); float d20 = v2.dot(v0); float d21 = v2.dot(v1); float denom = d00 * d11 - d01 * d01; barycentricOut.x = (d11 * d20 - d01 * d21) / denom; barycentricOut.y = (d00 * d21 - d01 * d20) / denom; return barycentricOut; }
/** @return the {@link #position} with the {@link #selectedInterpolation interpolation} applied */ Vector2 getPosition (float time) { position.set(targetPosition); position.sub(startPosition); position.scl(getInterpolation(selectedInterpolation).apply(time / duration)); position.add(startPosition); return position; }
/** Checks whether the line segment and the circle intersect and returns by how much and in what direction the line has to move * away from the circle to not intersect. * * @param start The line segment starting point * @param end The line segment end point * @param point The center of the circle * @param radius The radius of the circle * @param displacement The displacement vector set by the method having unit length * @return The displacement or Float.POSITIVE_INFINITY if no intersection is present */ public static float intersectSegmentCircleDisplace (Vector2 start, Vector2 end, Vector2 point, float radius, Vector2 displacement) { float u = (point.x - start.x) * (end.x - start.x) + (point.y - start.y) * (end.y - start.y); float d = start.dst(end); u /= d * d; if (u < 0 || u > 1) return Float.POSITIVE_INFINITY; tmp.set(end.x, end.y, 0).sub(start.x, start.y, 0); tmp2.set(start.x, start.y, 0).add(tmp.scl(u)); d = tmp2.dst(point.x, point.y, 0); if (d < radius) { displacement.set(point).sub(tmp2.x, tmp2.y).nor(); return d; } else return Float.POSITIVE_INFINITY; }
/** Checks whether the line segment and the circle intersect and returns by how much and in what direction the line has to move * away from the circle to not intersect. * * @param start The line segment starting point * @param end The line segment end point * @param point The center of the circle * @param radius The radius of the circle * @param displacement The displacement vector set by the method having unit length * @return The displacement or Float.POSITIVE_INFINITY if no intersection is present */ public static float intersectSegmentCircleDisplace (Vector2 start, Vector2 end, Vector2 point, float radius, Vector2 displacement) { float u = (point.x - start.x) * (end.x - start.x) + (point.y - start.y) * (end.y - start.y); float d = start.dst(end); u /= d * d; if (u < 0 || u > 1) return Float.POSITIVE_INFINITY; tmp.set(end.x, end.y, 0).sub(start.x, start.y, 0); tmp2.set(start.x, start.y, 0).add(tmp.scl(u)); d = tmp2.dst(point.x, point.y, 0); if (d < radius) { displacement.set(point).sub(tmp2.x, tmp2.y).nor(); return d; } else return Float.POSITIVE_INFINITY; }
public void solveFakeIK (Vector3 target) { float gravity = Gdx.graphics.getDeltaTime() * GRAVITY; endPoint.set(target); bones[0].position.set(endPoint); for (int i = 0; i < bones.length - 1; i++) { Bone bone = bones[i]; endPoint.set(bone.position); diff.set(endPoint.x, endPoint.y).sub(bones[i + 1].position.x, bones[i + 1].position.y); diff.add(0, gravity); diff.add(bones[i + 1].inertia.x, bones[i + 1].inertia.y); diff.nor().scl(bones[i + 1].len); float x = endPoint.x - diff.x; float y = endPoint.y - diff.y; float delta = Gdx.graphics.getDeltaTime(); bones[i + 1].inertia.add((bones[i + 1].position.x - x) * delta, (bones[i + 1].position.y - y) * delta, 0).scl(0.99f); bones[i + 1].position.set(x, y, 0); } } }
private void setContainerPosition (Actor actor, float x, float y) { this.targetActor = actor; Stage stage = actor.getStage(); if (stage == null) return; container.pack(); float offsetX = manager.offsetX, offsetY = manager.offsetY, dist = manager.edgeDistance; Vector2 point = actor.localToStageCoordinates(tmp.set(x + offsetX, y - offsetY - container.getHeight())); if (point.y < dist) point = actor.localToStageCoordinates(tmp.set(x + offsetX, y + offsetY)); if (point.x < dist) point.x = dist; if (point.x + container.getWidth() > stage.getWidth() - dist) point.x = stage.getWidth() - dist - container.getWidth(); if (point.y + container.getHeight() > stage.getHeight() - dist) point.y = stage.getHeight() - dist - container.getHeight(); container.setPosition(point.x, point.y); point = actor.localToStageCoordinates(tmp.set(actor.getWidth() / 2, actor.getHeight() / 2)); point.sub(container.getX(), container.getY()); container.setOrigin(point.x, point.y); }
private void setContainerPosition (Actor actor, float x, float y) { this.targetActor = actor; Stage stage = actor.getStage(); if (stage == null) return; container.pack(); float offsetX = manager.offsetX, offsetY = manager.offsetY, dist = manager.edgeDistance; Vector2 point = actor.localToStageCoordinates(tmp.set(x + offsetX, y - offsetY - container.getHeight())); if (point.y < dist) point = actor.localToStageCoordinates(tmp.set(x + offsetX, y + offsetY)); if (point.x < dist) point.x = dist; if (point.x + container.getWidth() > stage.getWidth() - dist) point.x = stage.getWidth() - dist - container.getWidth(); if (point.y + container.getHeight() > stage.getHeight() - dist) point.y = stage.getHeight() - dist - container.getHeight(); container.setPosition(point.x, point.y); point = actor.localToStageCoordinates(tmp.set(actor.getWidth() / 2, actor.getHeight() / 2)); point.sub(container.getX(), container.getY()); container.setOrigin(point.x, point.y); }
@Override public boolean touchDragged (int x, int y, int pointer) { if (pointer != 0) return false; delta.set(x, y).sub(last); if (mode == TransformMode.Rotate) { point.set(cam.position).sub(lookAt); if (tmpV.set(point).nor().dot(yAxis) < 0.9999f) { xAxis.set(cam.direction).crs(yAxis).nor(); rotMatrix.setToRotation(xAxis, delta.y / 5); point.mul(rotMatrix); } rotMatrix.setToRotation(yAxis, -delta.x / 5); point.mul(rotMatrix); cam.position.set(point.add(lookAt)); cam.lookAt(lookAt.x, lookAt.y, lookAt.z); } if (mode == TransformMode.Zoom) { cam.fieldOfView -= -delta.y / 10; } if (mode == TransformMode.Translate) { tCurr.set(x, y); translated = true; } cam.update(); last.set(x, y); return true; }
public void draw (Batch batch, float parentAlpha) { // Use Stage#toScreenCoordinates, which we know is correct. toScreenCoordinates.set(testX, testY).sub(getOriginX(), getOriginY()).scl(getScaleX(), getScaleY()).rotate(getRotation()) .add(getOriginX(), getOriginY()).add(getX(), getY()); getStage().toScreenCoordinates(toScreenCoordinates, batch.getTransformMatrix()); // Do the same as toScreenCoordinates via Actor#localToParentCoordinates. localToAscendantCoordinates(null, localToParentCoordinates.set(testX, testY)); getStage().stageToScreenCoordinates(localToParentCoordinates); // System.out.println(name + " " + toScreenCoordinates + " " + localToParentCoordinates); batch.setColor(getColor()); batch.draw(region, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation()); super.draw(batch, parentAlpha); } }
float camX = camera.position.x; float camY = camera.position.y; Vector2 camMin = new Vector2(camera.viewportWidth, camera.viewportHeight); camMin.scl(camera.zoom/2); //bring to center and scale by the zoom level Vector2 camMax = new Vector2(borderWidth, borderHeight); camMax.sub(camMin); //bring to center //keep camera within borders camX = Math.min(camMax.x, Math.max(camX, camMin.x)); camY = Math.min(camMax.y, Math.max(camY, camMin.y)); camera.position.set(camX, camY, camera.position.z);
/** Rotates the Vector2 by the given angle around reference vector, counter-clockwise assuming the y-axis points up. * @param degrees the angle in degrees * @param reference center Vector2 */ public Vector2 rotateAround (Vector2 reference, float degrees) { return this.sub(reference).rotate(degrees).add(reference); }
@Override public void startAnimation(String id, Tween.Type repeatType, int count, ActionCallback cb, Vector2 p0, Vector2 pf) { startAnimation(id, repeatType, count, null); if (currentAnimation != null) { Vector2 tmp = new Vector2(pf); float angle = tmp.sub(p0).angle() + 90; lookat(angle); } }
@Override public void moveTowards(float x, float y, float speed) { TMP_SOURCE_VECTOR.set(getX(), getY()); TMP_TARGET_VECTOR.set(x, y); Vector2 direction = TMP_TARGET_VECTOR.sub(TMP_SOURCE_VECTOR).nor(); float xComponent = speed * MathUtils.cosDeg(direction.angle()); float yComponent = speed * MathUtils.sinDeg(direction.angle()); TMP_SOURCE_VECTOR.add(xComponent, yComponent); set(TMP_SOURCE_VECTOR.x, TMP_SOURCE_VECTOR.y); }