开发者

Some math and animation

开发者 https://www.devze.com 2023-02-04 19:35 出处:网络
I have a grass texture: I use it in my 2d-game. I want to animate it by code, without any predefined animations.

I have a grass texture:

Some math and animation

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 ab开发者_JAVA技巧le to regulate stoop of the image.

Some math and animation

Any advices?


This is what you may get easily by shifting pixels:

Some math and animation

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:

Some math and animation

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"]


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号