【问题标题】:Rotating points around pivot in 3D space在 3D 空间中围绕枢轴旋转点
【发布时间】:2020-11-18 09:02:22
【问题描述】:

我正在尝试围绕 3D 空间中的枢轴旋转点,以便每个圆都面向前一个圆,以便在连接时它们形成管状。我正在努力寻找在 Unity 中移动点的正确方法。

这是它们在旋转之前的样子:

缺少的部分在RotatePointAroundPivot 函数中。我试过了

var newVector = Quaternion.Euler(velocity) * originalVector;
// this seems to be wrong as velocity is in Unity distance units, and it takes degrees, which I'm not sure how to calculate from a vector
// this doesn't work either, looks like the same reason, it takes degrees, which I don't know how to calculate
var newVector = Quaternion.AngleAxis(rotationDegrees, pivot) * originalVector;

我查看了this thread,但此解决方案假定您已经计算出正确的角度。我不知道如何把我的速度变成一个角度。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CircleDrawing : MonoBehaviour
{
    Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 velocity)
    {
        var originalVector = point - pivot;

        var newVector = ???; // what goes here to create the newVector? Need to take velocity and turn it into an angle to alter originalVector

        return newVector + pivot;
    }

    void DrawCircle(Vector3 center, Vector3 velocity)
    {
        Color color = Random.ColorHSV();

        var circleSteps = 10;
        var circleRadius = 5;

        for (int i = 0; i < circleSteps; i++)
        {
            var circleFraction = (float)i / circleSteps;
            var angleRadians = 2 * Mathf.PI * circleFraction;
            var x = circleRadius * Mathf.Cos(angleRadians);
            var y = circleRadius * Mathf.Sin(angleRadians);
            var coord = center + new Vector3(x, 0, y);

            var rotatedCoord = RotatePointAroundPivot(coord, center, velocity);

            GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            sphere.transform.position = rotatedCoord;
            sphere.gameObject.GetComponent<Renderer>().material.color = color;

        }
    }

    void Start()
    {
        var centers = new List<Vector3>();
        centers.Add(new Vector3(0, 0, 0));

        int numCircles = 3;
        for (int i = 1; i <= numCircles; i++)
        {
            var center = new Vector3(10, 10, 10) + centers[i - 1];
            centers.Add(center);

            var velocity = center - centers[i - 1];
            DrawCircle(center, velocity);
        }
    }
}

【问题讨论】:

  • velocity 中,您将传递一个表示旋转轴*角度的向量...例如,如果您想围绕全局 Z 轴旋转大约 90°,这将是 new Vector3 (0,0,90) 您的问题是您在var velocity = center - centers[i - 1]; 中计算出错误的角度和轴,这将在您的两个中心之间返回一个向量...这将是10,10,10,因为您的第一个中心有0,0,0,您的第二个中心有10,10,10 我猜是什么你真正想要的是垂直于那个向量的圆,对吧?
  • 是的,没想到那样,但是是的,圆应该垂直于圆 A 和 B 之间的向量
  • 我仍然不确定如何实现这一点 - 我不知道如何计算垂直于矢量的旋转
  • 我很想知道为什么有人反对这个问题?

标签: unity3d math


【解决方案1】:

听起来您从线程中获得了 RotateAroundPivot 方法,例如this one 使用角度作为输入。

这会让您的用例感到困惑。

你所拥有的是中心之间的偏移向量

var velocity = center - centers[i - 1];
        

因此,与其以某种方式从这里计算欧拉角,您可以/应该直接使用已经为您计算好的Quaternion.LookRotation

创建具有指定向前和向上方向的旋转。

Z 轴与向前对齐,X 轴与向前和向上的叉积对齐,Y 轴与 Z 和 X 之间的叉积对齐。

然后您可以在计算中使用这种旋转,例如

void RotateAroundPivot(Vector3 point, Vector3 pivot, Vector3 direction)
{
    var currentOffset = point - pivot;

    var newOffset = Quaternion.LookRotation(direction) * currentOffset;

    return pivot + newOffset;
}

注意:在智能手机上打字,但我希望思路清晰

现在不确定是这个还是你必须添加 90° ^^


经过进一步思考,实际上可能并不那么微不足道^^

我认为你需要的是

  1. 您的中心之间有偏移方向
  2. 获取当前偏移向量
  3. 获取与这两个垂直的向量 -> 即您要绕其旋转的轴
  4. 获取当前偏移量与中心方向之间的当前角度
  5. 获取当前角度与目标角度 90° 之间的差异
  6. 围绕垂直轴旋转这个不同的角度,你应该有你的位置。

大概是这样的

void RotateAroundPivot (Vector3 point, Vector3 pivot, Vector3 centerOffset)
{
    // 1.
    var currentOffset = point - pivot;

    // 2.
    var rotationAxis = Vector3.Cross(currentOffset, centerOffset);

    // 3.
    var currentAngle = Vector3.Angle(currentOffset, centerOffset);

    // 4.
    var angleDistance = 90 - currentAngle;

    // 5.
    var rotation = Quaternion.AngleAxis(angleDistance, rotationAxis);

    var newOffset = rotation * currentOffset;

    return pivot + newOffset;
}

仍然不确定,因为智能手机^^

【讨论】:

  • 啊,这听起来很理想。我明天去试试
猜你喜欢
  • 1970-01-01
  • 2015-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多