【问题标题】:cos and sin not providing expected outputcos 和 sin 没有提供预期的输出
【发布时间】:2013-11-13 01:43:07
【问题描述】:

我正在编写一个函数,它将对象中包含的所有精灵旋转到它们从该对象中心的位置(如果我将精灵向右移动,当我旋转容器对象时,我想要sprite 以保持其相对于新旋转对象的位置)。

我的做法如下(我为对象中包含的每个精灵运行此代码)

        std::pair<float,float> childPos = child->get_pos(); // This returns a pair with the x and y values of the child sprite

        float radius   = std::sqrt( (xPos - childPos.first)*(xPos - childPos.first) + (yPos - childPos.second)*(yPos - childPos.second)); // xPos and yPos are the container's x and y position
        angle = atan2(yPos - childPos.second, xPos - childPos.first); 

        angle = angle + (cAngle /180*3.14); // cAngle is the container's angle


        float newX = radius * std::cos(angle);
        float newY = radius * std::sin(angle);


        child->set_pos(newX, newY); // this sets the sprite's x and y coordinates
        child->set_angle(drawables[i].second->angleOffset + cAngle); // this sets the spries rotation around it's local axis. it only affects the sprite's orientation, not position

我已经试验过了,角度和半径总是符合我自己的计算。 (当它与精灵相差 90 度时,我让它打印出程序给我的角度,它是 90。然后每次容器精灵旋转时它都会保持适当的调整)但是,cos 和 sin 函数给出看似随机的结果.所有包含的精灵都失控并最终出现在屏幕上的随机位置。 (这个函数在容器旋转的每一帧都运行,将子元素调整为精灵的当前角度。)

我是否遗漏了一些明显的东西?我不太确定我的代码有什么问题。你能提供的任何帮助都会很棒x.x

-edit,有人告诉我应该提供一个示例来确切说明问题所在,因此我让控制台输出其中一个子精灵的数据。

结果如下

> ----------------------
> --> Container X: 50
> --> Container Y: 200
> --> Container Angle (in raidans): 0
> --> Container Angle (in degrees): 0
> -
> --> Sprite X: 50
> --> Sprite Y: 180
> --> Sprite Radius from container's center: 20
> --> Sprite Angle from container's center (in radians): 1.5708(note, this will st ay the same, as I can't properly adjust it each time
> because cos and sin provide  weird output)
> --> Sprite Angle from container's center (in degrees): 90.0457(note, this will s tay the same, as I can't properly adjust it each time
> because cos and sin provid e weird output)
> --> Sprite's angle adjusted for the container's rotation (in radians): 1.5708
> --> Sprite's angle adjusted for the container's rotation (in degrees): 90.0457
> -
> --> result of using cos with the adjusted angle in radians: -8.74228e-007
> --> result of using sin with the adjusted angle in radians: 20
> ----------------------
> 
> ----------------------
> --> Container X: 50
> --> Container Y: 200
> --> Container Angle (in raidans): 14489.8
> --> Container Angle (in degrees): 252.766
> -
> --> Sprite X: 50
> --> Sprite Y: 180
> --> Sprite Radius from container's center: 20
> --> Sprite Angle from container's center (in radians): 1.5708(note, this will st ay the same, as I can't properly adjust it each time
> because cos and sin provide  weird output)
> --> Sprite Angle from container's center (in degrees): 90.0457(note, this will s tay the same, as I can't properly adjust it each time
> because cos and sin provid e weird output)
> --> Sprite's angle adjusted for the container's rotation (in radians): 5.98016
> --> Sprite's angle adjusted for the container's rotation (in degrees): 342.812
> -
> --> result of using cos with the adjusted angle in radians: 19.0887
> --> result of using sin with the adjusted angle in radians: -5.96822
> ----------------------
> 
> ----------------------
> --> Container X: 50
> --> Container Y: 200
> --> Container Angle (in raidans): 20505.2
> --> Container Angle (in degrees): 357.702
> -
> --> Sprite X: 50
> --> Sprite Y: 180
> --> Sprite Radius from container's center: 20
> --> Sprite Angle from container's center (in radians): 1.5708(note, this will st ay the same, as I can't properly adjust it each time
> because cos and sin provide  weird output)
> --> Sprite Angle from container's center (in degrees): 90.0457(note, this will s tay the same, as I can't properly adjust it each time
> because cos and sin provid e weird output)
> --> Sprite's angle adjusted for the container's rotation (in radians): 7.81071
> --> Sprite's angle adjusted for the container's rotation (in degrees): 447.748
> -
> --> result of using cos with the adjusted angle in radians: 0.865144
> --> result of using sin with the adjusted angle in radians: 19.9813
> ----------------------
> 
> ----------------------
> --> Container X: 50
> --> Container Y: 200
> --> Container Angle (in raidans): 20636.9
> --> Container Angle (in degrees): 360
> -
> --> Sprite X: 50
> --> Sprite Y: 180
> --> Sprite Radius from container's center: 20
> --> Sprite Angle from container's center (in radians): 1.5708(note, this will st ay the same, as I can't properly adjust it each time
> because cos and sin provide  weird output)
> --> Sprite Angle from container's center (in degrees): 90.0457(note, this will s tay the same, as I can't properly adjust it each time
> because cos and sin provid e weird output)
> --> Sprite's angle adjusted for the container's rotation (in radians): 7.8508
> --> Sprite's angle adjusted for the container's rotation (in degrees): 450.046
> -
> --> result of using cos with the adjusted angle in radians: 0.0637081
> --> result of using sin with the adjusted angle in radians: 19.9999
> ----------------------

不太清楚为什么它会提供这样奇怪的结果。

【问题讨论】:

  • 如果您说sincos 给出的结果不正确,那么提出SSCCE (sscce.org) 应该很简单,对吧?请将其添加到您的问题中。
  • 如果 cAngle = 10,并且你多次调用它,那么每次对象将再旋转 10 度。那是你要的吗? (我的猜测是你希望孩子只旋转容器旋转改变的量)
  • @NPE:别打扰! Nobody writes testcases any more.
  • @NPE,我添加了一个输出示例。这与我的示例一样独立,因为我使用的代码需要一个库才能运行,并且在当前状态下演示它需要大量与错误本身无关的额外代码。 (我已经测试了所有这些,以确保是这个功能给我带来了问题)
  • @Legacyblade:不,你可以做一个测试用例。

标签: c++ visual-c++ geometry


【解决方案1】:

我想你想要:

float newX = childPos.first + radius * std::cos(angle);
float newY = childPos.second + radius * std::sin(angle);

(假设容器角度是您希望孩子每次调用它时旋转的量,如上面的评论中所述)

【讨论】:

  • 应该可以。但是当我传递角度(以弧度为单位)时,cos 和 sin 会返回像“-8.74228e-007”这样的值
  • 是的,这是正确的(cos(90 deg) =~ 0, -8.74228e-007 = -.00000087)。
  • 这确实让我得到了正确的输出。不过,角度计算有些奇怪。我忘记添加容器的位置。因此,不是将其设置为精灵下方 20 像素,而是将其设置为屏幕顶部下方 20 像素 x.x。我确实发现我的角度计算显然很时髦。如果我使用它并且只是将角度设置为以弧度为单位的 cAngle,它会给出正确的结果(如果子精灵相对于容器精灵的角度为 0 度)。但是当我使用函数计算的角度时,它只是随机旋转。奇数。
【解决方案2】:

我可以看到两个可能导致问题的潜在问题:

  1. 如果cAngle 是整数类型,那么cAngle / 180 将是一个截断除法。您需要将其更改为 cAngle / 180.0
  2. 您应该使用M_PI 而不是3.14;它更精确。

【讨论】:

  • cAngle 是一个浮点数。虽然我在过去 XD 之前也遇到过这个错误。另外,我将开始使用 M_PI。我不知道有一个为 PI 提供的常量
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多