gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:38:37 24 4
gpt4 key购买 nike

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

我是如何做到的如下(我为对象中包含的每个 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

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

我是否遗漏了一些明显的东西?我不太确定我的代码有什么问题。如果你能提供任何帮助,那就太好了 x.x

-edit,我被告知我应该提供一个示例来准确说明问题所在,所以我让控制台输出其中一个子 Sprite 的数据。

结果如下

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

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

最佳答案

我想你想要:

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

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

关于c++ - cos 和 sin 没有提供预期的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19935449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com