android - 在游戏 LibGDX 中旋转对象

标签 android rotation libgdx 2d-games

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

这是对象构造后的样子:

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

看样子,问题出在根源上。当物体被拉伸(stretch)时,端件的旋转原点需要改变。但我不知道如何正确计算原点。

这是我现在使用的代码:

// 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

如有任何帮助,我们将不胜感激。感谢您阅读到这里。

最佳答案

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

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

希望对你有帮助

关于android - 在游戏 LibGDX 中旋转对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12939942/

相关文章:

android - 这是什么错误? + LibGDX + super 跳线

java - 使用 libgdx 写入 json 文件

java - 如何使用 RxJava 发出错误然后发出缓存数据?

java - Android - 即使应用程序关闭时也在后台计算时间

android: ConnectivityManager.CONNECTIVITY_ACTION,网络连接状态未改变时发送广播

iphone - 全屏 youtube 视频、旋转和状态栏 (iOS)

java - LibGDX 多相机

android - OSMdroid 无法在 android studio 中获取当前位置

javafx - 如何将偏航、俯仰和滚动增量(不是欧拉)应用于相对于节点旋转轴而不是场景旋转轴的节点?

c# - OpenCV - 如何检测和测量两帧之间的角度?