Some math and animation

2019-04-14 20:24发布

问题:

I have a grass texture:

I use it in my 2d-game. I want to animate it by code, without any predefined animations.

The grass should interact with wind. So when the wind is stronger, the grass should stoop into need side more.

First version of animation I made using sinusoid function, but such animation is a bit ugly, because the base of the grass moves left/right like all another part of picture. And with sinusoid I'm not able to regulate stoop of the image.

Any advices?

回答1:

This is what you may get easily by shifting pixels:

Although probably not very useful for you, here is a Mathematica program:

f[l_, sh_] := Module[{c = l, k = (Dimensions@l)[[1]]},
  For[i = 1, i <= k, i++,
   c[[i]] = RotateRight[l[[i]], IntegerPart[(k + 1 - i)/sh]]];
  Return[c];]

b = ArrayPad[ImageData@a, {{40}, {40}, {0}}, {1, 1, 1}];
Export["c:\\anim.gif", 
 {Image@b, Image@f[b, 7],
  Image@f[b, 5], Image@f[b, 3],
  Image@f[b, 5], Image@f[b, 7], Image@b}, "DisplayDurations" -> .3]

Edit

But you can get a better effect by applying a circular transform:

b = Image@ArrayPad[ImageData@a, {{40, 0}, {40}, {0}}, {1, 1, 1}];
f[image_, angleMult_] := ImageForwardTransformation[image, (
    fi = ArcTan[Abs[#[[2]]/(#[[1]] - .5)]];
    fi1 = angleMult fi (#[[1]]^2 + #[[2]]^2)/2;
    {(1/2 - Sin[fi1] #[[2]] - Cos[fi1]/2 + 
       Cos[fi1] #[[1]]), -Sin[fi1]/2 + Sin[fi1] #[[1]] + 
      Cos[fi1] #[[2]]}) &]
t = Table[f[b, x], {x, 0, .2, .02}];
t1 = Reverse@t;
Export["c:\\anim.gif", Join[t, t1], "DisplayDurations" -> .15];
Import["c:\\anim.gif", "Animation"]


回答2:

You could just shift the rows, so that e.g. every 3rd row is shifted 1px to the right, beginning with the bottom.



回答3:

How are you displaying the texture? When using a billboard you could manipulate the vertices of the billboard and even triangulate the billboard for more control.