0
\$\begingroup\$

I have code that changes texture in each frame depending on the player's state - walking, idle.

My problem is that the idle texture (from separate png) has a slightly different size, and due to that it appears not in line with rest of the animations, seemingly being in a slightly different position when returning to idle.

How can I position the frames based on the center of texture instead of the bottom left?

batch.draw(determineTexture(), position.x, position.y,
            currentFrame.getRegionWidth()/2f, currentFrame.getRegionHeight()/2f,
            currentFrame.getRegionWidth(), currentFrame.getRegionHeight(),1f,1f, rotation,false);

TextureRegion determineTexture(){
        if(playerState == State.idle)
            currentFrame = idleAnimation.getKeyFrame(stateTime, true);
        else if (playerState == State.walkingUP || playerState == State.walkingDOWN || playerState == State.walkingRIGHT || playerState == State.walkingLEFT )
            currentFrame = walkAnimation.getKeyFrame(stateTime, true);
        return currentFrame;
}

One png has a height of 57 - idle, while 6 columns and one row of walking frames have a height of 87 pixels.

\$\endgroup\$
7
  • \$\begingroup\$ Why don't you make them the same size? \$\endgroup\$
    – Vaillancourt
    Jul 25, 2019 at 23:08
  • \$\begingroup\$ See some past Q&A about a similar issue. \$\endgroup\$
    – DMGregory
    Jul 25, 2019 at 23:27
  • \$\begingroup\$ @AlexandreVaillancourt Tried it just resizing png with this ps2pdf.com/resize-png but now image is blurry and visibly bigger by ~ 25 % from other frames of animation. Its just that walking animation has arms spread wider and its bigger in resolution size thanks to that. \$\endgroup\$
    – new User
    Jul 26, 2019 at 8:03
  • \$\begingroup\$ @DMGregory a bit simillar at first glance thanks, but not really useful :(. Shame i really struggle right now because that free asset pack looks cool and i wonder if thats me doing something wrong or i should look for new asset pack because you cant have two different heights. \$\endgroup\$
    – new User
    Jul 26, 2019 at 8:07
  • \$\begingroup\$ What force in the universe prevents you from having two different heights? Just offset your draw position accordingly, using a defined hotpoint/pivot you've defined each frame. This is addition and subtraction, not the halting problem. \$\endgroup\$
    – DMGregory
    Jul 26, 2019 at 8:20

1 Answer 1

0
\$\begingroup\$

SOLVED ! I have modified the code as follows: instead of just position.x and position.y as arguments in SpriteBatch.draw() i modified those values as follows after some trial and error

position.x - currentFrame.getRegionWidth()/2f 
position.y - currentFrame.getRegionHeight()/2f

So batch draw call looks now like this:

 batch.draw(determineTexture(), position.x - currentFrame.getRegionWidth()/2f, position.y - currentFrame.getRegionHeight()/2f,
            currentFrame.getRegionWidth()/2f, currentFrame.getRegionHeight()/2f,
            currentFrame.getRegionWidth(), currentFrame.getRegionHeight(),1f,1f, rotation,false);

That makes it centered every time i think Maybe i should mark it as beginner question next time but it felt satysfying to solve after some hints from @DMGregory thanks

\$\endgroup\$
2
  • \$\begingroup\$ This answer would be better if it showed the code change in context and described how/why making this change solves your problem. \$\endgroup\$
    – DMGregory
    Jul 26, 2019 at 8:46
  • \$\begingroup\$ @DMGregory I am still figuring out why it works \$\endgroup\$
    – new User
    Jul 26, 2019 at 9:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .