Modifying Terrain in a circle

Blitz3D Forums/Blitz3D Programming/Modifying Terrain in a circle

dirkduck(Posted 2003) [#1]
Hey everyone. I'm trying to be able to modify a piece of a terrain at a given x,y coordinate, then around that point in a circle with a variable radius. Right now I raise the terrain with a square "radius" easily enough:
For xr=0 To radius
		For zr=0 To radius
			ModifyTerrain ter,x+xr,z+zr,height
			ModifyTerrain ter,x-xr,z-zr,height
			ModifyTerrain ter,x+xr,z-zr,height
			ModifyTerrain ter,x-xr,z+zr,height
		Next
	Next


But im not so sure about how to do it circularly. Thanks for any help!


Rob Farley(Posted 2003) [#2]
for angle=0 to 358
for r=0 to radius

x=sin(angle)*r
y=cos(angle)*r

..etc


You've just got to be careful that the bigger the radius you may have to reduce the amount the angle increases by or you'll miss points out.


sswift(Posted 2003) [#3]
That's the slow way.

Also, it will modify points in the middle more than points at the edges, creating a cone shaped depression.

Instead, do this:

For xr=-radius To radius
   For zr=-radius To radius
      If (xr*xr + zr*zr) < (radius*radius)
         ModifyTerrain ter,x+xr,z+zr,height
      EndIf
   Next
Next

This is the fastest way to do it.

What this code does is looks at each point in a square section of terrain once, determines if it is within a circle of X radius, and if it is, modifies that point.

This code:
(xr*xr + zr*zr) < (radius*radius)

Is an optimization of this:
sqr(xr*xr + zr*zr) < radius


sqr(xr*xr + zr*zr) is the distance equation. That tells you how far the point is from 0,0.

I square radius (radius*radius) because it's faster to compare a squared radius against a squared distance than to find the square root of the distance.

Of course, for the number of points you are going to modify, this optimiation is trivial, and won't really affect the speed in any perceiveable way, but I tend to optimize out square roots out of habit because when doing them on thousands of points they can add up.


dirkduck(Posted 2003) [#4]
Thanks for the tips! One more problem though. I'm trying to have it increase the height every time the code is called, with TerrainY (I've tried with TerrainHeight, same problem though) and adding the height variable on top of the TerrainY

For xr=-radius To radius
   For zr=-radius To radius
      If (xr*xr + zr*zr) < (radius*radius)
         ModifyTerrain ter,x+xr,z+zr,TerrainY(ter,x+xr,y,z+zr)+height
      EndIf
   Next
Next


After I do that the 'mount' wont get any higher, it just makes a bunch of points (no bigger than the initial heigh variable) and shifts them around everytime it is called. Thanks for any help!

EDIT: Just messing around, it seems like TerrainY makes the points, and TerrainHeight slowly decreaces the height, although it reamins even across all the points.


dirkduck(Posted 2003) [#5]
Hmm, well playing around I think I got it working, had to mess with scaleentity. Thanks for the help sswift and Dr. Av!


Drago(Posted 2003) [#6]
remeber the Highest value for a terrains Hieght is cliped to 1
so you only have 0.0 to 1.0 for the hight value, so you do have to scale it.


dirkduck(Posted 2003) [#7]
Yep, I didn't realize that before, and that was what was causing all the problems. Also, if I make the height vairable 0.005 it works fine (maybe 0.004 would too, I forget), but when I go any below that (0.003, 0.0001...etc), it doesn't make any change at all. Anyone know why? Thanks.


Floyd(Posted 2003) [#8]
Terrain height values are stored internally as single bytes.

A value of 1 means 1/255, 2 means 2/255, ... 255 means 255/255 = 1.

Thus a 1024x1024 terrain needs only 1MB to store heights.
But there are only 256 possible heights.