Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am currently moving my old slick2d project to libgdx. I have created a custom AnimationExt class that extends Animation to which I added a draw method. The Animation's frames are actually TextureRegion so I draw current frame TextureRegion. Here is the code, it's pretty simple.

    public class AnimationExt extends Animation {

    float stateTime=0;
    TextureRegion currentFrame=null;
    boolean isStopped=false;

    public void draw(SpriteBatch batch,float x, float y,int originX, int originY, float xscale, float yscale, boolean hMirror, boolean vMirror, float angle, boolean clockwise){
        if(isStopped==false){
            stateTime += Gdx.graphics.getDeltaTime();
            currentFrame=getKeyFrame(stateTime);

        }else{
            currentFrame=getKeyFrame(stateTime);
        }
        batch.draw(currentFrame, x-currentFrame.getRegionWidth()/2, y-currentFrame.getRegionHeight()/2,currentFrame.getRegionWidth(),currentFrame.getRegionHeight());
        }


    public AnimationExt(float frameDuration,
            Array<? extends TextureRegion> keyFrames, int playType) {
        super(frameDuration, keyFrames, playType);
    }
}

This code works well. BUT when I add parameters to the batch.draw method (originX, originY, scaleX, scaleY, angle, clockwise) like this:

batch.draw(currentFrame, x-currentFrame.getRegionWidth()/2, y-currentFrame.getRegionHeight()/2,originX,originY,currentFrame.getRegionWidth(),currentFrame.getRegionHeight(),xscale,yscale,angle,clockwise);

The drawing method goes crazy: it draws the texture rotated to -90 (whereas the angle I entered was 0) and it the width and height are inverted. To fix this I had to do change the code like that :

batch.draw(currentFrame, x+currentFrame.getRegionWidth()/2, y-currentFrame.getRegionHeight()/2,originX,originY,currentFrame.getRegionHeight(),currentFrame.getRegionWidth(),xscale,yscale,90+angle,clockwise);

But I don't want to keep this code as I'm pretty sure it's weird and I want to know why does the drawing method have such a weird behaviour when adding more parameters. By the way, I'm using the very last nightly builds of 17th June.

share|improve this question

1 Answer 1

What values are you using for originX, originY, scaleX, scaleY? maybe you are using wrong values for originX and originY. The origin is bottom left corner of the textureregion as stated in the docs. To set the origin to the center of the texture set

originX = currentFrame.getRegionWidth()/2; 
originY = currentFrame.getRegionHeight()/2;

Hope this helps.

There is also a thread with a question related to the draw function, maybe it will help you.

share|improve this answer
    
Ups, just saw this was posted a year ago... –  PhilDev Dec 5 at 23:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.