You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

游戏开发中如何让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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 10:50:46