一些数学和动画 [英] Some math and animation

查看:181
本文介绍了一些数学和动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个草质地:

我用它在我的2D游戏。我想通过code动画它,没有任何predefined动画。

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.

有何意见?

推荐答案

这是你可以通过改变像素得到什么​​轻松:

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]

修改

但你可以通过应用获得更好的效果循环变换:

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

这篇关于一些数学和动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆