【发布时间】: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 之间的向量
-
我仍然不确定如何实现这一点 - 我不知道如何计算垂直于矢量的旋转
-
我很想知道为什么有人反对这个问题?