三角有两种表达方式,一种是人类喜欢的角度 Degree,一种是数学喜欢的弧度 Redian。
它们之间的转换为
r
e
d
=
d
e
g
∗
(
π
/
180
)
=
d
e
g
∗
0.01745329
d
e
g
=
r
e
d
∗
(
180
/
π
)
=
r
e
d
∗
57.29578
red = deg * (\pi / 180) = deg * 0.01745329\\ deg = red * (180 / \pi) = red * 57.29578\\
red=deg∗(π/180)=deg∗0.01745329deg=red∗(180/π)=red∗57.29578
我们无需手写,一般数学库中包含了角度与弧度的相互转换
Mathf.Red2Deg: 0.01745329
Mathf.Deg2Red:57.29578
正弦 sin 和 余弦 cos,还有一些其他形式三角函数
sec
θ
=
1
cos
θ
csc
θ
=
1
sin
θ
tan
θ
=
sin
θ
cos
θ
cot
θ
=
1
tan
θ
=
cos
θ
sin
θ
\sec\theta = \frac{1}{\cos\theta}\qquad \csc\theta = \frac{1}{\sin\theta}\\ \tan\theta = \frac{\sin\theta}{\cos\theta}\qquad \cot\theta = \frac{1}{\tan\theta} = \frac{\cos\theta}{\sin\theta}
secθ=cosθ1cscθ=sinθ1tanθ=cosθsinθcotθ=tanθ1=sinθcosθ
三角恒等式
sin
2
θ
+
cos
2
θ
=
1
\sin^2\theta + \cos^2\theta = 1
sin2θ+cos2θ=1
一般的数学库还提供反三角函数,如
Asin,Acos,Atan,它们可以将其转换为角度
三角函数在单位圆形位置表示
x
=
cos
θ
y
=
sin
θ
sin
θ
2
+
cos
θ
2
=
1
x = \cos\theta\\ y = \sin\theta\\ \sin\theta^2+\cos\theta^2 = 1
x=cosθy=sinθsinθ2+cosθ2=1
在三角形的其他公式,如正弦定理,余弦定理
正弦定理
sin
A
a
=
sin
B
b
=
sin
C
c
\frac{\sin A}{a} = \frac{\sin B}{b} = \frac{\sin C}{c}
asinA=bsinB=csinC
余弦定理
a
2
=
b
2
+
c
2
−
2
b
c
cos
A
b
2
=
a
2
+
c
2
−
2
a
c
cos
B
c
2
=
a
2
+
b
2
−
2
a
b
cos
C
a^2 = b^2 + c^2 - 2bc\cos A\\ b^2 = a^2 + c^2 - 2ac\cos B\\ c^2 = a^2 + b^2 - 2ab\cos C\\
a2=b2+c2−2bccosAb2=a2+c2−2accosBc2=a2+b2−2abcosC
这里演示一个物体的随机旋转,确定一个围绕旋转的物体,然后生成多个物体围绕它旋转。
旋转中心
public class TriangleText : MonoBehaviour
{
[SerializeField] private int _count = 5;
[SerializeField] private float _radius = 2f;
[SerializeField] private GameObject _prefab;
void Start()
{
//获得弧度
float red = 2 * Mathf.PI / _count;
for (int i = 0; i < _count; i++)
{
//获得位置
Vector3 position = new Vector3(
Mathf.Cos(red * i) * _radius, transform.position.y, Mathf.Sin(red * i) * _radius);
//获得方向
Quaternion quaternion = Quaternion.FromToRotation(Vector3.up, (transform.position - position).normalized);
//创建
Instantiate(_prefab, position + transform.position, quaternion);
}
}
}
一个中心点

旋转物体
[SerializeField] private float _rotationSpeed = 30f;
[SerializeField] private Transform _point;
private float _distance;
private float _currentRed;
private Quaternion _rondomRoation;
private void Start()
{
_rondomRoation = Quaternion.AngleAxis(Random.Range(0f, 180f), Random.onUnitSphere);
_rondomRoation = _rondomRoation.normalized;
Debug.Log(_rondomRoation);
_distance = (_point.position - transform.position).magnitude;
_currentRed = Vector3.Angle(Vector3.right, transform.position) * Mathf.Deg2Rad;
if (Vector3.Cross(Vector3.right, transform.position).y < 0)
_currentRed += Mathf.PI;
StartCoroutine(Rotation());
}
private IEnumerator Rotation()
{
while (true)
{
_currentRed = (_currentRed + _rotationSpeed * Mathf.Deg2Rad * Time.deltaTime) % 360f;
float x1 = Mathf.Cos(_currentRed) * _distance;
float y1 = Mathf.Sin(_currentRed) * _distance;
Vector3 position = new Vector3(x1, 0, y1);
position = _rondomRoation * position + _point.position;
Quaternion quaternion =
Quaternion.FromToRotation(Vector3.up, (_point.position - transform.position).normalized);
transform.SetPositionAndRotation(position, quaternion);
yield return null;
}
// ReSharper disable once IteratorNeverReturns
}
物体

效果图
20220720_155057