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

查看:193
本文介绍了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.

现在您可能会注意到,类似的

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.

从本质上讲,这意味着该命令

Essentially what that means is that the command

//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天全站免登陆