简体   繁体   English

libgdx spritebatch不是基于纹理的Origin绘制的

[英]libgdx spritebatch not drawing based on a texture's Origin

I'm drawing a collection of sprites here, using a smaller (4x4) texture when zoomed out, and a larger (16x16) texture when zoomed in. When the zoom level reaches a threshold, the swap happens. 我在这里绘制一组精灵,在缩小时使用较小的(4x4)纹理,在放大时使用较大的(16x16)纹理。当缩放级别达到阈值时,交换发生。

However, for this to be a smooth transition, the sprites (located at [x,y]) need to be drawn with their origin being the centre of the sprite. 然而,为了使其成为平滑过渡,需要绘制精灵(位于[x,y]),其原点是精灵的中心。 Currently they are being drawn based on their lower left corner. 目前他们正在根据他们的左下角绘制。 So when the texture swap occurs, the sprite jumps slightly, as the origins don't match. 因此,当纹理交换发生时,精灵会略微跳跃,因为原点不匹配。

I've created some variables to debug and make sure that they are correct. 我已经创建了一些变量来调试并确保它们是正确的。

    TextureRegion sprite = null;

    boolean useHighResTexture = (_gameRef.cameraZoom < CAMERA_ZOOM_SWITCH_LO_RES);
    if (useHighResTexture) {
        //Zoomed in, Hi res, Use animation
        sprite = animation.getKeyFrame(animationStateTime, true);

    } else {
        //Zoomed out, lo-res, Use static image
        sprite = Assets.instance.getSmall();
    }


    float spriteWidth = sprite.getRegionWidth();
    float spriteHeight = sprite.getRegionHeight();
    float spriteOriginX = spriteWidth/2;
    float spriteOriginY = spriteHeight/2;

    //Draw(TEXTURE, x, y, originx, originy, 
    //      width, height, scaleX, scaleY, rotation, 
    //      srcX, srcY, 
    //      srcWidth, srcHeight, boolflipX, boolflipY

    //this.setCenter(spriteOriginX, spriteOriginY);

    batch.draw(sprite.getTexture(), this.x, this.y, spriteOriginX, spriteOriginY, 
            spriteWidth, spriteHeight, 1.0f, 1.0f, this.directionFaced, 
            sprite.getRegionX(), sprite.getRegionY(),
            sprite.getRegionWidth(), sprite.getRegionHeight(), false, false);

You can see that I'm using an animation on the zoomed in version (16x16) and a static image on the zoomed out version (4x4) using the AssetsManager. 您可以看到我使用AssetsManager在放大版本(16x16)上使用动画,在缩小版本(4x4)上使用静态图像。

The variables spriteWidth , spriteHeight are correct both at either 16 or 4 depending on whether zoomed in or out. spriteWidthspriteHeight变量在164都是正确的,具体取决于放大还是缩小。

The variables spriteOriginX and spriteOriginY are correct (I assume) both at either 8 and 2 depending whether zoomed in or out. 变量spriteOriginXspriteOriginY82都是正确的(我假设),具体取决于放大还是缩小。

I would have thought with the origins set as above it would draw the sprite based on that origin (as offsets from the lower left corner) 我会想到如上设置的原点,它将根据该原点绘制精灵(作为左下角的偏移)

Is my call in the batch.draw() correct? 我在batch.draw()中的调用是否正确?

Many thanks, J 非常感谢,J

No, it is not correct. 不,这不对。 The draw() always uses the left, lower corner as position. draw()总是使用左下角作为位置。 The origin is used for scaling and rotation only. 原点仅用于缩放和旋转。
So for you this means, that you have to subtract the origin from your position: 所以对你而言,这意味着你必须从你的位置中减去原点:

batch.draw(sprite.getTexture(), this.x - spriteOriginX, this.y - spriteOriginY,
    spriteOriginX, spriteOriginY, spriteWidth, spriteHeight, 1.0f, 1.0f,
    this.directionFaced, sprite.getRegionX(), sprite.getRegionY(),
    sprite.getRegionWidth(), sprite.getRegionHeight(), false, false
);

By doing this you just "push" the left lower corner (which is the position) to the left and down, by half width and half height, so that your position really defines the center of the sprite. 通过这样做,您只需将左下角(即位置)向左下方“推”半宽半高,以便您的位置真正定义精灵的中心。

Again: The origin is only used for rotation and scaling. 再次:原点仅用于旋转和缩放。 If you set a rotation in the draw() method it will be rotated arround the origin (in your case the middle). 如果在draw()方法中设置旋转,它将围绕原点旋转(在您的情况下为中间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM