游戏开发中如何让PositionComponent绕固定中心点旋转?
实现PositionComponent绕固定中心点旋转
我尝试让PositionComponent绕游戏世界中的一个固定中心点旋转,但锚点均位于组件内部,这让我有些困惑。
编辑: 为给有类似需求的开发者提供参考,我已实现该功能,代码如下:
void update(double dt){ super.update(dt); double oldRadian = anchorComponent.angle; anchorComponent.angle += rotation * dt; anchorComponent.angle %= 2 * math.pi; double newRadian = anchorComponent.angle - oldRadian; rotatingComponent.angle = anchorComponent.angle; double x = rotatingComponent.position.x; double y = rotatingComponent.position.y; rotatingComponent.position.x = cos(newRadian) * (x - anchorComponent.position.x) - sin(newRadian) * (y - anchorComponent.position.y) + anchorComponent.position.x; rotatingComponent.position.y = sin(newRadian) * (x - anchorComponent.position.x) + cos(newRadian) * (y - anchorComponent.position.y) + anchorComponent.position.y; }
现在当rotation变量改变时,rotatingComponent会绕anchorComponent(二者均为PositionComponent)旋转。
内容的提问来源于stack exchange,提问作者enjoycurse
相关产品推荐
相关产品推荐

