3
\$\begingroup\$

I'm trying to learn how to code some "effects" more succinctly.

For example, I have sprite clouds in my game, and their transparency is determined with the following formula on their update-method, which is called repeatedly.

float transp = Math.abs(MathUtils.sin(0.2f * time) * 0.4f);

This works OK. However, let's say, that when the transparency reaches < 0.2, I want it to stay there, for some time, and so that the duration is randomized.

I could of course hack this with some "ugly" variables or something, but like I said, I'm trying to learn how to do stuff like this more to the point, hopefully with one-or-few-liners. In other words, more elegantly, more mathematically. Maybe more functionally would be the right description.

Perhaps some demoscene code could be a good starting point for learning things like this.

Anyone have any code or good examples or references to articles of such "dirty math tricks" (or this particular case)?

\$\endgroup\$
  • 1
    \$\begingroup\$ 1D perlin noise is a good replacement for sin() when you want something more random-looking. \$\endgroup\$ – bcrist May 21 '14 at 4:37
  • \$\begingroup\$ I would avoid doing this, personally, unless profiling has demonstrated a serious need to optimize these individual lines (and even then, succinctness is not necessarily a measure of performance). This kind of thing generally degrades the readability of code. \$\endgroup\$ – user1430 May 21 '14 at 15:31