将位置轴转换为时间的公式 [英] Formula to convert position axis into time
问题描述
在下面的代码下方,我有一个正以圆形移动(带有水平键输入)的立方体.
I have a cube which i am moving in circle shape (with horizontal key input) is below code suggested.
public class Oscillator : MonoBehaviour {
float timeCounter = 0;
float speed,width, height;
public float yPosition = 30;
// Use this for initialization
void Start () {
speed = 2; width = 10; height = 10;
}
// Update is called once per frame
void Update () {
timeCounter += Time.deltaTime * speed * Input.GetAxis("Horizontal");
float x = Mathf.Sin (timeCounter)* height;
float y = yPosition;
float z = Mathf.Cos (timeCounter) * width;
transform.position = new Vector3 (x, y, z);
}
}
现在我的物体正在以圆形运动,这很好.现在我想将我的物体运动转化为时间. 假设
Now my object is moving in circular shape which is fine. Now i want to translate my objects movement into time. Let suppose
如果我的对象x位置为1,那么它应该给我1.0的时间
if my object x position is 1 then it should give me time 1.0
如果是1.5,那应该给我1.5
if it is 1.5 then it should give me 1.5
它根据我对象的x位置(或可能贯穿z方向)增加或减少.
it increase or decrease according to x postion of my object (or possibly throught z).
我记录了对象的x位置,该位置从0到9.999,然后变为减小0,然后从-1到-9,然后变为减小0并到达其初始位置.这种圆周运动的x值对我来说很奇怪,我无法形成任何可以将我的x位置转换为时间的公式. 请问有人可以帮助我解决这个纯粹的数学和3D数学问题吗?
I logged my object's x position which is starting from 0 to 9.999 and then become decrease 0, then -1 to -9 then it become decrease 0 and reached to its initial position. This circular movement x values are strange for me, i am unable to form any formula that can convert my x position into time. Please can any one help me in this purely mathematics and 3d math problem?
推荐答案
您可以尝试以下操作:
获取圆的原点和圆的0之间的向量,例如时钟上的12.这个是恒定的.
Get the vector between origin of the circle and the 0 of your circle, like 12 on a clock. This one is constant.
然后,您在圆弧的原点和当前点之间有了矢量.
Then you have the vector between the origin of the circle and the current point.
尝试以下操作:
Vector3 from = new Vector3(0,0,1); / This is your 12
Vector3 to = GetCurrentVector();
float angle = Quaternion.FromToRotation(Vector3.up, to - from).eulerAngles.z;
Debug.Log(angle / 360f);
这篇关于将位置轴转换为时间的公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!