【问题标题】:Achieve parallax effect in libGDX game在 libGDX 游戏中实现视差效果
【发布时间】:2014-05-15 08:19:30
【问题描述】:

我正在和一些朋友一起做一个游戏,在这个游戏中我们有一个很大的水平世界,OrthographicCamera 只显示了其中的 1/3。当玩家的水平位置发生变化时,此相机会移动,因此相机只会向左和向右移动。

游戏中显示的一些对象靠近玩家的视点,而另一些则远离(例如岛屿)。考虑到这一点,我们不能为元素设置固定位置而只移动相机。我们需要考虑元素的距离来实现视差效果。

这是一个简单的图像来更好地解释它: 左侧的视口显示了游戏的 3 个对象。绿色的在玩家附近,红色的椭圆在远处,黄色的在中间。在右侧的视口中,相机已向右移动,因此所有对象都消失在左侧。问题是绿色矩形的相对运动大于黄色的运动。同理,黄色物体的运动量大于红色物体的运动量。

我创建的所有资产都考虑到了它们的距离,但现在,我如何使用 libGDX 模拟这个透视图?有什么课可以做吗?如果我必须在每次迭代中设置元素位置,我该如何计算正确的位置?

【问题讨论】:

  • 我会为每一层使用不同的 OrthographicCamera。将每一层视为具有自己的坐标系比例。其中一层将是游戏物理发生的主要层。在每一帧上,在更新主图层的相机后,抓取它的位置并将其缩放版本应用为每个其他图层相机位置的位置(每个图层都应该有一个相机位置比例参数)。对于每一层,在添加该层的精灵之前,首先将其相机的组合矩阵应用于精灵批次的投影矩阵。
  • 这是一个例子。他们使用 OrthographicCamera 子类的单个实例来实现,但基本概念相同。

标签: java android libgdx


【解决方案1】:

请注意,下面的示例未经过测试,因为我只是在回忆我是如何做到的。这个想法很简单 - 为每个具有初始位置和速度的额外层创建层并移动它们。如果一个层超出边缘,则将另一个层(这就是我们创建额外层的原因)放在相反的边缘。

假设您有一个具有初始位置、大小和速度的视差对象-

public class Parallax extends DynamicGameObject {

    public float width, height; // Use setter/getter if you prefer

    public Parallax(float x, float y, float width, float height, float velocityX, float velocityY) {

        super(x, y, width, height);
        velocity.set(velocityX, velocityY);
        this.width = width;
        this.height = height;

    }

    public void update(float deltaTime) {
        position.add(velocity.x * deltaTime, velocity.y * deltaTime);
    }

    public void setPosition(float x, float y) {
        position.set(x, y);
    }
}

DynamicGameObject 取自SuperJumperdemo-

public class DynamicGameObject extends GameObject {

    public final Vector2 velocity;
    public final Vector2 accel;

    public DynamicGameObject(float x, float y, float width, float height) {
        super(x, y, width, height);
        velocity = new Vector2();
        accel = new Vector2();
    }
}

GameObject 也是如此-

public class GameObject {

    public final Vector2 position;
    public final Rectangle bounds;

    public GameObject(float x, float y, float width, float height) {
        this.position = new Vector2(x,y);
        this.bounds = new Rectangle(x - width/2f, y - height/2f, width, height);
    }
}

假设我们有两层 - 一层在前面,另一层在后面。我们每个都有一个纹理。每个纹理填满整个屏幕。我们为每一层创建两个实例,这样当一个纹理开始离开屏幕时,另一个出现在边缘以填补空白。如果您有较小的纹理,则需要先确定需要多少纹理才能填充屏幕,然后创建一个额外的图层以填充其间的空隙。

我们可以在世界创建过程中创建一系列视差层-

Array<Parallax> parallaxList = new Array<Parallax>(4);

我们可以像这样创建图层-

// Back
/* First parallax for back layer is at 0 x-axis. If you want to move the texture from right to left, the value of BACK_VELOCITY_X should be negative. You can experiment with velocity value for desire pace of movement. We do not want our layer to move on y-axis. Hence, it is set to 0. */

parallaxList.add(new Parallax(0, BACK_TEXTURE_HEIGHT, BACK_TEXTURE_WIDTH, BACK_TEXTURE_HEIGHT, BACK_VELOCITY_X, 0));

/* This one is also for back layer but it is positioned at the right edge of the layer above*/
parallaxList.add(new Parallax(BACK_TEXTURE_WIDTH, BACK_TEXTURE_HEIGHT, BACK_TEXTURE_WIDTH, BACK_TEXTURE_HEIGHT, SOME_VELOCITY_X, 0));

// Front
parallaxList.add(new Parallax(0, 0, FRONT_TEXTURE_WIDTH, FRONT_TEXTURE_HEIGHT, FRONT_VELOCITY_X, 0));
parallaxList.add(new Parallax(FRONT_TEXTURE_WIDTH, 0, FRONT_TEXTURE_WIDTH, FRONT_TEXTURE_HEIGHT, FRONT_VELOCITY_X, 0));

我们在每一帧的更新调用中更新层-

// In our example, TOTAL_LAYERS is 4
for (int i = 0; i < TOTAL_LAYERS; i++) {

    int tmpInt;
    Parallax parallax = parallaxList.get(i);

    parallax.update(deltaTime);

    // If one layer is off the edge, put it at the right of the next one
    // In this example, layers are moving from right to left
    if (parallax.position.x <= -parallax.width) {

        // We know that parallaxList's indexes 0 and 1 hold the back layers
        // and indexes 2 and 3 have the front layers. You can add additional
        // parameters in Parallax class to indicate a group so that you do not
        // have to determine the group in dirty way like this
        if(i == 0){
            tmpInt = 1;
        } else if(i == 1) {
            tmpInt = 0;
        } else if(i == 2) {
            tmpInt = 3;
        } else {
            tmpInt = 2;
        }

        parallax.setPosition(parallaxList.get(tmpInt).position.x + parallax.width, parallax.position.y);            
    }        
}

您可以使用 OrthographicCamera 和 SpriteBatch 来绘制视差层。您实际上可以使用您拥有的游戏相机,但我认为使用单独的相机更清洁。无论如何,视差纹理通常足够大,可以在单独的调用中进行批处理,因此使用游戏摄像机很可能不会为您节省一次绘制调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多