Search code examples
javalibgdx

Explanation on libgdx draw method


I am learning libgdx, I have passed around some of tutorials but I always face problems understanding the batch.draw() method

public void draw(Texture texture,
                 float x,
                 float y,
                 float originX,
                 float originY,
                 float width,
                 float height,
                 float scaleX,
                 float scaleY,
                 float rotation,
                 int srcX,
                 int srcY,
                 int srcWidth,
                 int srcHeight,
                 boolean flipX,
                 boolean flipY)

I read its documentation and still got confused of the following statement:-

The rectangle is offset by originX, originY relative to the origin.

WHAT DOES THIS MEAN?. Which origin are talking about here?

Also i did a simple sketch of what I visual understand the draw() method. Am I on the right path? Thanks.

Visual representation of how i understand the draw method


Solution

  • The rectangle is offset by originX, originY relative to the origin.

    Scaling and Rotation performed around originX, originY.

    batch.begin();
    batch.draw(texture,300,200,50,50,100,100,1,1,0,100,100,50,50,false,false);
    batch.end();
    

    And Output is

    enter image description here

    Now rotate by 90 degree :

    batch.draw(texture,300,200,50,50,100,100,1,1,90,100,100,50,50,false,false);
    

    so rectangle offseted around centre by 90 degree

    enter image description here

    originX and originY is in the center of Rectangle so no offset appears in x,y

    Let's put originX and originY at left bottom of rectangle and rotate by 90 degree

    batch.draw(texture,300,200,0,0,100,100,1,1,90,100,100,50,50,false,false);
    

    enter image description here