Some math and animation

2019-04-14 20:56发布

I have a grass texture:

alt text

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.

alt text

Any advices?

3条回答
淡お忘
2楼-- · 2019-04-14 21:00

This is what you may get easily by shifting pixels:

alt text

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:

alt text

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"]
查看更多
再贱就再见
3楼-- · 2019-04-14 21:04

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.

查看更多
太酷不给撩
4楼-- · 2019-04-14 21:16

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

查看更多
登录 后发表回答