Generating cubic spline equation through 2 control points with certain gradient at these points.

102 Views Asked by At

As part of a game I am developing, I am trying to generate 3D terrain procedurally in chunks, and such that if I deload and reload a chunk, it will generate the exact same. Since I need the height z to be differentiable with respect to both x and y(will need to calculate portion of an object's weight that acts down the slope), I decided I will do this by considering the x and y directions separately, and generating a piecewise polynomial in each direction which will pass through a start and end control point, each of which require the curve to have a certain gradient and height.

When we generate a chunk, the program:

  • Generates 2 random heights and 2 random gradients for x = 0 and x = 1 respectively (this is done by seeding a new Random() instance with the sum of a random world seed generated at the start of each new game, and the chunk's x-coordinate, so these generated heights and gradients can be reseeded if we deload and load the chunk).
  • Uses these 2 heights and 2 gradients (somehow) to generate a cubic polynomial (could be a higher degree if this is required, but ideally not as I will be computing this thousands of times per tick).
  • Hence calculates dz/dx.
  • Repeats for y-direction.

By splitting into the x and y directions, we only have to consider how to generate a function f such that:

For some known constants m1,m2, h1, h2
f(0) = h1
f(1) = h2
f '(0) = m1
f '(1) = m2

I've been struggling to find a general cubic equation that fits these criteria, but if a higher degree alternative is required that would still be fine, I just can't seem to find what I'm looking for online. Natural splines are very commonly used but I don't want my gradient to be 0 at end points as this would make the grid pattern of chunk edges clearly visible in the height of the terrain. If anyone has an alternative method for generating procedurally generating differentiable terrain that would be much appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

It's possible but a bit cumbersome to write the four equations and solve. Here is a direct approach.

Since you only consider two nodes, forget the "spline", it's simply a cubic polynomial. Call the values of $f$ at $0$ and $1$: $y_0$ and $y_1$, the first derivative $d_0$ and $d_1$, and the second derivative $s_0$ and $s_1$.

The second derivative of a cubic is linear, hence $f''(t)=s_0(1-t)+s_1t=(s_1-s_0)t+s_0$.

Therefore $f'(t)=(s_1-s_0)t^2/2+s_0t+k$. And we know $f'(0)=d_0=k$ and $f'(1)=d_1=(s_1-s_0)/2+s_0+d_0$, hence $d_1-d_0=(s_1+s_0)/2$.

Then $f(t)=(s_1-s_0)t^3/6+s_0t^2/2+d_0t+l$. Then $f(0)=l=y_0$ and $f(1)=y_1=(s_1-s_0)/6+s_0/2+d_0+y_0$, hence $y_1-y_0=d_0+(s_1+2s_0)/6$.

We have the expression of $f$, but with two unknown parameters $s_0,s_1$. But we also have two equations:

$$s_0+s_1=2(d_1-d_0)$$ $$2s_0+s_1=6(y_1-y_0-d_0)$$

Subtract the equations to get

$$s_0=6(y_1-y_0)-4d_0-2d_1$$

Then using the first equation

$$s_1=2d_0+4d_1-6(y_1-y_0)$$

All in all,

$$f(t)=[d_1+d_0-2(y_1-y_0)]t^3+[3(y_1-y_0)-2d_0-d_1]t^2+d_0t+y_0$$