Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libgdx: Rotate a texture when drawing it with spritebatch

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

Thank you!

like image 211
vedi0boy Avatar asked Jul 15 '14 00:07

vedi0boy


1 Answers

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

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

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

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);

is equivalent to

//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);
like image 98
clearlyspam23 Avatar answered Sep 21 '22 05:09

clearlyspam23