# terrain generation help

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?

• 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? Commented Jul 7, 2011 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)? Commented Jul 7, 2011 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). Commented Jul 7, 2011 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 Commented Jul 7, 2011 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. Commented Jul 7, 2011 at 20:41