【问题标题】:Libgdx sprite rotation causing position offsetLibgdx精灵旋转导致位置偏移
【发布时间】:2014-09-14 21:32:12
【问题描述】:

您好,我正在使用 Libgdx 框架为 android 创建游戏。我试图在靠近屏幕顶部的 X 轴中间放置一个精灵。我可以做到这一点,但由于精灵需要旋转 90 度(以节省精灵表上的空间),它会导致某种偏移,我不明白为什么。我相信这是因为原点旋转,但我尝试将原点设置为精灵的中心,但它仍然无法按计划工作。请您帮我理解为什么旋转会导致偏移以及如何修复它,谢谢。

    regions [1] = new TextureRegion(menu, 395, 0, 115, 320);
    logo=new Sprite(regions[1]);// the sprite
    logo.setPosition(W/2-logo.getWidth()/2,H*0.95f);  //draw right in the middle of X and 95% up Y
    logo.rotate(90);                                  //rotate the sprite 90 degrees
    logo.setOrigin(logo.getWidth()/2,logo.getHeight()/2);  //rotate around the centre of the sprite

    logo.draw(spriteBatch); //in render method draw the sprite

【问题讨论】:

  • 如果我改变 logo.setPosition(W/2-logo.getWidth()/2,H*0.95f); to logo.setPosition(W/2-logo.getWidth()/2,0);由于某种原因,它将精灵定位在 y 轴的几乎一半而不是底部。

标签: java libgdx


【解决方案1】:

旋转后设置精灵的原点:

logo.rotate(90);                                  //rotate the sprite 90 degrees
logo.setOrigin(logo.getWidth()/2,logo.getHeight()/2);  //rotate around the centre of the sprite

你应该反过来做。先设置原点,再旋转:

logo.setOrigin(logo.getWidth()/2,logo.getHeight()/2); 
logo.rotate(90);     

【讨论】:

  • 谢谢。这会有所帮助,但由于某种原因,Y 轴上似乎仍然存在偏移。如果我将 Y 轴设置为 0,它会出现在 0 上方约 200 像素处。你知道为什么会这样吗?
  • 我将精灵定位在我想要的位置。我仍然不明白 Y 偏移量来自哪里,但至少它适用于所有具有此 logo.setPosition(W/2-logo.getWidth()/2,H*0.95f-logo.getHeight()* 的设备0.65f);
猜你喜欢
  • 2023-03-22
  • 1970-01-01
  • 2014-04-04
  • 2014-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-19
相关资源
最近更新 更多