Using Quadratic Bezier Curves to generate a cave that stays within certain bounds

I'm working on a project that generates a series of quadratic bezier curves and connects them together, maintaining slope from the end of one segment to the beginning of the next to make the transition smooth. The problem is that while the path is smooth, it tends to go off screen frequently.

The way this is done currently:

``````P0 = new Point(0, gapStart);
P2 = new Point(wallWidth, gapEnd);
P1 = getAnchorPoint();
``````

where:
GapStart is the P2.y of the previous curve
wallWidth is a constant
gapEnd is the one and only random aspect of the cave generation It's the Y value that the curve will end at.

The getAnchorPoint function takes points 0 and 2 and generates an anchor point so that the slope at the beginning of this segment is the same as the end of the previous segment.

So the main question is, what values can gapEnd be to ensure that the next curve has a gapEnd that won't send the curve off the screen?

In other words, how do I determine the min and max values of gapEnd so that the next curve is safe?

In addition, it's important that these values don't box the following curve in to being impossible: i.e. they cannot allow for the next curve to not have a possible solution that would allow for continued curve generation.

Image of cave generation process

• Why are you chopping them up into small pieces at all?
– Job
Aug 6, 2011 at 2:15
• I'm using AS3's built in graphics.curveTo function that generates a quadratic curve, so I'm making each curve a piece. I also don't want to do pre-generated levels - the levels will use a random seed, but they will be generated as the game progresses. The levels also will be indefinite size. As for a bezier curve with many more points, I think I would run into issues getting the curves to look right. I'm also not sure how much processing power it would take to do a bezier curve with many, many points. Aug 6, 2011 at 2:18
• I'm about a year and a half into a project that has piecewise cubic beziers at it's heart. I literally spend days talking about splines. But I am not sure what you mean here. My guess is that you want to maintain C2 continuity while constraining the positions of the control points- if that is the case you need to define your problem a bit better before anyone can give you a good answer. Aug 6, 2011 at 2:41
• @TDuncanSmith, I'm not sure I understand what you're saying. We want to maintain continuity within the whole set of beziers, but we've already solved that problem. The issue is choosing an acceptable Y for P2 that allows a following curve that stays on screen. Aug 6, 2011 at 3:02
• What I mean is that you are not expressing your constraints very precisely. What sort of continuity are you looking for? C0? C1? C2? Do you even understand what I am asking here? Do you understand that the beziers know nothing about your screen coordinates? If your question were a question, I could answer it. But you are not asking a question... Aug 6, 2011 at 5:31