1. 程式人生 > >2D區塊地圖地表生成演算法

2D區塊地圖地表生成演算法

Tile/Map-Based Game Techniques: Handling Terrain Transitions

This article is another in my series of Tile/Map-based Game Techniques. In this article I discuss a method for handling terrain transitions on your main map display.

First off, by "terrain" I am referring to the background or base layer of graphics for the map display. Terrain might refer to normal terrain types, such as grasslands or forests, but it is not limited to this. In a science-fiction game, for instance, the terrain could be bare metal or even the landscape features of another planet.

The Problem

The problem of terrain transitioning grows out of the need for a single type of terrain to be able to mesh with itself and still look good when placed next to another terrain. A forest tile should fit together with other forest tiles so that the forest proceeds seamlessly from one map cell to the next. But what happens when the forest tile is placed next to another type of terrain, such as mountains or grassland? Without some form "transitioning" the map looks very blocky and artificial.

Figure 1: Terrain Without Transitions


One solution to the problem is to create specialized transition tiles that "blend" sections of each terrain into a single tile. However, the need for all terrains to transition into any other means that the number of specialized tiles escalates quickly. It may not be obvious at first glance, but the number of transitions required even between just 2 terrain types is quite large. On a square grid, there are 8 possible points of transition: one for each side and one for each corner. Even the simple forest-to-grassland transition would require 256 transitions, and this would have to be done for each terrain transition combination. If you have 9 or 10 terrain types, the amount of graphics this approach would require is unworkable.

This can be refined some by using transitions with a transparent element, allowing for more variety with fewer tiles. The forest transitions would then be able to overlap any terrain. But 256 transition variations for each terrain type still seems pretty excessive.

So how do you trim down the number of required transition graphics to a number that is more manageable? This is how we did it.

Our Solution

The first part of our solution was to assign a "precedence" to the various terrain types. By precedence, I mean that when two different terrain types meet, one of them invariably "overlaps" the other. In the example of forest meeting grassland, if forest has a higher precedence (and it should) then it will always overlap the grassland.

In Artifact, we used the following terrain precedence (listed highest to lowest): jungle, forest, mountain, hill, swamp, deserts, grassland, water (open water or river). Please note that this precedence does not reflect the relative elevations of the terrain but is instead based on which terrains looks best when overlapping other terrains.

Figure 2: Artifact Terrain Precedence


The next step was to reduce the number of terrain transition variations from 256. This number can be cut drastically by separating the "edge" transitions and the "corner" transitions. As it was pointed out above, a single terrain cell has 8 points of transition: one for each side and one for each corner. Thus, there are only 16 possible edge transitions, and 16 possible corner transitions. By using combinations of edge and corner transitions you can create all of the necessary 256 variations with only 32 total tiles. This is a huge reduction in graphics required.

The template we used followed a binary format. For the edges, west was considered "bit 0", north was "bit 1", and east and south were "bit 2" and "bit 3", respectively. Similarly for the corners, the northwest corner was "bit 0", the northeast corner "bit 1", and so on. How we arranged the actual terrain transition graphics is demonstrated in Figure 3. If you think of the covered edges as 1 and the non-covered edges as 0, you can see that the columns proceed in normal binary manner: 0000, 0001, 0010, 0011, 0100, and so on.

Figure 3: Terrain Transition Template


Figure 4 shows how this was applied to create the grassland transitions in Artifact.

Figure 4: Artifact Grassland Transitions


With this method drawing the map is now a 2-step process. For each map cell, the base terrain must be drawn, and then any transitions that overlay it in reverse order of precedence (lowest precedence drawn first). However, the reduction in graphics required more than makes up for the additional work. Also, since the transition graphics are mostly transparent, the actual "work" involved is less than it might seem.

Using the precedence established earlier, and the bit assignments for the edges and corners, calculating which transitions you need in a particular map cell is relatively straightforward. Essentially, for each map cell, you check all adjacency possibilities for terrain types that overlap the terrain of the cell. The transition information for a single terrain type need only use 8-bits of data (4 bits for the edges and 4 bits for the corners) which fits conveniently into a single byte. Thus, the total terrain transition information for Artifact's 9 terrain types requires only 9 bytes per map cell.

You can pre-calculate the transition information and store it with the map, or you can calculate it "on the fly" at runtime. For rendering Artifact's map display, I calculate the transitions for the visible portion of the map only. This reduces the amount of storage required since only a small portion of the map is visible at one time.

A quick example: To calculate the transitions needed for a hill terrain, you need only consider any adjacent jungles, forests, and mountains, since those are the only terrain types that have a higher precedence. Figure 5 demonstrates the overlapping of transitions on the base terrain graphics, with a hill as the center terrain.

Figure 5: Artifact Terrains, Before and After


Conclusion

With a bit of preparation in the graphics and a few tricks during the rendering, you can achieve professional-looking terrain transitions in your game. While drawing the map becomes a bit more complicated, the reduction in graphics required and the flexibility of the system more than make up for that.

Copyright ?1999 by David Michael. All Rights Reserved.
All Artwork Copyright ?1999 by Samu Games. All Rights Reserved.


Date this article was posted to GameDev.net: 2/23/2000
(Note that this date does not necessarily correspond to the date the article was written)

相關推薦

2D區塊地圖地表生成演算法

Tile/Map-Based Game Techniques: Handling Terrain Transitions This article is another in my series of Tile/Map-based Game Techniques. In

untiy 2D 曲面地圖動態生成

      前面說要做個2D曲面,因為一直在找工作沒時間,如果大家有機會可以幫小弟推薦推薦,小弟不勝感激。好言歸正傳了,開始講今天的東西了,先截個圖看看效果。圖如下: 第一張是在scence檢視下的圖,可以很清楚的看到這個地形的分佈,第二張則是一個球在場景中的執行圖,也

2D地圖擦除演算法

.  關於2D地圖擦除演算法,去年我寫過一個實現,勉強實現了地形擦除,但跟最終效果還相差甚遠,這次我寫了一個完整的實現,在此記錄,留個印象。 .  去年的版本<<演算法 & 資料結構——裁剪多邊形>>,因為受限於當時框架用GDI實現的渲染器,只有擦除地形沒有擦除地圖,這次換了O

區塊鏈技術--區塊鏈的生成和鏈接

數據 交易 pos gpo 節點 技術 隨機 生成 body 1、區塊的生成 (1)礦工在挖礦前要組建區塊,將coinbase交易打包進區塊 (2)將交易池中高優先級的交易打包進區塊   優先級=交易的額度 * UTXO的深度/交易的size (3) 創建區塊的頭部【版

web叢集全域性唯一request id生成演算法, 替代uuid等“通用”方案

如何為每一個web請求分配一個在全叢集範圍內都唯一的request id 卻又不想去實現一個複雜的集中式id序列生成器呢? UUID? 這或許是個辦法,但不覺得不太甘心麼? 下面的這個方式可能可以幫到你: package test; import java.util.concur

js版裝置id生成演算法分析

前言 1、網際網路業務經常會有送券、領紅包等活動,通常禮品發放是基於單個使用者ID只發放一次原則,同時還會結合是否新使用者、是否非常客(熟客不給優惠)等策略。 羊毛黨薅羊毛場景:1、編寫領禮品(券和紅包統稱禮品)指令碼;2、匯入批量使用者cookie或手機號,匯入批量網路代理(繞基於IP的風

區塊鏈的加密演算法__一,從維吉尼亞到RSA(未完待續)

密碼學源於數學。處於一些特殊的需要,自從數學有所發展的時候,就有了加密者與解密者。 一開始的密碼學加密法都是對稱加密法,這種情況一直到上個世紀中葉RSA加密法誕生。如今區塊鏈所使用的各種各樣的加密法都為不對稱加密,而最早的不對稱加密法就是RSA加密法,是它開啟了公鑰密碼學的新時代,以及如今的區塊

遞迴解決全排列生成演算法

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

LCG(linear congruential generator): 一種簡單的隨機數生成演算法

目錄 LCG演算法 python 實現 LCG演算法 LCG(linear congruential generator)線性同餘演算法,是一個古老的產生隨機數的演算法。由以下引數組成: 引數 m a c X

usersig 生成演算法 python 描述

由於我們的使用者使用的後臺的工具和平臺不盡相同,而我們 api 所能適應的平臺是有限的,所以在此簡要描述下 usersig 的生成演算法,以便在使用者需要而我們又沒有提供時,使用者可以自己進行實現。下面是 python 描述的演算法(其實直接可以用的), #! /usr/bin/pytho

據說,80%的人都搞不懂雜湊演算法 區塊鏈 雜湊演算法

本文約9000字+,閱讀(觀看)需要52分鐘 聊到區塊鏈的時候也少不了會聽到“雜湊”、“雜湊函式”、“雜湊演算法”,是不是聽得一頭霧水?別急,這一講我們來講講什麼是雜湊演算法。 雜湊是一種加密演算法 雜湊函式(Hash Function),也稱為雜湊函式或雜湊函式。雜湊函式是一個

區塊鏈的共識演算法

歡迎訪問我的個人站點,老廖的個人部落格。 前言   區塊鏈最有價值的地方就在於共識演算法,目前各種公鏈、聯盟鏈群魔亂舞,共識演算法林林總總。   研究了一段時日,也到了總結的時候,用幾篇文章將它們梳理一下。 作用、意義   區塊鏈是當下的大熱門,各種各樣的區塊鏈專案都會用一定篇幅來描述它的共識機制。

ArcGIS API for JavaScript 4.9學習筆記一(建立2D/3D地圖

ArcGIS API for JavaScript 4.9學習筆記一(建立2D/3D地圖) 2D: 程式碼: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta

偽隨機數生成演算法-梅森旋轉(Mersenne Twister/MT)

今天主要是來研究梅森旋轉演算法,它是用來產生偽隨機數的,實際上產生偽隨機數的方法有很多種,比如線性同餘法, 平方取中法等等。但是這些方法產生的隨機數質量往往不是很高,而今天介紹的梅森旋轉演算法可以產生高質量的偽隨 機數,並且效率高效,彌補了傳統偽隨機數生成器的不足。梅森旋轉演算法的最長週期取自一個梅森素數

usersig 生成演算法純 python 實現

  #! /usr/bin/python # coding:utf-8 # 此檔案是 tls sig api 的 python 另一種實現 # 使用了 python ecdsa 開發庫 __author__ = "[email protected]" __date__

MBTiles 離線地圖生成和使用

1、mbtiles地圖簡介 mbtiles 為離線地圖,形式為瓦片形式,用sqlite資料庫儲存,資料庫內容符合mbtiles儲存規範。 地圖儲存有精確的經緯度資訊。 2、MBTiles 地圖使用。 所有生成的mbtiles檔案都可以用Map Tiler 軟體開啟。 lea

區塊鏈主流共識演算法

一.POW(Proof Of Work) Proof Of Work,也就是工作量證明。工作量證明系統(或者說協議、函式),是一種應對拒絕服務攻擊和其他服務濫用的經濟對策。它要求發起者進行一定量的運算,也就意味著需要消耗計算機一定的時間。這種系統要求得到證明的過程是低效且漫長的,可是校驗則是高效且迅速,概括

一種較為高效的TreeList生成演算法(Delphi實現)

記得不久前曾寫過篇關於TreeList生成的文章。雖然那個演算法裡,我已經有對葉節點做判斷,避免無用的Filter操作。但是非葉節點的Filter操作依然是無可避免的。而Filter又是影響整個生成的最重要因素,因此當帶子節點的節點很多時,速度還是要被拖下去的。 後來我看到了一種覺得不錯的思路,

用於視覺化的座標軸與外包框的網格生成演算法

在三維模型顯示與互動任務中, 座標軸和外包框是其基本的輔助工具。相關方法可以分成幾何體(網格構造)演算法與著色器方法(點精靈方式等),本文僅討論相關的網格生成。 考慮到座標軸與外包框可以用柱體與錐體兩大基本元素組合生成,基本元素又可進一步劃分為由取樣點構成的圓周,圓面和錐體斜面都可由取樣點圓周與中心點構成扇形

比特幣地址生成演算法詳解

1 生成過程 比特幣地址生成流程如下圖所示:   第一步,隨機選取一個32位元組的數,大小介於1~0xFFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFE BAAE DCE6 AF48 A03B BFD2 5E8C D036 4141之間,作為私鑰 18e14a7b6a307