简体   繁体   English

在iphone或Java上实现像Scorched Earth Game这样的地形破坏

[英]Implementing Terrain destruction like Scorched Earth Game on iphone or Java

I'm looking for an example of how to implement 2D terrain destruction that you see in games like scorched earth or on the iphone iShoot . 我正在寻找一个如何实现2D地形破坏的例子,你可以在烧焦的地球或iphone iShoot等游戏中看到

I'm looking to implement a game that needs to do destructible terrain and render it using OpenGL (LWJGL in Java) and using OpenGL ES on the iPhone. 我正在寻求实现一个需要做可破坏地形的游戏,并使用OpenGL(Java中的LWJGL)和在iPhone上使用OpenGL ES来渲染它。

替代文字
(source: vintagecomputing.com ) (来源: vintagecomputing.com

As I recall, in Worms they used two images; 我记得,在沃尔姆斯他们使用了两张图片; The "pretty" terrain with colour, and a mask terrain that is pure black and white. 带有颜色的“漂亮”地形,以及纯黑色和白色的面具地形。 Hit detection is always done on the mask. 命中检测始终在掩码上完成。

If you actually want the terrain to collapse like it did in Tank Wars, you'll need to iterate over each column of your image and have it search for gaps between the terrain and the bottom of the playing field. 如果你真的希望地形像坦克大战一样崩溃,你需要迭代你的图像的每一列,并让它搜索地形和比赛场地底部之间的间隙。 If any gaps are detected, shift the terrain above the gap down to the lowest point possible in your column. 如果检测到任何间隙,请将间隙上方的地形向下移动到列中可能的最低点。

A simple example of this could be done with an array where 1 represents solid terrain and 0 represents empty space. 一个简单的例子可以用一个数组来完成,其中1代表实地形,0代表空的空间。 In this case, I've set up the left side of the array as ground level, to element [0] would be on the ground: 在这种情况下,我将数组的左侧设置为地平面,元素[0]将在地面上:

[1,1,1,1,1,1,0,0,0,0]

Lets assume the terrain is struck from the side and a hole is made: 让我们假设地形从侧面被击中并形成一个洞:

[1,1,0,0,1,1,0,0,0,0]

You're now left with a floating piece of terrain above another piece of terrain. 你现在留下了另一块地形上方的浮动地形。 To make the floating terrain collapse, iterate over the array, keeping track of the first position you find a 0 (empty space). 要使浮动地形折叠,请遍历数组,跟踪找到0的第一个位置(空白区域)。 Then, as you continue to iterate, upon discovering a 1 (terrain) simply shift the 1 to where the 0 was. 然后,当您继续迭代时,在发现1(地形)时,只需将1移动到0所在的位置。 Repeat the process by iterating from that the old 0 position + 1. 通过迭代旧的0位置+ 1重复该过程。

[1,1,1,0,0,1,0,0,0,0]

[1,1,1,1,0,0,0,0,0,0]

This is the basic approach, not the most efficient one. 这是基本方法,而不是最有效的方法。 It would be much faster to move all indexes of terrain above the gap down at the same time, for example. 例如,将所有地形索引同时向下移动到间隙会快得多。

EDIT: 编辑:

As the first comment states, a sort on the list is even easier. 正如第一条评论所述,列表上的排序更容易。 I'm keeping my original response intact since it helps explains the actual principle behind the collapsing terrain. 我保持原始的响应完整,因为它有助于解释崩溃地形背后的实际原理。

Soviut's answer is great! Soviut的答案很棒! I use a similar algorithm in the destructive terrain feature in Scorched Earth for iPhone . 我在Scorched Earth for iPhone的破坏性地形特征中使用了类似的算法。 I decided to stay true to the original and have the terrain settle instantly, but while I was considering having animated terrain settling, I ran into some performance problems. 我决定坚持原版并立即让地形稳定下来,但是当我考虑动画地形稳定时,我遇到了一些性能问题。 You can see evidence of this in iShoot as well, since iShoot uses a slowly settling animated terrain. 你也可以在iShoot中看到这方面的证据,因为iShoot使用缓慢定位的动画地形。 There are situations where the ground is still settling from one player's turn when the next player fires a weapon. 有些情况下,当下一个玩家发射武器时,地面仍在从一个玩家的转弯处解决。 This can interfere with the shot, and the interference can change depending on how quickly the next player fires. 这可能会干扰击球,并且干扰可能会根据下一位牌手的射击速度而改变。 Since Scorched Earth is a turn-based game, it seems like a good idea to have the game wait until the ground is settled until switching to the next player. 由于Scorched Earth是一款回合制游戏,让游戏等到地面稳定直到切换到下一个玩家似乎是一个好主意。

To render the terrain, I used OpenGL to draw a polygon with one pair of vertices at each horizontal screen location, like this: 为了渲染地形,我使用OpenGL在每个水平屏幕位置绘制一对带有一对顶点的多边形,如下所示:

1 3 5 7 9
0 2 4 6 8

Points with even numbers represent the line of pixels at the bottom of the screen. 带偶数的点表示屏幕底部的像素线。 Points with odd numbers represent the vertical pixel location of the terrain. 具有奇数的点表示地形的垂直像素位置。 This information is copied into a point array, which is rendered with glVertexPointer, glColorPointer, and glDrawArrays, as a triangle strip, like this: 此信息被复制到一个点数组中,该数组使用glVertexPointer,glColorPointer和glDrawArrays作为三角形条带进行渲染,如下所示:

// prepare vertex buffer
for (int i=0,j,k=0,K=480;k<=K;k++) {
    j = (k-(int)offsetX+480)%480;
    vGroundLevel[i++] = k;
    vGroundLevel[i++] = offsetY>0 ? 0 : offsetY;
    vGroundLevel[i++] = k;
    vGroundLevel[i++] = [env groundLevelAtIndex:j]+offsetY;
}
....
// render vertex buffer
glVertexPointer(2, GL_FLOAT, 0, vGroundLevel);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cGround);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 2*480);

The offsetX and offsetY parameters allow the terrain to be repositioned relative to the screen, allowing the player to move around the environment interactively, while maintaining the game environment as a wrap-around continuum. offsetX和offsetY参数允许相对于屏幕重新定位地形,允许玩家以交互方式在环境中移动,同时将游戏环境保持为环绕连续体。

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

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