Saturday, September 1, 2018

How to create 2D game terrain?


Last month, I experimented a lot in game development using cocos2d for the iPhone, especially on moving and modifying sprites. However, vertices and polygons are still new stuff for me.



I developed an algorithm that creates a set of points. I now want to use them to create my 2D terrain. Here is how it looks like when I connect the points in java, so that you can get the idea


A line graph of connected dots


The points are connected with lines. However a real game terrain is a shape that is colored or has a texture overlayed, right? For example:


An image of in-game terrain looking pretty


How could I use my points to create such a terrain?


What I tried so far is I created an array and filled it with my points. I pass this array to this method


void ccDrawPoly ( const CGPoint *  vertices,
NSUInteger numOfVertices,
BOOL closePolygon
)



draws a poligon given a pointer to CGPoint coordiantes and the number of vertices measured in points. The polygon can be closed or open



This actually draws a plain shape. But from the blue picture it looks like there is not one shape but many.


Please share you knowledge on how to create 2d game terrain.


Update 03.06.2011


Based on the two good answer I have following result


enter image description here



Answer




The easy/naive solution would simply be to take your existing points, and for each point have a corresponding point that's at the same Y position but off screen. From there, create your polygon by taking, say, points 0, 1, 1's pair, and 0's pair and pass that to your DrawPoly method. Then continue on with (1, 2, 2's pair, 1's pair), etc.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...