0

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

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

        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 函数给出看似随机的结果. 所有包含的精灵都失控并最终出现在屏幕上的随机位置。(这个函数在容器旋转的每一帧都运行,将孩子调整到精灵的当前角度。)

我错过了什么明显的东西吗?我不太确定我的代码有什么问题。你能提供的任何帮助都会很棒xx

-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
> ----------------------

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

4

2 回答 2

2

我想你想要:

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

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

于 2013-11-12T17:43:07.733 回答
1

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

  1. 如果cAngle是整数类型,cAngle / 180则将是截断除法。您将希望将其更改为cAngle / 180.0
  2. 您应该使用M_PI而不是3.14; 它更精确。
于 2013-11-12T17:11:41.450 回答