cocos2d - how to draw a bottle sprite with dynamically changing water level
Posted
by
Oliver
on Stack Overflow
See other posts from Stack Overflow
or by Oliver
Published on 2012-10-14T14:17:18Z
Indexed on
2012/10/14
15:37 UTC
Read the original article
Hit count: 168
I am trying to draw a (2d) sprite in cocos2d showing a bottle. The bottle shall be able to have a dynamic water level (i.e. the amount of water in the bottle can change over the lifetime of the sprite).
I am wondering how to do this. I currently have a PNG file of the empty bottle. I adjusted the alpha channel of that PNG so when rendering the sprite I can draw a blue rectangle and render the bottle texture over it. That will give the impression of the water being inside the bottle.
However, the bottle's shape is not a rectangle itself of course, so the water can be seen out of the bounds of the bottle. I can change the bottle image in a way that only the bottle itself is transparent and set the "outside world" to an opaque color & alpha channel value, but that again prevents the "world background" to be visible in that area.
I simply don't have a clue how to realize this in a sane manner. Do I really have to read every pixel of the bottle image, identify which pixel is "inside" of the bottle and then draw the water pixel by pixel? There must be an easier way, right? ;)
Any best practices for these kinds of tasks?
edit: see picture below, to make somewhat clearer, what I am talking about ;)
© Stack Overflow or respective owner