【问题标题】:libgdx: Rotate a texture when drawing it with spritebatchlibgdx:使用 spritebatch 绘制纹理时旋转纹理
【发布时间】:2014-09-05 01:36:33
【问题描述】:

我在绘制纹理时尝试旋转纹理。我认为这样做比在paint.net中将图像旋转90度并将它们保存在不同的文件中更有意义。我认为 spritebatch 绘图参数的 api 文档,但我只是不明白。有srcX、srcY、originX等一堆参数。我也想知道如何对纹理区域做同样的事情。这是api文档页面的链接:http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/SpriteBatch.html

谢谢!

【问题讨论】:

    标签: rotation libgdx textures


    【解决方案1】:

    再次from the documentation,但为了方便使用,我在这里复制了一些,所以我可以更好地解释一下。

    x - the x-coordinate in screen space
    y - the y-coordinate in screen space
    

    这两个值表示在屏幕空间(游戏空间)中绘制纹理的位置。非常不言自明。

    originX - the x-coordinate of the scaling and rotation origin relative to the screen space coordinates
    originY - the y-coordinate of the scaling and rotation origin relative to the screen space coordinates
    

    这两个值表示相对于屏幕空间发生旋转(和缩放)的位置。因此,例如,如果您在此处给出值 0, 0,则旋转和缩放将围绕纹理的一个角(我相信是左下角)发生,而如果您给出中心(宽度/2,高度/2 ),旋转和缩放将围绕纹理的中心进行(这可能是您想要的任何“正常”旋转)

    width - the width in pixels
    height - the height in pixels
    

    在屏幕上绘制纹理的尺寸。

    scaleX - the scale of the rectangle around originX/originY in x
    scaleY - the scale of the rectangle around originX/originY in y
    

    代表矩形比例的值,其中 0 和 1 之间的值将缩小矩形,大于 1 的值将扩大矩形。请注意,这是相对于您之前给出的原点,这意味着如果这不是中心,图像可能看起来会失真。

    rotation - the angle of counter clockwise rotation of the rectangle around originX/originY
    

    图像旋转的角度。同样,这是在前面给出的原点附近,所以如果原点不是图像的中心,旋转可能看起来不“正确”

    srcX - the x-coordinate in texel space
    srcY - the y-coordinate in texel space
    

    这两个值是您希望使用的图像文件(.png、.jpg 等)的实际区域的起始位置,以像素为单位。基本上是你的形象的开始。

    srcWidth - the source with in texels
    srcHeight - the source height in texels
    

    同样,这两个值是您正在使用的图像文件的实际区域的宽度和高度,以像素为单位。

    flipX - whether to flip the sprite horizontally
    flipY - whether to flip the sprite vertically
    

    最后,这两个布尔值用于水平或垂直翻转图像。

    现在您可能会注意到类似的method for drawing TextureRegions 没有 srcX、srcY、srcWidth 或 srcHeight。这是因为那些是values you give to a texture region when you create it from a texture

    基本上这意味着命令

    //with TextureRegions
    SpriteBatch.draw(textureRegion, x, y, originX, originY, width, height, scaleX, scaleY, rotation);
    

    等价于

    //with Textures from TextureRegions
    SpriteBatch.draw(textureRegion.getTexture(), x, y, originX, originY, width, height, scaleX, scaleY, rotation, textureRegion.getRegionX(), textureRegion.getRegionY(), textureRegion.getRegionWidth(), textureRegion.getRegionHeight(), false, false);
    

    【讨论】:

    • 非常感谢你,你真的一定花了很多时间,因为它太棒了!!!非常有帮助。我认为来自 libgdx 的马里奥应该把它放在 libgdx 的 javadocs 中。 :P 我会投票,但我没有足够的代表,但我会尝试得到一些只是为了投票。
    • 这让我很清楚,但请注意xyoriginXoriginY 相关,而originY 又与屏幕空间相关。不知道为什么文档说它们是相对于屏幕空间的。该函数实际上是final float worldOriginX = x + originX;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 2014-10-28
    • 2011-11-24
    • 1970-01-01
    • 2019-10-17
    • 1970-01-01
    相关资源
    最近更新 更多