2012-10-17 80 views
1

我正在使用LibGDX为Android开发游戏。我必须在游戏中需要旋转的物体。这些物体是一块板子和一根管子。我遇到的问题是,管件由三件组成,即中心件和端件。管和板可以拉伸。因为它们可以被拉伸,所以端部件必须是独立的图形,以便它们不会被拉伸扭曲。我很难搞清楚如何正确地做到这一点。位置和旋转从Box2D主体中检索。在游戏中旋转物体LibGDX

这是对象的样子,一旦构成:

tube piece with end caps http://weaverhastings.com/tube.png

这是端件:

end cap for the tube http://weaverhastings.com/tube_endpiece.png

这是去中间的一块:

middle piece for the tube http://weaverhastings.com/tube_middle.png

从看,它看起来像是问题的根源。当物体被拉伸时,端部件旋转的起点需要改变。但我无法弄清楚如何正确计算原点。

这里是我使用的是现在的代码:

// Right border 
batch.draw(border,      // AtlasRegion for the border 
      position.x,     // The position as reported from box2d body 
      position.y,     
      25 + 150 * scale.x,   // 25 is 2 x the end piece width, 150 is the width of the middle piece, it is multiplied by the scale because it can be stretched. 
      height/2,     // This is just the height of the tube piece 
      border.getRegionWidth(), // the is the width of the border 
      height,      
      0.6f,      // The scale of the borders is fixed 
      0.8f,          
      rotation * MathUtils.radiansToDegrees); // the rotation as retrieved from box2d body 

// Left border 
batch.draw(border, 
      position.x, 
      position.y, 
      25 - 150 * scale.x, 
      height/2, 
      border.getRegionWidth(), 
      height, 
      0.6f, 
      0.8f, 
      rotation * MathUtils.radiansToDegrees); 

视频可以看出,管件旋转这里:http://youtu.be/RusL4Mnitds

任何帮助将不胜感激。感谢您阅读这篇文章。

+0

批量属于哪个类? –

+0

来自libGDX API的SpriteBatch。 – weaverx9x9

回答

3

在Libgdx中,旋转的起点是相对于对象的位置,例如,你告诉它被绘制的地方。

所以对于左边框,你需要说originX是中间棋子(middlePiece.width/2)或其他任何东西的宽度的一半,对于正确的棋子originX需要为负值,这个值为(-middlePiece.width/2)。当不旋转时,他们的位置当然需要位于中间部分的末端。他们每个人的OriginY是中心棋子高度的一半。 (middlePiece.height/2)

希望这会有帮助