libgdx: 使用spritebatch绘制时旋转纹理

14
1个回答

69

再次从文档中摘取,为了方便使用和更好的解释,已经复制到此处。

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

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

现在您可能会注意到,类似的绘制TextureRegions的方法没有srcX、srcY、srcWidth或srcHeight。这是因为这些值是您在从纹理创建纹理区域时提供的

基本上,这意味着该命令

//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的Mario应该将此放入libgdx的javadocs中。:P 我想点赞,但我没有足够的声望,但我会尽力获得一些声望来点赞这个。 - vedi0boy
1
这对我来说很清楚,但请注意xy是相对于originXoriginY的,而这两者又是相对于屏幕空间的。不确定为什么文档说明它们是相对于屏幕空间的。该函数实际上执行的是final float worldOriginX = x + originX; - Juan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接