Refine search
@Override public Vector2 cpy () { return new Vector2(this); }
/** 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); } } }
/** 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); }
/** Does not set the normal member! */ protected VertexInfo getVertexAt (final VertexInfo out, int x, int y) { final float dx = (float)x / (float)(width - 1); final float dy = (float)y / (float)(height - 1); final float a = data[y * width + x]; out.position.set(corner00).lerp(corner10, dx).lerp(tmpV1.set(corner01).lerp(corner11, dx), dy); out.position.add(tmpV1.set(magnitude).scl(a)); out.color.set(color00).lerp(color10, dx).lerp(tmpC.set(color01).lerp(color11, dx), dy); out.uv.set(dx, dy).scl(uvScale).add(uvOffset); return out; }
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); }
/** Applies a mouse moved event to the stage and returns true if an actor in the scene {@link Event#handle() handled} the * event. This event only occurs on the desktop. */ public boolean mouseMoved (int screenX, int screenY) { mouseScreenX = screenX; mouseScreenY = screenY; if (!isInsideViewport(screenX, screenY)) return false; screenToStageCoordinates(tempCoords.set(screenX, screenY)); InputEvent event = Pools.obtain(InputEvent.class); event.setStage(this); event.setType(Type.mouseMoved); event.setStageX(tempCoords.x); event.setStageY(tempCoords.y); Actor target = hit(tempCoords.x, tempCoords.y, true); if (target == null) target = root; target.fire(event); boolean handled = event.isHandled(); Pools.free(event); return handled; }
private Actor fireEnterAndExit (Actor overLast, int screenX, int screenY, int pointer) { screenToStageCoordinates(tempCoords.set(screenX, screenY)); Actor over = hit(tempCoords.x, tempCoords.y, true); if (over == overLast) return overLast; event.setStage(this); event.setStageX(tempCoords.x); event.setStageY(tempCoords.y); event.setPointer(pointer); event.setType(InputEvent.Type.exit); event.setRelatedActor(over); overLast.fire(event); Pools.free(event); event.setType(InputEvent.Type.enter); event.setRelatedActor(overLast); over.fire(event); Pools.free(event);
void calculatePositionAndValue (float x, float y, boolean isTouchUp) { float oldPositionX = knobPosition.x; float oldPositionY = knobPosition.y; float oldPercentX = knobPercent.x; float oldPercentY = knobPercent.y; float centerX = knobBounds.x; float centerY = knobBounds.y; knobPosition.set(centerX, centerY); knobPercent.set(0f, 0f); if (!isTouchUp) { if (!deadzoneBounds.contains(x, y)) { knobPercent.set((x - centerX) / knobBounds.radius, (y - centerY) / knobBounds.radius); float length = knobPercent.len(); if (length > 1) knobPercent.scl(1 / length); if (knobBounds.contains(x, y)) { knobPosition.set(x, y); } else { knobPosition.set(knobPercent).nor().scl(knobBounds.radius).add(knobBounds.x, knobBounds.y); } } } if (oldPercentX != knobPercent.x || oldPercentY != knobPercent.y) { ChangeEvent changeEvent = Pools.obtain(ChangeEvent.class); if (fire(changeEvent)) { knobPercent.set(oldPercentX, oldPercentY); knobPosition.set(oldPositionX, oldPositionY); } Pools.free(changeEvent); } }
/** @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; }
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); } }
/** Initialize the bodies, anchors, lengths, max lengths, and ratio using the world anchors. */ public void initialize (Body bodyA, Body bodyB, Vector2 groundAnchorA, Vector2 groundAnchorB, Vector2 anchorA, Vector2 anchorB, float ratio) { this.bodyA = bodyA; this.bodyB = bodyB; this.groundAnchorA.set(groundAnchorA); this.groundAnchorB.set(groundAnchorB); this.localAnchorA.set(bodyA.getLocalPoint(anchorA)); this.localAnchorB.set(bodyB.getLocalPoint(anchorB)); lengthA = anchorA.dst(groundAnchorA); lengthB = anchorB.dst(groundAnchorB); this.ratio = ratio; float C = lengthA + ratio * lengthB; }
public void draw (Batch batch, float parentAlpha) { Stage stage = getStage(); if (stage.getKeyboardFocus() == null) stage.setKeyboardFocus(this); keepWithinStage(); if (style.stageBackground != null) { stageToLocalCoordinates(tmpPosition.set(0, 0)); stageToLocalCoordinates(tmpSize.set(stage.getWidth(), stage.getHeight())); drawStageBackground(batch, parentAlpha, getX() + tmpPosition.x, getY() + tmpPosition.y, getX() + tmpSize.x, getY() + tmpSize.y); } super.draw(batch, parentAlpha); }
/** Sets actorCoords to this event's coordinates relative to the specified actor. * @param actorCoords Output for resulting coordinates. */ public Vector2 toCoordinates (Actor actor, Vector2 actorCoords) { actorCoords.set(stageX, stageY); actor.stageToLocalCoordinates(actorCoords); return actorCoords; }
/** Applies a touch moved event to the stage and returns true if an actor in the scene {@link Event#handle() handled} the * event. Only {@link InputListener listeners} that returned true for touchDown will receive this event. */ public boolean touchDragged (int screenX, int screenY, int pointer) { pointerScreenX[pointer] = screenX; pointerScreenY[pointer] = screenY; mouseScreenX = screenX; mouseScreenY = screenY; if (touchFocuses.size == 0) return false; screenToStageCoordinates(tempCoords.set(screenX, screenY)); InputEvent event = Pools.obtain(InputEvent.class); event.setType(Type.touchDragged); event.setStage(this); event.setStageX(tempCoords.x); event.setStageY(tempCoords.y); event.setPointer(pointer); SnapshotArray<TouchFocus> touchFocuses = this.touchFocuses; TouchFocus[] focuses = touchFocuses.begin(); for (int i = 0, n = touchFocuses.size; i < n; i++) { TouchFocus focus = focuses[i]; if (focus.pointer != pointer) continue; if (!touchFocuses.contains(focus, true)) continue; // Touch focus already gone. event.setTarget(focus.target); event.setListenerActor(focus.listenerActor); if (focus.listener.handle(event)) event.handle(); } touchFocuses.end(); boolean handled = event.isHandled(); Pools.free(event); return handled; }
ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); shape.set(new Vector2(-40, 0), new Vector2(40, 0)); ground.createFixture(shape, 0); shape.dispose(); bd.position.set(-10, 10); bd.angle = 0.5f * (float)Math.PI; bd.allowSleep = false; Vector2 axis = new Vector2(2, 1); axis.nor(); pjd.initialize(ground, body, new Vector2(0, 0), axis);