【问题标题】:Libgdx's Matrix4#translate() doesn't work as expectedLibgdx 的 Matrix4#translate() 不能按预期工作
【发布时间】:2015-06-24 16:59:14
【问题描述】:

我正在尝试使用变换矩阵绘制NinePatch,以便可以缩放、旋转、移动等。所以我创建了一个继承自 LibGDX 的 NinePatch 类的类,它负责矩阵。

这就是我计算我的变换矩阵的方式(每次以下值之一发生变化时我都会更新它):

this.transform
    .idt()
    .translate(originX, originY, 0)
    .rotate(0, 0, 1, rotation)
    .scale(scale, scale, 1)
    .translate(-originX, -originY, 0)
;

以及我如何呈现我的自定义 NinePatch 类:

drawConfig.begin(Mode.BATCH);
this.oldTransform.set(drawConfig.getTransformMatrix());
drawConfig.setTransformMatrix(this.transform);
this.draw(drawConfig.getBatch(), this.x, this.y, this.width, this.height); // Libgdx's NinePatch#draw()
drawConfig.setTransformMatrix(this.oldTransform);

案例 1

这是我渲染 4 个九个补丁时得到的结果: Position = 0,0 / Origin = 0,0 / Scale = 0.002 / Rotation = 每个 9patch 不同

我得到了我所期望的。

案例 2

现在相同的 4 个九个补丁: 位置 = 0,0 / 原点 = 0.5,0.5 / 比例 = 相同 / 旋转 = 相同

您可以看到我的 9 个补丁不是在 0,0(它们的位置)而是在 0.5,0.5(它们的原点)绘制,就像我在计算变换矩阵时没有 .translate(-originX, -originY, 0) 一样。可以肯定的是,我评论了这条指令,我确实得到了相同的结果。那么为什么我的第二次翻译显然没有被考虑在内?

【问题讨论】:

    标签: java opengl matrix libgdx nine-patch


    【解决方案1】:

    问题可能是您的缩放比例。因为它还缩小了翻译,所以您的第二个翻译实际上翻译了(-originX*scale, -originY*scale, 0),因为 scale=0.002,看起来根本没有翻译。例如,对于 x 坐标,您计算:

    x_final = originX + scale * (-originX + x_initial)
    

    【讨论】:

    • 你是对的,只是我必须编写不同的代码。
    【解决方案2】:

    我必须更改计算我的变换矩阵的代码,以便在转换回 Guillaume G 时将比例考虑在内。除了我的代码与他的不同:

    this.transform
        .idt()
        .translate(originX, originY, 0)
        .rotate(0, 0, 1, rotation)
        .scale(scale, scale, 1)
        .translate(-originX / scale, -originY / scale, 0);
    ;
    

    【讨论】:

      猜你喜欢
      • 2014-05-18
      • 2023-03-11
      • 2015-06-26
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多