1

我正在使用 LibGDX 为 Android 开发游戏。我必须在游戏中需要旋转的对象。对象是板和管。我遇到的问题是,管件由三件组成,中心件和末端件。管和板可以拉伸。因为它们可以拉伸,所以末端必须是单独的图形,这样它们就不会因拉伸而变形。我很难弄清楚如何正确地做到这一点。从 Box2D 主体中检索位置和旋转。

这是对象构建后的样子:

带端盖的管件 http://weaverhastings.com/tube.png

这是结尾部分:

管端盖 http://weaverhastings.com/tube_endpiece.png

这是中间的部分:

管子的中间部分 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

任何帮助将不胜感激。谢谢你读到这里。

4

1 回答 1

3

在 Libgdx 中,旋转的原点是相对于对象位置的,例如,您告诉它要绘制的位置。

因此,对于左边框,您需要说 originX 将是中心部分宽度的一半(middlePiece.width/2)或其他任何内容,而对于右部分,originX 将需要为负这个值(-middlePiece.width/2)。当不旋转时,它们的位置当然需要位于中心部件的末端。他们每个人的 OriginY 将是中心部分高​​度的一半。(middlePiece.height/2)

希望这可以帮助

于 2012-11-05T19:16:55.300 回答