如何将对象从锚点移动到锚点? [英] How to move object from Anchor to Anchor?

查看:116
本文介绍了如何将对象从锚点移动到锚点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用例是:

    在屏幕上点击并保存点".作为开始锚点
  1. 第二次点击屏幕并保存点".作为末端锚点
  2. 按下将对象从锚点开始移动到末端的按钮
  1. tap on the screen and save the "point" as starting anchor
  2. tap on the screen second time and save the "point" as end anchor
  3. push the button that will move the object from starting to end anchor

我已经建立了自己的节点,该节点使用的是ObjectAnimator,类似于太阳系示例.我唯一的问题是我不知道如何确定评估者的起点和终点.我的第一个想法是从起始和结束锚点的姿势中提取x,y,z

I've built my own node that is using ObjectAnimator similar like in the solar system example. My only problem is that I do not know how to determine start and end point for the the evaluator. My first thought was to take the x,y,z from Pose of start and end anchor

Vector3 start = new Vector3(startAnchor.getPose().tx(), startAnchor.getPose().ty(), startAnchor.getPose().tz());
Vector3 end = new Vector3(endAnchor.getPose().tx(), endAnchor.getPose().ty(), endAnchor.getPose().tz());

...

movingAnimation.setObjectValues(startingPoint, endPoint);
movingAnimation.setPropertyName("localPosition");
movingAnimation.setEvaluator(new Vector3Evaluator());

但是当我这样做时,动画是在完全不同的地方完成的.

but when I do that animation is done from completely different places.

我还没有找到用于此类操作的内置工具的参考. 我正在使用Sceneform.

I haven't found any reference to built-in tools for such operation. I'm using Sceneform.

问题是:如何从锚A到锚B制作流畅的动画(一张简单的幻灯片就足够了?)

So the question is: How to make a fluent animation (a simple slide is enough) from anchor A to anchor B?

推荐答案

我在HelloSceneform示例中做到了这一点.我创建了第一个AnchorNode并将"andy"节点添加为子节点.在下一个点击中,我创建了endPosition AnchorNode并开始将动画移动到该位置.

I did this in the HelloSceneform sample. I created the first AnchorNode and added the "andy" node as a child. On the next tap, I created the endPosition AnchorNode and started the animation to move to that position.

要记住的事情是,如果您要使用具有不同父对象的对象的位置,则要使用 worldPosition 与localPosition.

The thing to remember is that if you are using the positions of objects with a different parent, you want to use worldPosition vs. localPosition.

  private void onPlaneTap(HitResult hitResult, Plane plane, MotionEvent motionEvent) {
      if (andyRenderable == null) {
        return;
      }
      // Create the Anchor.
      Anchor anchor = hitResult.createAnchor();

      // Create the starting position.
      if (startNode == null) {
        startNode = new AnchorNode(anchor);
        startNode.setParent(arFragment.getArSceneView().getScene());

        // Create the transformable andy and add it to the anchor.
        andy = new Node();
        andy.setParent(startNode);
        andy.setRenderable(andyRenderable);
      } else {
        // Create the end position and start the animation.
        endNode = new AnchorNode(anchor);
        endNode.setParent(arFragment.getArSceneView().getScene());
        startWalking();
      }
  }

  private void startWalking() {
    objectAnimation = new ObjectAnimator();
    objectAnimation.setAutoCancel(true);
    objectAnimation.setTarget(andy);

    // All the positions should be world positions
    // The first position is the start, and the second is the end.
    objectAnimation.setObjectValues(andy.getWorldPosition(), endNode.getWorldPosition());

    // Use setWorldPosition to position andy.
    objectAnimation.setPropertyName("worldPosition");

    // The Vector3Evaluator is used to evaluator 2 vector3 and return the next
    // vector3.  The default is to use lerp. 
    objectAnimation.setEvaluator(new Vector3Evaluator());
    // This makes the animation linear (smooth and uniform).
    objectAnimation.setInterpolator(new LinearInterpolator());
    // Duration in ms of the animation.
    objectAnimation.setDuration(500);
    objectAnimation.start();
  }

这篇关于如何将对象从锚点移动到锚点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆