Making a function periodic

766 Views Asked by At

This might not be the best place to ask this question, but here it goes... I'm creating a game and need 3D sea waves. Since it's for mobiles, there's no time to generate entire screen worth of waves on every frame, so I figured I'll create a pattern and copy it over and over again. I created a function $h(x,y,time)$ which returns $z$ coordinate of point $T(x,y)$ where $x\in[0,m\rangle\cap\mathbb{N}$ and $y\in[0,n\rangle\cap\mathbb{N}$ based on modified simplex noise algorithm.

I had difficulty visualising algorithm for 3D waves, so I started with a simple graph plot, where y is the height of the wave. By running simplified function $h$ as $h(x)$, I get something like this:

2d_pattern_repeat

Obviously, this is no good. You can clearly see that the pattern doesn't smoothly fade into next instance. Therefore, I calculated $y$ coordinate as $$y=h(x)\cdot(1-\frac{x}{m})+h(x-m)\cdot\frac{x}{m}$$

This results in something like this:

2d_pattern_blend

It was good enough for me, so I attempted to expand it to 3D space. My math and logic seem non-existant from this point on, as the best I could come up with is this:

if (y-x < 0) then $A(x-y,0),B(m,n-x+y)$

else $A(0,y-x),B(m+x-y,n)$

$$f=\frac{\sqrt{(A_x-x)^2+(A_y-y)^2}}{\sqrt{(A_x-B_x)^2+(A_y-B_y)^2}}$$

$$z=h(x,y,0)\cdot(1-f)+h(x-m,y-n,0)\cdot f$$

This fails terribly. Here's the result of that expression - blue square is a $z=0$ plane. You can clearly see that the pattern doesn't blend smoothly.

3d_pattern_repeat

I know that the expression should fail, but am unable to come up with a working expression. How can I make the function repeat itself smoothly like in the 2D example on both x and y axes?

Thanks in advance, Mirac7


EDIT: I've come up with the expression which should calculate height correctly.

$$z=\big(h(x,y,0)\cdot(1-\frac{x}{m})+h(x-m,y,0)\cdot\frac{x}{m}\big)\cdot(1-\frac{y}{n})+\big(h(x,y-n,0)\cdot(1-\frac{x}{m})+h(x-m,y-n,0)\cdot\frac{x}{m}\big)\cdot\frac{y}{n}$$

The result of this expression is this:

new_equation_3d

It's still not quite there. When I increase the $m$ and $n$, thereby gaining more precision from every point, I get smoother transition. I need to set $m,n$ to at least 128 points to get somewhat smooth transition, which is way too many to be calculated per frame.

But another problem occurred. It is barely noticeable in the above rendering, but here's what heightmap of some tiny waves with $m,n=128$ looks like:

heightmap

Can someone please help me figure out why these edges are occurring? Thanks, Mirac7.

1

There are 1 best solutions below

2
On

If your $h(x,y)$ should be periodic with period $m$ in $x$-direction and periodic with period $n$ in $y$-direction choose a finite linear combination of terms of the form $$(x,y)\mapsto \cos{2j\pi x\over m}\cos{2k\pi x\over m}$$ with $j$, $k\in{\mathbb N}$, and similar terms with $\cos(\>)\cos(\>)$ replaced by $\cos(\>)\sin(\>)$, etc.

Experiment with Mathematica or similar to obtain a wave plot of your liking.