如何做半圆周运动?

Dan*_*iel 2 c# algorithm math geometry unity-game-engine

假设我有一辆汽车在 P0 位置,我想把它移动到 P1 位置,就像这 4 个例子:

在此处输入图片说明

P0 和 P1 之间的直线距离为d,运动到达的垂直最大高度为d/3。我想模拟这个从 P0 到 P1 的顺时针半圆运动。

假设dir = P1 - P0(length d)perpd/3垂直于的向量 (长度) dir

假设t = 0是半圆运动的开始和t = 1结束,我如何测量汽车的角度和位置t = i

MBo*_*MBo 5

我们必须找到这个圆弧和圆心的角度。

首先找到半径。

R^2 = (d/2)^2 + (R-d/3)^2  //pythagorean
R = 13/24 * d
Run Code Online (Sandbox Code Playgroud)

现在角

half_angle = arcsin(12/13) ~ 67.4 degrees 
angle = 2 * half_angle ~ 135 degrees = 2.35 radians
Run Code Online (Sandbox Code Playgroud)

标准化 perp 向量

uperp = perp / len(perp)
Run Code Online (Sandbox Code Playgroud)

获取圆心

M = (P0 + P1)/2   //chord middle
C = M + uperp * 5/24 * d
Run Code Online (Sandbox Code Playgroud)

起始角度

A0 = atan2(P0.Y-C.Y, P0.X-C.X)
Run Code Online (Sandbox Code Playgroud)

最后坐标

Car.X = C.X + R * Cos(A0 + t * angle)
Car.Y = C.Y + R * Sin(A0 + t * angle)
Run Code Online (Sandbox Code Playgroud)

在 Unity 中,这看起来像:

Vector2 startPosition;
Vector2 endPosition;
Vector2 perp;
float t;

float d = (endPosition - startPosition).magnitude;

float radius = 13f/24f * d;
float angle = 2f * Mathf.Asin(12f/13f);

Vector2 uperp = perp.normalized;

Vector2 M = (startPosition+endPosition)*0.5f;
Vector2 C = M + uperp * 5f/24f * d;

float A0 = Mathf.Atan2(startPosition.y-C.y, startPosition.x-C.x);
float At = A0 + t * angle;

Vector2 newPos = C + radius * new Vector2(Mathf.Cos(At), Mathf.Sin(At));
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • @Rex5 [GeoGebra](https://www.geogebra.org)(出于某种奇怪的原因,我更喜欢旧的[第5版本](https://wiki.geogebra.org/en/Reference:GeoGebra_Installation)) (2认同)