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

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

问题描述

我的用例是:

  1. 点击屏幕并保存点"作为起始锚
  2. 第二次点击屏幕并保存点".作为端锚
  3. 按下按钮将对象从开始锚点移动到结束锚点

我已经构建了自己的节点,该节点使用类似于太阳系示例中的 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天全站免登陆