# How to draw portion of a sprite in libGDX

Let's say i have a sprite like this:

and i want to draw the lower portion of it, only the yellow color.. Initially the sprite has dimensions 10x10, i want it to be drawn 10x3

Do i have to redimension both the sprite and the texture region to mantain proportion between the two?

• It sounds like you're looking for masking. – Anko Apr 16 '15 at 20:53
• My intent is to draw the life bar of a character. I have the complete bar sprite and i want to draw the portion related to the "health" of the character. So, if i have 30% health, i want th sprite to be drawn 30% tall but if i use the sprite.setSize method, it stretches the texture. Can you explain how do i use masking? – Catta_ Apr 16 '15 at 21:05
• Sorry, I don't know the specifics in Libdgx well enough to explain them myself. I've written an answer explaining the general concept of clipping/masking though, which might help with conceptual understanding. – Anko Apr 16 '15 at 21:12
• A similar question to this was asked recently: gamedev.stackexchange.com/questions/98470/… – Kurley Apr 16 '15 at 21:13
• I found your answer on the other question very useful BUT i have the problem that the sprite was created as a textureregion of an imageAtlas so with frame.getTexture() i get all the atlas and not only the region realted to that sprite!! – Catta_ Apr 16 '15 at 21:24

The answer I gave in the other question doesn't quite do exactly what you want, but the following example might help. You still need to re-proportion both the heights to do what you want, but you also need to move the input data region down to compensate for the smaller window:

spriteBatch.draw(
sprite.getTexture(),
x,
y,
width / 2,
height / 2,
width,
height * 0.3f,                                        /* Scale output height */
sprite.getScaleX(),
sprite.getScaleY(),
0,
sprite.getRegionX(),
sprite.getRegionY() + sprite.getRegionWidth() * 0.7f, /* Move input data position */
sprite.getRegionWidth(),
(int)((float)sprite.getRegionWidth() * 0.3f),         /* Scale input height */
false,
false);