flutter 围绕固定位置(x,y)坐标旋转PositionComponent

0yg35tkg  于 2022-11-25  发布在  Flutter
关注(0)|答案(1)|浏览(324)

我试图让一个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;
}

现在,当变量旋转发生变化时,rotationComponent将围绕anchorComponent(两个positionComponents)旋转。

mspsb9vt

mspsb9vt1#

您可以使用MoveAlongPathEffect使组件绕着一个点做圆周运动,然后您可以将其与lookAt方法结合起来使组件“向内”旋转,使其看起来像是绕着该点旋转。您可能还需要设置nativeAngle,这取决于您要旋转的PositionComponent的外观。

相关问题