Side view terrain generation (worms style)

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Post Reply
User avatar
Chris Jones
Lich
Posts: 1742
Joined: Tue Apr 05, 2005 1:11 pm
Location: Gosport, South England
x 1

Side view terrain generation (worms style)

Post by Chris Jones »

im thinking about making a small worms style game as a side project from OGE.

ive tried googling for answers but didnt come up with much, probably because i dont know enough to start searching for anything technical, like algorithm names etc

basically, i want to be able to randomly (maybe seeded? so i can generate the same landscapes more than once?) generate terrain, with overhangs/caves.

also, any information on doing this with ogre? for example, using tileable textures, but using it over the terrain? so when tiling the image, removing/making transparent parts of the image where the terrain is/isnt there? if that makes sense?

also, i dont want to use worms, i want to have different teams, humans, aliens etc

worms is just using 2d images with animations, is it a good idea to use that approach or someway of using 3d parts instead? 1 of the problems im thinking of, is in worms its easy to have a walk animation for the worm and have them climbing steep gradients on the terrain because it doesnt have legs, but with a person, it would have to look like their legs are actually standing on the terrain and walking up it, any ideas on how to approach that too?

thanks

Chris
Horizon
Greenskin
Posts: 122
Joined: Thu Jun 02, 2005 3:24 pm

Post by Horizon »

I suppose as long as the terrain is 2d it should be possible to generate... You could look at using some kind of fractal. For example you could even just use mandlebrot and test if the mandelnumber > someConstant that pixel is solid, else it's open. Mandelbrot probably doesn't give too interesting levels though :P

As to the characters, why not make them fully 3d then you could animate them normally. This also gives the possibility for ragdolls etc. Maybe the physics shouldn't be completely 3d though, because you need to keep the entities more or less on a plane. Completely limiting the physics to a plane would remove some spectacular visuals like characters being blow towards/away from the camera though :).
User avatar
Kezzer
Orc
Posts: 444
Joined: Mon Aug 16, 2004 2:19 pm
Location: Silicon Valley

Post by Kezzer »

I swear I read somewhere that there was a way of changing a camera to orthographical although I'm most probably very wrong.

[EDIT] I swear it was done in The Blob game to generate the top-view maps? [/EDIT]
code | blog
jacmoe wrote:MMORPG good. :o
Xavier bad. :x
Horizon
Greenskin
Posts: 122
Joined: Thu Jun 02, 2005 3:24 pm

Post by Horizon »

Ah yes, there is. Let me see....

In Frustrum and thus inherited by Camera:
virtual void setProjectionType (ProjectionType pt)
Sets the type of projection to use (orthographic or perspective).
User avatar
nickgravelyn
Halfling
Posts: 96
Joined: Sat Jan 28, 2006 7:41 pm
Location: Michigan

Post by nickgravelyn »

If I had to guess, I'd say the best way to generate the terrain would be to read in an image and generate your terrain based on the pixels in the image. It seems like that would be a nice way to design the levels.

As for random levels, I recently did some 2D fractal work (albeit written using C++, GLUT, and OpenGL but the vertex generation can still be useful) and figured you could use it as a start. The function of interest to you would be void Recursion(void). That's where all the random splitting and generation occur. Hopefully this will compile fine in Windows, but I've only tested it in Mac OS X so far.

**Edit: Removed most code to save space. PM me for the full code**

Code: Select all

using std::list;

class Point
{
public:
	Point():x(0),y(0) {}
	Point(float a, float b):x(a),y(b) {}
	
	float x, y;
	
	void Normalize()
	{
		float mag = 1 / sqrt(x*x+y*y);
		x *= mag;
		y *= mag;
	}
	
	void operator=(Point a)
	{
		x = a.x;
		y = a.y;
	}
	
	void operator+=(Point a)
	{
		x += a.x;
		y += a.y;
	}
	
	void operator-=(Point a)
	{
		x -= a.x;
		y -= a.y;
	}
	
	Point operator-(Point a)
	{
		Point r;
		r.x = x - a.x;
		r.y = y - a.y;
		return r;
	}
	
	Point operator+(Point a)
	{
		Point r;
		r.x = x + a.x;
		r.y = y + a.y;
		return r;
	}
	
	Point operator*(float a)
	{
		Point r;
		r.x = x * a;
		r.y = y * a;
		return r;
	}
	
	void operator*=(float a)
	{
		x *= a;
		y *= a;
	}
};

float Distance(Point a, Point b)
{
	return (sqrt(pow(a.x - b.x, 2) +
				 pow(a.y - b.y, 2)));
}

list <Point> points;

void Recursion()
{
	list<Point>::iterator itr, itr2;
	Point start, end, slope, mid;
	float distance, frandom;
	int random;
	itr = points.begin();
	itr2 = points.begin();
	for(itr++; itr != points.end(); itr++, itr2++)
	{
		end = (*itr);										//get our end point (right)
		start = (*itr2);									//get our start point (left)
		slope = end - start;								//find the slope between them
		slope.Normalize();									//get this as a unit normal
		distance = Distance(end, start);					//find the distance between them
		mid = end - (slope * (.5 * distance));				//get the midpoint
		random = (rand() % 201) - 100;						//find a random number -100 to 100
		frandom = (float)random / 100;						//turn that number into a percent -1.0 to 1.0
		mid.y += frandom * changeAmount;					//move the midpoint up or down based on percent
		itr2 = points.insert(itr, Point(mid.x, mid.y));		//insert our new midpoint and move the start point
	}
	changeAmount *= pow(2, -roughness);						//change the height changing variable
}
Nick Gravelyn
User avatar
Chris Jones
Lich
Posts: 1742
Joined: Tue Apr 05, 2005 1:11 pm
Location: Gosport, South England
x 1

Post by Chris Jones »

thanks for your replies

about using an image, i could, but i want to randomly generate terrain, otherwise i would have to draw them myself, so i wouldnt have many maps at all.
tidalwv
Gnoblar
Posts: 23
Joined: Tue Apr 04, 2006 8:22 pm

Post by tidalwv »

I did a simple 2D Scorched Earth clone with randomized terrain. To draw the terrain I iterated through the columns of the image surface, picked a pixel at some height, and filled everything below it as land. The height of each column would be based on the previous one, a random number, and wether the land was sloping up, down or flat. From there I just used the bitmap as a mask for what was land and what was not.

It's crazy simple and worked for my purpose, but it isn't anything close to Worms' maps; it might be a start though. I don't know how you'd pull this off with Ogre, though; I was using SDL for graphics for this.
Post Reply