Archived

This topic is now archived and is closed to further replies.

billybob

why does this cause my terrain to become massivley tall?

Recommended Posts


  

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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by SnprBoB86
wow... now I see why people say C++ is dangerous...

-SniperBoB-


This is a C feature. If it causes any consternation then you should make a class which contains the array, and a count to see if you pass over the limit -> C++ magic...

Share this post


Link to post
Share on other sites
Hmm.. I can''t believe I didn''t know that Now that I think about it, I probably HAVE been running into problems, but whenever something happens for a weird reason I just shuffle around the code a bit until it works

Knowing this should save me a bit from one of those cases where everything seems fine but isn''t working.

Share this post


Link to post
Share on other sites
this isn''t your question, but i see a problem with your code (just because i''ve done it before). you iterate through each row and column in your terrain, and set the point there to the average of itself and the 8 around it; however, this changes the values there before they are used for the next row/column. so, when you are "smoothing" the point (2,2), the points (1,1),(1,2),(1,3),and (2,1) have already been "smoothed". while the effects of this aren''t obvious sometimes, there will be cases where it makes your terrain look funky (at least compared to what it would look like). maybe you will like it better that way, i dunno. but it annoyed me enough that i saved the "smoothed" values into a separate array, and then copied them all back to the terrain after all the calculations were done.

Share this post


Link to post
Share on other sites
i am a n00b. back in the day when i did qbasic, to get a ten element array, you would go a[9] and it would give you 0-9, meaning 10 elements. i just assumed c was the same

Share this post


Link to post
Share on other sites
i just dont go for the edges, i start at 1 and end at width - 1. in the final, the terrain will be invisible on the edges, and be really tall, to prevent people from driving off into nowhere

Share this post


Link to post
Share on other sites