libgdx:使用 spritebatch 绘制纹理时旋转纹理 [英] libgdx: Rotate a texture when drawing it with spritebatch

查看:21
本文介绍了libgdx:使用 spritebatch 绘制纹理时旋转纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im trying to rotate textures when I draw them. I figured it would make more sense to do this than to rotate the images 90 degrees in paint.net and save them in different files. I looked thought the api documentation for spritebatch drawing arguments but I just dont understand. There are a bunch of arguments such as srcX, srcY, originX and so on. Also i would like to know how to do the same for texture regions. Heres a link to the api documentation page:http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/SpriteBatch.html

谢谢!

推荐答案

再次来自文档,但为了方便使用而复制到这里,所以我可以更好地解释一下.

again from the documentation, but copied here for ease of use and so I can explain a little better.

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

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

these two values represent the location to draw your texture in screen space (game space). Pretty self explanatory.

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),旋转和缩放将围绕纹理的中心进行(这可能是您想要的任何正常"旋转)

these two values represent the location where rotations (and scaling) happen from with respect to the screen space. So for instance, if you give the value 0, 0 here, the rotation and scaling will happen around one of the corners of your texture (the bottom left I believe), whereas if you give the center (width/2, height/2), the rotation and scaling would happen around the center of your texture (this is probably what you want for any "normal" rotations)

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

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

the dimensions for drawing your texture on screen.

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 的值将扩大矩形.请注意,这是相对于您之前给出的原点,这意味着如果这不是中心,图像可能看起来会失真.

values representing the scale of your rectangle, where values between 0 and 1 will shrink the rectangle, and values greater than 1 will expand the rectangle. Note that this is with respect to the origin you gave earlier, which means that if this is not the center the image may look distorted.

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

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

the angle to rotate the image by. Again, this is around the origin given earlier, so the rotation may not appear "correct" if the origin is not the center of the image

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

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

these two values are the starting location of the actual region of the image file (.png, .jpg, whatever) that you wish to use, in pixels. Basically the start of your image.

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

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

similarly, these two values are the width and height of the actual region of the image file you are using, in pixels.

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

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

Finally, these two booleans are used to flip the image either horizontally or vertically.

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

Now you may notice that the similar method for drawing TextureRegions has no srcX, srcY, srcWidth, or srcHeight. This is because those are the 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:使用 spritebatch 绘制纹理时旋转纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆