Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I'm trying to make a 3D terrain generator. In doing so, I decided that I would use basic rectangles and then just turn them by having 4 points, 1 on each side, then turn the rectangle to fit in those points. Now when I try to make it so that it can decide a new point, with the distance being the length of the new rectangle ( which all rectangles are congruent so it'll stay the same ) and the slope of the 2D line between the bricks will be a randomly picked number ( Math.random()*3-3 ) so I tried to solve it like this

sizeOfRectangle=(X-oldPoint.X)*new_slope

then came out like this

X=sizeOfRectangle/new_slope+oldPoint.X

then find the y-intercept

B=oldPoint.Y-(new_slope*oldPoint.X)

then I would plug in X to the 2D equation of a line

Y=new_slope*X+B

yet for some reason all the rectangles would come out facing either 90 or 180 degree angles. Did I do my math wrong or is it some other error I made?

share|improve this question
    
You begin by saying that you are trying to rotate a rectangle to fit inside 4 points, but in your code you are only looking at a single point. Could you explain what X and oldPoint.X are in terms of the rectangle you are trying to rotate? –  Vladimir Sotirov Jul 7 '11 at 20:07
    
I'm afraid I don't understand what you're trying to do. Are the projections of the rectangles (down to the sea level) always congruent? If so, then why do you need to calculate two coordinates? Aren't you then only interested in the altitude? I would call the altitude $z$, but you may have labelled your axes differently. Please tell us which axes is which! I cannot guess your data structure from this excerpt either, what are all the vertices of the new rectangle? (if that's how you do it)? –  Jyrki Lahtonen Jul 7 '11 at 20:11
    
And another point: How do you guarantee that your rectangle is planar? A rendering routine may fix that for you, if it only projects the vertices to the screen, but non-planar rectangles may produce unexpected gaps near the border, when you clip them with respect to the viewing frustum. Or may be clipping will work out right, but the calculation of the normal may be off (and hence everything that depends on that normal). –  Jyrki Lahtonen Jul 7 '11 at 20:13
    
sorry I did a bad job at explaining it, basically the rectangles are objects that I know are planar and for each new rectangle it's just a clone of that. Then how I make it is start at one corner, do all the calculations for the different points, then using function ang(a,b) { return math.atan2(b.y - a.y, b.x - a.x) * 180 / math.pi; } to find the rotation needed for the X and Z axis. By making each point the midpoint of each side, I know that the 2 points used will have the same Z or X axis, so if it's the Z axis I'll just plug it in where the X would go. This way it's just simpler 2D math –  SDuke Jul 7 '11 at 20:40
    
Then I try to find where the next rectangles will be by adding a new one on each side of it, from there I want it to find a new point ( either on the X or Z side, whichever way it moved ) that's exactly the same distance away as the rectangle will be long. So I tried that. Then if it was moved on the X side of the original rectangle, the new rectangle would use the same Z rotation as the original rectangle. –  SDuke Jul 7 '11 at 20:41
show 2 more comments

2 Answers

up vote 1 down vote accepted

I'm having trouble following your explanations (and it looks like I'm not the only one), but the general term for the kind of algorithm I think you're trying to implement (and the kind which lewellen describes) is "midpoint displacement". Google for it.

Ps. As Jyrki notes in the comments, a minor problem with using rectangles for this kind of algorithm is that, in general, four random points in 3D space will not be coplanar. Using triangles (and repeatedly subdividing each of them into four sub-triangles) avoids this problem, although of course one may also work on a rectangular lattice (as in the "diamond–square" algorithm) and only finally subdivide rectangles into triangles if and as needed.

share|improve this answer
add comment

Seems like it would be easier to start with a rectangle. For each of the four corners, pick a random altitude for each corner.

From the resulting rectangle, cut it up in to four equal sized rectangles. For the point shared by all four rectangles (the one in the middle if you were to draw it out), pick a new altitude for this point between the minimum and maximum altitudes of the original rectangle.

For each of the four resulting rectangles, perform the above procedure until you reach a desired depth or the resultant rectangle you are working on as an area less than some desired area.

share|improve this answer
    
I was going to try to do it so that I could implement some perlin noise stuff into it later and have endless terrain, but I guess I'll try doing a fractal aproach first. –  SDuke Jul 8 '11 at 1:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.