Clev3r
Clev3r

Reputation: 1578

Animate sprite programmatically

I'm trying to think of a method to 'sort of programmatically' animate sprites in a game. The purpose of this is to avoid creating ~20-50 frames of each sprite (very time consuming), then running a loop to sequence the frames, thus producing the desired animation. Below is one example of a sprite I would like to animate. The desired animation should give 'waves' to the image, originating in the center and expanding outward. This will make the water color look alive, or as if it's still wet and shimmering. I'm using Cocos2D so each sprite has these methods available already: move, rotate, scale. My initial thought is to use Cocos2D's particle system to try and create this effect, but I worry about the efficiency of running the system for perhaps 50 or so sprites. Is there a better way?

'play game button'

Upvotes: 0

Views: 367

Answers (3)

Jack Ward
Jack Ward

Reputation: 1

You might want to research normal mapping techniques to get the "shimmer" effect you want. Basically you would find or generate a normal map of a "water ripple" texture that scales up every frame so it appears to be expanding, then sample that into a lighting system, and finally tweak the amount of light reflection, refraction, etc until you get the effect you want. I learned how to do this from this video and his fast 2D lighting series. I'm not sure if there's any tutorials for Cocos2D, but Gamemaker uses OpenGl, so as long as you're using that, the shader code should be roughly the same.

Now if you still want additional distortion on the puddle so the sprite itself is actually warping, you could do a similar technique but rather than sampling a normal map, you can sample a height map of the ripple texture and distort the UV coordinates you sample from based on the alpha value of the height map.

If you don't want to do that then you can write a simple sinusoidal distortion shader and tweak it a bit to fit your needs (for example you could change the distortion direction and amount based on the pixel's position relative to the center of the sprite). Again, you might need to do some research on how to implement this into Cocos2D since I'm not very familiar with it.

Hopefully that helps or at least points you in the right direction!

Upvotes: 0

YvesLeBorg
YvesLeBorg

Reputation: 9079

What you want to achieve is probably best done with custom vertex/fragment shader. Unfortunately not many examples floating around, so you have to look. But to get you started, look here, there might be one there close to your desires. Beware, shaders can be tricky and are hardly the kind of 'drop from open source repository' to your codebase type of software. Some tweaking required, your mileage will vary.

for a crash 'intro' on openGL , shaders and stuff, if you are inclined, this blog was very helpful to me. Follow the links.

Upvotes: 2

dqhendricks
dqhendricks

Reputation: 19251

A particle system would probably be pretty rough on performance if you have a large number of them.

Most people use pre-created water tiles that animate. So each tile would have 4 frame of animation or whatever, and on creation, you would set each one to animate through those 4 frames repeatedly.

You may also want to look into the CCRipple3D action.

Upvotes: 0

Related Questions