【发布时间】: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
> ----------------------
不太清楚为什么它会提供这样奇怪的结果。
【问题讨论】:
-
如果您说
sin和cos给出的结果不正确,那么提出SSCCE (sscce.org) 应该很简单,对吧?请将其添加到您的问题中。 -
如果 cAngle = 10,并且你多次调用它,那么每次对象将再旋转 10 度。那是你要的吗? (我的猜测是你希望孩子只旋转容器旋转改变的量)
-
@NPE:别打扰! Nobody writes testcases any more.
-
@NPE,我添加了一个输出示例。这与我的示例一样独立,因为我使用的代码需要一个库才能运行,并且在当前状态下演示它需要大量与错误本身无关的额外代码。 (我已经测试了所有这些,以确保是这个功能给我带来了问题)
-
@Legacyblade:不,你可以做一个测试用例。
标签: c++ visual-c++ geometry