why does this cause my terrain to become massivley tall?

Started by
19 comments, last by billybob 21 years, 8 months ago

  

void smoothHeightMap(int iterations)
{
	int i, j, x, y;
	float st, sm[8];
	for(i = 0; i < iterations; i++)
	{
		for(y = 1; y < terrain_y - 1; y++)
		{
			for(x = 1; x < terrain_x - 1; x++)
			{
				sm[0] = terrain_HeightMap[x-1][y-1];
				sm[1] = terrain_HeightMap[x-1][y];
				sm[2] = terrain_HeightMap[x-1][y+1];
				sm[3] = terrain_HeightMap[x][y-1];
				sm[4] = terrain_HeightMap[x][y];
				sm[5] = terrain_HeightMap[x][y+1];
				sm[6] = terrain_HeightMap[x+1][y-1];
				sm[7] = terrain_HeightMap[x+1][y];
				sm[8] = terrain_HeightMap[x+1][y+1];
				st = 0;
				for(j = 0; j < 9; j++)
				{
					st = st + sm[j];
				}
				terrain_HeightMap[x][y] = float(st / 9);
			}
		}
	}
}
  
its supposed to smooth the terrain out by averaging the 9 heights around it and making that the new height. but it causes the terrain to grow miles high after only two iterations. did i make some stupid mistake i can''t seem to find for hours and hours?
Advertisement
Well, for one thing you initalised sm with 8 members but i think there are meant to be nine. This could be a mistake when transfering the code to the forum, but if not then I think that is your problem, could be wrong though

[edited by - Grambo on July 28, 2002 8:34:54 PM]
--24 Beers in a Case.24 Hours in a Day.Coincedence? I think not!www.gramb0.co.uk
but including 0 there are 9.
I meant the line
	float st, sm[8]; 

Here you initalise it with 8 memebers not 9
--24 Beers in a Case.24 Hours in a Day.Coincedence? I think not!www.gramb0.co.uk
When you define an [8] array, you create [0]-[7].

Thus,
sm[8] = terrain_HeightMap[x+1][y+1];
cannot be done, and
j < 9
should be < 8

just change the [8] array to [9].

------------
aud.vze.com - The Audacious Engine <-- It''s not much, yet. But it''s mine... my own... my preciousssss...
MSN: nmaster42@hotmail.com, AIM: LockePick42, ICQ: 74128155
_______________________________________Pixelante Game Studios - Fowl Language
oh it does that automatically?!?! crap, i need to fix a lot of things. i though you initalized it with the number that you needed including 0. sorry for the thread
Are you sure about that LockePick? I''ve always created the array with one less element (ie, if I needed 5 I would do nums[4]) and I''ve never ran into any problems. In which cases do you create the array with the higher number and when do you do it with one less? Do you always do the full number and I''ve just been lucky?
Ivve always used the higher number, and when I haven''t it usually crashed my program, so I think you must have been lucky Ivko. Either that or something strange is going on
--24 Beers in a Case.24 Hours in a Day.Coincedence? I think not!www.gramb0.co.uk
quote:Original post by Grambo
Ivve always used the higher number, and when I haven''t it usually crashed my program, so I think you must have been lucky Ivko. Either that or something strange is going on


there have been a few times when i run my program, i cause aol instant messenger''s buddy list window to freeze. i wonder if i''m overrunning my arrays?!
quote:Original post by Ivko
Are you sure about that LockePick? I''ve always created the array with one less element (ie, if I needed 5 I would do nums[4]) and I''ve never ran into any problems. In which cases do you create the array with the higher number and when do you do it with one less? Do you always do the full number and I''ve just been lucky?


WELLLLLLL...

You can do this as many times as you want, wherever you want and etc. BUUUUUT, when you do this your not reserving the last block of mem thath you think your reserving and so if the computer decides to use that space for some other data then your "last" entry in your array will be terminally corrupted. So you can say that if you want you can pass the array bounds at will (you can even just reserve 10 and use 10000), but you risk corrupting your data because your not reserving those blocks (worse yet, you could even pass over other data in memory and corrupt it with data that you want to put into the array).

Think of an array as a pointer to the first block in mem and then accessing [1] would be a pointer++ operation. This might help you understand why you risk writing over other data and losing array data.

I think this is a great reason *NOT* to pass the bounds in arrays.

This topic is closed to new replies.

Advertisement