如何在代码中使用正弦定律
How to use law of sines in code
我有一个关于根据对角线计算速度 x 和 y 的问题。属于三角形。检查下图
在这种情况下,我知道每个角的所有角度,我知道 A 总是 600。
我开始搜索并发现了正弦定律,我想我应该使用它,但我不知道如何在 Java 中使用它。
非常感谢任何帮助。
这是我目前得到的
Vector2 pos = mBall.getPosition();
double angleA = Math.atan2(target.x - pos.x, target.y - pos.y) * 180.0d / Math.PI;
double angleB = 90.0f;
double angleC = 180f - (angleA + angleB);
double sideA = MAXIMUM_VELOCITY;
double lawofsines = sideA / Math.sin(Math.toRadians(angleA));
您不需要正弦定律。由于在法线坐标系中,X 和 Y 维度根据定义是垂直的,因此您可以使用:
X = A*Cos(a) where a is the angle and A is the hypotenuse
Y = A*Sin(a)
这来自 Right Triangle
的属性。
您使用的 atan2
有误。调用约定模仿弧切线 atan(dy/dx)
为
atan2(dy, dx)
但是从代码可以看出,不需要计算角度,单位指向目标的方向就足够了,
dx = target.x-pos.x
dy = target.y-pos.y
ds = Math.hypot(dx,dy)
vel.x = MAXIMUM_VELOCITY * (dx/ds)
vel.y = MAXIMUM_VELOCITY * (dy/ds)
我有一个关于根据对角线计算速度 x 和 y 的问题。属于三角形。检查下图
在这种情况下,我知道每个角的所有角度,我知道 A 总是 600。
我开始搜索并发现了正弦定律,我想我应该使用它,但我不知道如何在 Java 中使用它。
非常感谢任何帮助。
这是我目前得到的
Vector2 pos = mBall.getPosition();
double angleA = Math.atan2(target.x - pos.x, target.y - pos.y) * 180.0d / Math.PI;
double angleB = 90.0f;
double angleC = 180f - (angleA + angleB);
double sideA = MAXIMUM_VELOCITY;
double lawofsines = sideA / Math.sin(Math.toRadians(angleA));
您不需要正弦定律。由于在法线坐标系中,X 和 Y 维度根据定义是垂直的,因此您可以使用:
X = A*Cos(a) where a is the angle and A is the hypotenuse
Y = A*Sin(a)
这来自 Right Triangle
的属性。
您使用的 atan2
有误。调用约定模仿弧切线 atan(dy/dx)
为
atan2(dy, dx)
但是从代码可以看出,不需要计算角度,单位指向目标的方向就足够了,
dx = target.x-pos.x
dy = target.y-pos.y
ds = Math.hypot(dx,dy)
vel.x = MAXIMUM_VELOCITY * (dx/ds)
vel.y = MAXIMUM_VELOCITY * (dy/ds)