cos和sin没有提供预期的输出

时间:2013-11-12 17:06:52

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

我正在编写一个函数,它将一个对象中包含的所有精灵从该对象的中心转移到它们的位置(如果我向右移动一个精灵,当我旋转容器对象时,我想要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
> ----------------------

不太确定为什么它会提供类似的奇怪结果。

2 个答案:

答案 0 :(得分:2)

我想你想要:

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

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

答案 1 :(得分:1)

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

  1. 如果cAngle是整数类型,则cAngle / 180将是截断分区。您需要将其更改为cAngle / 180.0
  2. 您应该使用M_PI代替3.14;它更精确。
相关问题