gpt4 book ai didi

java - 使用 Canvas 和 Path 围绕一个点进行 2d 旋转

转载 作者:行者123 更新时间:2023-11-29 22:20:38 27 4
gpt4 key购买 nike

问题涉及 Android 路径形状。这是一个三角形,我将其用作指向屏幕 Canvas 上对象的箭头。这是一个 2d 游戏。屏幕中间的玩家,他周围和屏幕外的物体。

这些箭头应该绕着屏幕的中心旋转,并有一个半径,这样它们就可以绕着玩家旋转一圈。箭头指向玩家需要移动的对象。

我现在所拥有的有点管用,但箭头以荒谬的速度绕着圆圈飞驰。有趣的是,他们指向了正确的方向,但他们并没有停留在圆圈上的正确点上。 (如果箭头指向东北,箭头应该在圆圈的东北部,等等)

我敢肯定这是因为数学。我可能用错了atan2。或者 canvas.translate 错误。或者我根本不应该使用 atan2。帮助! :)

代码如下:

    // set the shape of our radar blips
oBlipPath.moveTo(0, -5);
oBlipPath.lineTo(5, 0);
oBlipPath.lineTo(0, 5);

// Paint all the enemies and radar blips!
for(int i=0; i<iNumEnemies; i++){
if (oEnemies[i].draw(canvas, (int)worldX, (int)worldY)){

//calculate the degree the object is from the center of the screen.
//(user is the player. this could be done easier using iWidth and iHeight probably)
//we use a world coordinate system. worldY and worldX are subtracted
fDegrees = (float)Math.toDegrees(Math.atan2((oEnemies[i].getEnemyCenterY()-worldY)-user.getShipCenterY(), (oEnemies[i].getEnemyCenterX()-worldX)-user.getShipCenterX()));

canvas.save();

//get to the center
canvas.translate((iWidth / 2) , (iHeight / 2) );

//move a little bit depending on direction (trying to make arrows appear around a circle)
canvas.translate((float)(20 * Math.cos(fDegrees)), (float)(20* Math.sin(fDegrees)));

//rotate canvas so arrows will rotate and point in the right direction
canvas.rotate(fDegrees);

//draw arrows
canvas.drawPath(oBlipPath, oBlipPaint);
canvas.restore();

}
}

最佳答案

仿射变换不是可交换的。它们通常以明显的last-specified-first-applied 顺序应用。作为替代方案,请考虑 rotate()围绕一个点旋转的变化。

关于java - 使用 Canvas 和 Path 围绕一个点进行 2d 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7327806/

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