【问题标题】:Determine new position given rotation and required distance [closed]确定给定旋转和所需距离的新位置[关闭]
【发布时间】:2017-05-12 18:05:05
【问题描述】:

我有一个对象,我从二维空间中获取位置和旋转。

我需要将对象的位置在 X 和 Y 之间以相同的方向推进 12 米,它的旋转仅提供更改 X 和 Y 位置的方法。

这是我尝试并失败的一个(糟糕)示例。

if (direction <= 45)
{
    float nx = Convert.ToSingle(direction * .13333333);
    float ny = 12 - nx;
} else if (direction <= 90)
{
    float ny = Convert.ToSingle((90 - direction) * .133333333);
    float nx = 12 - ny;
} else if (direction <= 135)
{
    float ny = Convert.ToSingle((135 - direction) * -.133333333);
    float nx = -12 - ny;
} else if (direction <= 180)
{
    float nx = Convert.ToSingle((180 - direction) * -.133333333);
    float ny = -12 - nx;
}

我是否甚至使用正确的公式或方法来获得所需的结果? 我有理由相信我需要 Cos 和 Tan,但对如何使用它们或何时使用它们有自己的想法。

【问题讨论】:

  • 我想你应该稍微了解一下(2D)向量数学的基础知识。 Google 在寻找这方面的资源方面确实是无敌的……
  • 你可以完全避免这些条件,如果你创建一个带有正弦和余弦的向量,那么距离就是要相乘的半径,比如x = distance * cos(angle)y = distance * sin(angle)。切线不应该是必需的。角度必须以弧度指定(整圆 = 2 * Pi)。

标签: c# direction


【解决方案1】:

你必须把向量想象成一个三角形,然后求解它。答案并不太复杂。 https://www.mathsisfun.com/algebra/trig-finding-side-right-triangle.html

    //convert degrees into radians for the .NET trig functions
    direction *= Math.PI / 180;

    float nx = (float)(12 * Math.Cos(direction));
    float ny = (float)(12 * Math.Sin(direction));

【讨论】:

    猜你喜欢
    • 2016-05-26
    • 2016-04-26
    • 1970-01-01
    • 2016-05-16
    • 2020-12-10
    • 2013-09-28
    • 2011-06-29
    • 1970-01-01
    相关资源
    最近更新 更多