public Vector3 getPosition() { return new Vector3(position); }
final Vector3 difference = Vector3.subtract(firstPoint, secondPoint); Vector3 directionFromTopToBottom = difference.normalized(); Quaternion rotationFromAToB = Quaternion.lookRotation(directionFromTopToBottom, desiredUp); Quaternion.rotateVector(rotationFromAToB, Vector3.forward()).normalized(); Vector3 rightDirection = Quaternion.rotateVector(rotationFromAToB, Vector3.right()).normalized(); Vector3 upDirection = Quaternion.rotateVector(rotationFromAToB, Vector3.up()).normalized(); desiredUp.set(upDirection); final float halfHeight = difference.length() / 2; final Vector3 center = Vector3.add(firstPoint, secondPoint).scaled(.5f); Vector3.add( directionFromTopToBottom.scaled(-halfHeight), Vector3.add( rightDirection.scaled(radius * cosTheta), upDirection.scaled(radius * sinTheta))); Vector3 normal = Vector3.subtract(topPosition, directionFromTopToBottom.scaled(-halfHeight)).normalized(); topPosition = Vector3.add(topPosition, center); UvCoordinate uvCoordinate = new UvCoordinate(uStep * edgeIndex, 0); Vector3.add( directionFromTopToBottom.scaled(halfHeight), Vector3.add( rightDirection.scaled(radius * cosTheta), upDirection.scaled(radius * sinTheta))); normal = Vector3.subtract(bottomPosition, directionFromTopToBottom.scaled(halfHeight)) .normalized();
private static float calculateDeltaRotation( Vector3 currentPosition1, Vector3 currentPosition2, Vector3 previousPosition1, Vector3 previousPosition2) { Vector3 currentDirection = Vector3.subtract(currentPosition1, currentPosition2).normalized(); Vector3 previousDirection = Vector3.subtract(previousPosition1, previousPosition2).normalized(); float sign = Math.signum( previousDirection.x * currentDirection.y - previousDirection.y * currentDirection.x); return Vector3.angleBetweenVectors(currentDirection, previousDirection) * sign; } }
Vector3 firstToSecond = Vector3.subtract(startPosition1, startPosition2); Vector3 firstToSecondDirection = firstToSecond.normalized(); Vector3 deltaPosition1 = Vector3.subtract(newPosition1, previousPosition1); Vector3 deltaPosition2 = Vector3.subtract(newPosition2, previousPosition2); previousPosition1.set(newPosition1); previousPosition2.set(newPosition2); float dot1 = Vector3.dot(deltaPosition1.normalized(), firstToSecondDirection.negated()); float dot2 = Vector3.dot(deltaPosition2.normalized(), firstToSecondDirection); float dotThreshold = (float) Math.cos(Math.toRadians(SLOP_MOTION_DIRECTION_DEGREES)); if (!Vector3.equals(deltaPosition1, Vector3.zero()) && Math.abs(dot1) < dotThreshold) { return false; if (!Vector3.equals(deltaPosition2, Vector3.zero()) && Math.abs(dot2) < dotThreshold) { return false; float startGap = firstToSecond.length(); gap = Vector3.subtract(newPosition1, newPosition2).length(); float separation = Math.abs(gap - startGap); float slopPixels = gesturePointersUtility.inchesToPixels(SLOP_INCHES);
private static void updateEndPointUV(List<Vertex> vertices) { // Update UV coordinates of ending vertices for (int edgeIndex = 0; edgeIndex <= NUMBER_OF_SIDES; edgeIndex++) { int vertexIndex = vertices.size() - edgeIndex - 1; Vertex currentVertex = vertices.get(vertexIndex); currentVertex.setUvCoordinate( new UvCoordinate( currentVertex.getUvCoordinate().x, (Vector3.subtract( vertices.get(vertexIndex).getPosition(), vertices.get(vertexIndex - NUMBER_OF_SIDES - 1).getPosition()) .length() + vertices.get(vertexIndex - NUMBER_OF_SIDES - 1).getUvCoordinate().y))); } }
Vector3 deltaPosition1 = Vector3.subtract(newPosition1, previousPosition1); Vector3 deltaPosition2 = Vector3.subtract(newPosition2, previousPosition2); previousPosition1.set(newPosition1); previousPosition2.set(newPosition2); if (Vector3.equals(deltaPosition1, Vector3.zero()) || Vector3.equals(deltaPosition2, Vector3.zero())) { return false;
Vector3 localPosition = new Vector3(); Node cornerNode; localPosition.set(-0.5f * image.getExtentX(), 0.0f, -0.5f * image.getExtentZ()); cornerNode = new Node(); cornerNode.setParent(this); localPosition.set(0.5f * image.getExtentX(), 0.0f, -0.5f * image.getExtentZ()); cornerNode = new Node(); cornerNode.setParent(this); localPosition.set(0.5f * image.getExtentX(), 0.0f, 0.5f * image.getExtentZ()); cornerNode = new Node(); cornerNode.setParent(this); localPosition.set(-0.5f * image.getExtentX(), 0.0f, 0.5f * image.getExtentZ()); cornerNode = new Node(); cornerNode.setParent(this);
@Override protected boolean updateGesture(HitTestResult hitTestResult, MotionEvent motionEvent) { int actionId = motionEvent.getPointerId(motionEvent.getActionIndex()); int action = motionEvent.getActionMasked(); if (action == MotionEvent.ACTION_MOVE) { Vector3 newPosition = GesturePointersUtility.motionEventToPosition(motionEvent, pointerId); if (!Vector3.equals(newPosition, position)) { delta.set(Vector3.subtract(newPosition, position)); position.set(newPosition); debugLog("Updated: " + pointerId + " : " + position); return true; } } else if (actionId == pointerId && (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP)) { complete(); } else if (action == MotionEvent.ACTION_CANCEL) { cancel(); } return false; }
@Override public void onUpdate(FrameTime frameTime) { if (infoCard == null) { return; } // Typically, getScene() will never return null because onUpdate() is only called when the node // is in the scene. // However, if onUpdate is called explicitly or if the node is removed from the scene on a // different thread during onUpdate, then getScene may be null. if (getScene() == null) { return; } Vector3 cameraPosition = getScene().getCamera().getWorldPosition(); Vector3 cardPosition = infoCard.getWorldPosition(); Vector3 direction = Vector3.subtract(cameraPosition, cardPosition); Quaternion lookRotation = Quaternion.lookRotation(direction, Vector3.up()); infoCard.setWorldRotation(lookRotation); } }
public DragGesture( GesturePointersUtility gesturePointersUtility, HitTestResult hitTestResult, MotionEvent motionEvent) { super(gesturePointersUtility); pointerId = motionEvent.getPointerId(motionEvent.getActionIndex()); startPosition = GesturePointersUtility.motionEventToPosition(motionEvent, pointerId); position = new Vector3(startPosition); delta = Vector3.zero(); targetNode = hitTestResult.getNode(); debugLog("Created: " + pointerId); }
@Override public void onUpdate(FrameTime frameTime) { float speed = (degreesPerSecond * frameTime.getDeltaSeconds()); Quaternion deltaRot = Quaternion.axisAngle(Vector3.up(), speedMultiplier * speed); setLocalRotation(Quaternion.multiply(getLocalRotation(), deltaRot)); }
/** * When translating, the up direction of the node must match the up direction of the plane from * the hit result. However, we also need to make sure that the original forward direction of the * node is respected. */ private Quaternion calculateFinalDesiredLocalRotation(Quaternion desiredLocalRotation) { // Get a rotation just to the up direction. // Otherwise, the node will spin around as you rotate. Vector3 rotatedUp = Quaternion.rotateVector(desiredLocalRotation, Vector3.up()); desiredLocalRotation = Quaternion.rotationBetweenVectors(Vector3.up(), rotatedUp); // Adjust the rotation to make sure the node maintains the same forward direction. Quaternion forwardInLocal = Quaternion.rotationBetweenVectors(Vector3.forward(), initialForwardInLocal); desiredLocalRotation = Quaternion.multiply(desiredLocalRotation, forwardInLocal); return desiredLocalRotation.normalized(); }
private void updatePosition(FrameTime frameTime) { // Store in local variable for nullness static analysis. Vector3 desiredLocalPosition = this.desiredLocalPosition; if (desiredLocalPosition == null) { return; } Vector3 localPosition = getTransformableNode().getLocalPosition(); float lerpFactor = MathHelper.clamp(frameTime.getDeltaSeconds() * LERP_SPEED, 0, 1); localPosition = Vector3.lerp(localPosition, desiredLocalPosition, lerpFactor); float lengthDiff = Math.abs(Vector3.subtract(desiredLocalPosition, localPosition).length()); if (lengthDiff <= POSITION_LENGTH_THRESHOLD) { localPosition = desiredLocalPosition; this.desiredLocalPosition = null; } getTransformableNode().setLocalPosition(localPosition); }
Vector3 normal = Vector3.subtract(centerPoint, nextPoint).normalized(); Vertex center = Vertex.builder()
@Override protected void onStart(HitTestResult hitTestResult, MotionEvent motionEvent) { debugLog("Started: " + pointerId); position.set(GesturePointersUtility.motionEventToPosition(motionEvent, pointerId)); gesturePointersUtility.retainPointerId(pointerId); }
@Override protected boolean updateGesture(HitTestResult hitTestResult, MotionEvent motionEvent) { int actionId = motionEvent.getPointerId(motionEvent.getActionIndex()); int action = motionEvent.getActionMasked(); if (action == MotionEvent.ACTION_CANCEL) { cancel(); return false; } boolean touchEnded = action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP; if (touchEnded && (actionId == pointerId1 || actionId == pointerId2)) { complete(); return false; } if (action != MotionEvent.ACTION_MOVE) { return false; } Vector3 newPosition1 = GesturePointersUtility.motionEventToPosition(motionEvent, pointerId1); Vector3 newPosition2 = GesturePointersUtility.motionEventToPosition(motionEvent, pointerId2); float newGap = Vector3.subtract(newPosition1, newPosition2).length(); if (newGap == gap) { return false; } gapDelta = newGap - gap; gap = newGap; debugLog("Update: " + gapDelta); return true; }
ArrayList<Integer> triangleIndices = new ArrayList<>(); ArrayList<Quaternion> rotations = new ArrayList<>(); Vector3 desiredUp = Vector3.up();
@Override protected boolean updateGesture(HitTestResult hitTestResult, MotionEvent motionEvent) { int actionId = motionEvent.getPointerId(motionEvent.getActionIndex()); int action = motionEvent.getActionMasked(); if (action == MotionEvent.ACTION_CANCEL) { cancel(); return false; } boolean touchEnded = action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP; if (touchEnded && (actionId == pointerId1 || actionId == pointerId2)) { complete(); return false; } if (action != MotionEvent.ACTION_MOVE) { return false; } Vector3 newPosition1 = GesturePointersUtility.motionEventToPosition(motionEvent, pointerId1); Vector3 newPosition2 = GesturePointersUtility.motionEventToPosition(motionEvent, pointerId2); deltaRotationDegrees = calculateDeltaRotation(newPosition1, newPosition2, previousPosition1, previousPosition2); previousPosition1.set(newPosition1); previousPosition2.set(newPosition2); debugLog("Update: " + deltaRotationDegrees); return true; }