A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: Exploding bitmap

  1. #1
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223

    Exploding bitmap

    I want to make bitmaps explode, but not by creating separate explosion graphics, instead I want to use the original picture and make it blow up into pieces, like this:
    http://www.tonypa.pri.ee/test/explode.html

    What you think? Anyone done something like that or has ideas how to improve it?

  2. #2
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506

  3. #3
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    maybe improve it using the Voronoi diagram?, also known as the celluar filter in photoshop or as shader in 3dsmax
    like this:

    some links I found with google:
    http://www.donhavey.com/blog/tutoria...onoi-diagrams/
    http://www.righto.com/fractals/vor.html

    but I am sure you´ll find more as you search yourself

    but that demo is already pretty cool, perhaps add some glim, sparks or noise effects (wich can be unique sprites again) so that it looks more like breaking.

    Also perhaps add random dirt maps, a common method in the 3d industry is using so called cavity (convex/concarve) or ambient occlusion maps to fake dirt and altering of objects. If you apply that on 2d it could for instance mean a slight inside glow of brown or black with blending mode set to multiply.


    another cool idea could be to clamp chunks together instead of exploding thus imploded shifting into each other. Like hitting metal and watching how it gets more and more compact.
    If you then use tesseltation of triangles it could look really great

  4. #4
    Senior Member Sietjp's Avatar
    Join Date
    Jan 2005
    Location
    Paris, France
    Posts
    711
    In your example link tony, how is it done ?
    In my latest games, I've maybe made something you're looking for. I have a movie clip, I split it into several piece of bitmaps and I explode the bitmaps.

  5. #5
    Senior Member fil_razorback's Avatar
    Join Date
    Jul 2005
    Location
    Paris, France
    Posts
    607
    I did that some time ago for my TRPG. I think that's about what you're looking for, you just have to make a variation of it that fits your needs
    http://fil.razorback.free.fr/tmp/FK/damageTest.html
    http://fil.razorback.free.fr/tmp/FK/damageTest.fla

  6. #6
    Senior Member
    Join Date
    Mar 2008
    Posts
    301
    I know that this is easier said than done, but I think some rotation on the little pieces would make it look a lot nicer. How exactly you would accomplish this, I don't know. Obviously you could just "draw" them rotated, but that's going to slow things down. Is it possible that you could prerender rotated shrapnel shaped pieces, then do a bitmapfill on to them? I wonder if that would be faster.

    I have rotating shrapnel in this demo but it's all solid colored. Maybe you could just approximate the original bitmap by drawing small solid colored shrapnel of appropriate color.

    EDIT: forgot to link the demo I mentioned - http://www.wesisgood.com/follow/index.html

    Or you could just have each piece as a separate bitmap and just rotate them in the same way you would rotate anything else. Probably a lot more efficient than all of the other crazy things I suggested.
    Last edited by WesIsGood; 06-20-2008 at 03:16 PM.

  7. #7
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Did I mention it should of course run fast? Since its only for the eye-candy, most of the power is needed elsewhere. There will be many other elements flying around same time.

    renderhjs, that looks interesting but too complicated.

    I am simply using 1 bitmap and copyPixel to put pieces there, so cant handle rotations. After some time it breaks the bitmap into 4 smaller bitmaps.

  8. #8
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    you could do some pre-rendering if you wanted to save cpu.

  9. #9
    Senior Member
    Join Date
    Mar 2008
    Posts
    301
    Yeah, I love pre-rendering. If you're not exploding anything that's being created or deformed dynamically, I don't see what it would hurt. I'm only suggesting rotation because to me without rotation it looks more like it is being sliced up methodically than being exploded.

    EDIT: It looks like you have changed it a bit since I last looked. Now things are exploding outward instead of completely randomly. That's actually something I considered suggesting, and I think it definitely looks better now.
    Last edited by WesIsGood; 06-20-2008 at 06:59 PM.

  10. #10
    Elvis...who tha f**k is Elvis? phreax's Avatar
    Join Date
    Feb 2001
    Posts
    1,836
    Tony, I actually think that you should stick to what you've got, if it's fast enough! I think it looks great as it is
    Streets Of Poker - Heads-Up Texas Hold'em Poker Game

  11. #11
    ....he's amazing!!! lesli_felix's Avatar
    Join Date
    Nov 2000
    Location
    London UK
    Posts
    1,506
    That looks better now.

    I would add some kind of particle trail to those pieces, otherwise it looks like a shattering instead of an explosion.

  12. #12
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Thanks, guys

    replaced it back to simple example for now
    Last edited by tonypa; 06-22-2008 at 02:23 PM.

  13. #13
    Senior Member Sietjp's Avatar
    Join Date
    Jan 2005
    Location
    Paris, France
    Posts
    711
    That is very fast for me 30% of CPU on the game field.

  14. #14
    Zombie Coder EvilKris's Avatar
    Join Date
    Jun 2006
    Location
    Fukuoka, Japan.
    Posts
    796
    Hmm.. I wouldn't mind some of that for one of my games. any chance of some open-source action there Tony?

  15. #15
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Sure EvilKris, here is what i used:

    Setting it up
    PHP Code:
    //the array to hold all the pieces
    var explArr:Array = new Array();
    //bitmap for explosion, size of the screen
    var explBmp:Bitmap = new Bitmap(new BitmapData(400400true0));
    this.addChild (explBmp);
    //example object to be blown up
    var ob:Object = new Object();
    //use bitmap with linkage name ship_bmd
    ob.bmp = new Bitmap(new ship_bmd(00));
    ob.bmp.200;
    ob.bmp.200;
    this.addChild (ob.bmp);
    //listeners
    this.stage.addEventListener (MouseEvent.CLICKmyClickfalse0true);
    this.addEventListener (Event.ENTER_FRAMEdoExplodefalse0true);

    //user clicked, explode
    function myClick (ev:MouseEvent):void {
        
    //hide the original bitmap
        
    if(explArr.length == 0){
            
    //only hide it if the explosion was not already running
            
    this.removeChild(ob.bmp);
        }
        
    startExplode (ob.bmp);

    Begin explosion
    PHP Code:
    //starts the explosion with bitmap b
    function startExplode (b:Bitmap):void {
        
    //clear the array, dont clear if you want to have multiple explosions
        
    explArr = new Array();
        
    //first object to be inserted into array, its full bitmap
        
    var curOb:Object = new Object();
        
    //get bitmapdata
        
    curOb.bmd b.bitmapData;
        
    //position
        
    curOb.b.x;
        
    curOb.b.y;
        
    //movement
        
    curOb.xd 0;
        
    curOb.yd 0;
        
    //lifetime
        
    curOb.0;
        
    //put into array
        
    explArr.push(curOb);

    Run enterframe event
    PHP Code:
    //runs explosion
    function doExplode (e:Event):void {
        
    //check if array is empty
        
    if(explArr.length 0){
            
    //start manipulating the bitmap
            
    explBmp.bitmapData.lock();
            
    //clear the bitmap
            
    explBmp.bitmapData.fillRect(new Rectangle(00400400), 0);
            
    //loop the array
            
    for(var n:int 0explArr.lengthn++){
                
    //get first piece in the array
                
    var curOb:Object explArr.shift();
                
    //move the piece, 4 is speed value
                
    curOb.+= curOb.xd/4;
                
    curOb.+= curOb.yd/4;
                
    //copy this piece into main bitmap
                
    var r:Rectangle = new Rectangle(00curOb.bmd.widthcurOb.bmd.height);
                
    explBmp.bitmapData.copyPixels(curOb.bmdr, new Point(curOb.xcurOb.y));
                
    //check if its time is up
                
    if(curOb.<= 0){
                    
    //break it up if its at least 3 pixels
                    //you could break up 2 pixels too but it wont change much
                    
    if(curOb.bmd.width 2){
                        
    //create 4 new pieces
                        
    breakOb (curOb00);
                        
    breakOb (curOb10);
                        
    breakOb (curOb01);
                        
    breakOb (curOb11);
                    }
                }else{
                    
    //reduce life
                    
    curOb.t--;
                    
    //its still alive, add back into array
                    
    explArr.push(curOb);
                }
            }
            
    //array is empty
            
    if(explArr.length == 0){
                
    //clear the bitmap
                
    explBmp.bitmapData.fillRect(new Rectangle(00400400), 0);
                
    //re-place object
                
    this.addChild (ob.bmp);
                
    ob.bmp.40 Math.random() * 320;
                
    ob.bmp.40 Math.random() * 320;
            }
            
    //finished manipulating the bitmap
            
    explBmp.bitmapData.unlock();
        }

    Breaking up
    PHP Code:
    //creates smaller 1/4 piece of bitmap
    function breakOb (curOb:Objectxd:Numberyd:Number):void {
        var 
    newOb:Object = new Object();
        
    //half the size of old piece
        
    var s:Number curOb.bmd.width/2;
        
    //create bitmapdata
        
    newOb.bmd = new BitmapData(sstrue0);
        var 
    r:Rectangle = new Rectangle(xd syd sss);
        
    //copy part of old bitmap into new piece
        
    newOb.bmd.copyPixels(curOb.bmdr, new Point(00));
        
    //check for empty bitmap
        
    newOb.bmd.getColorBoundsRect (0xFF0000000x00000000false);
        
    //find if it contains any non-transparent pixels
        
    if(r.width || r.height 1){
            
    //place new piece
            
    newOb.curOb.xd s;
            
    newOb.curOb.yd s;
            
    //check if this was first division from original bitmap image
            
    if(curOb.xd == && curOb.yd == 0){
                
    //make it move apart from origin
                
    var xm:Number newOb.curOb.s/2;
                var 
    ym:Number newOb.curOb.s/2;
                
    //add some randomness
                
    xm xm xm * (Math.random() * 1) * 1;
                
    ym ym ym * (Math.random() * 1) * 1;
            }else{
                
    //completely random movement
                
    xm Math.random() - 0.5;
                
    ym Math.random() - 0.5;
            }
            
    //movement speed depends on the size, change 30 / (s * s) for other results
            
    var tm:Number Math.sqrt(xm xm ym ym) * (30 / (s));
            
    //save movement, getting half of the movement from old piece
            
    newOb.xd xm/tm curOb.xd/2;
            
    newOb.yd ym/tm curOb.yd/2;
            
    //lifetime, play with values here for different results
            
    newOb.10 Math.random() * 30 1.5;
            
    //add to array
            
    explArr.push(newOb);
        }

    Notice that original bitmap should not be rotated, and it works better on the square images.
    Attached Files Attached Files

  16. #16
    Elvis...who tha f**k is Elvis? phreax's Avatar
    Join Date
    Feb 2001
    Posts
    1,836
    Thanks for sharing, Tony! Your example looks great! Runs great on my trusty old laptop (1,66GHz, 2Gb RAM). You could add an fps counter if you want more solid stats! Doesn't seem to slow down though, and the look and feel is awesome!
    Streets Of Poker - Heads-Up Texas Hold'em Poker Game

  17. #17
    Zombie Coder EvilKris's Avatar
    Join Date
    Jun 2006
    Location
    Fukuoka, Japan.
    Posts
    796
    You're the man Tony. You should post things like that on your site for posterity. If you don't I might plop it on mine, with full credit given to you of course. Just good to have snippets like that put somewhere they can be read, instead of rotting away on some internet forum.

  18. #18
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Feel free to use and modify it any way you like

  19. #19
    Senior Member Sietjp's Avatar
    Join Date
    Jan 2005
    Location
    Paris, France
    Posts
    711
    I've just played a game with wonderful explosions, the demo animation can give you interesting ideas.

    http://www.alphabounce.com/

  20. #20

    Lightbulb

    I know this thread is ancient, but I'm looking to do something somewhat similar and was wondering if anyone has seen anything like this:

    • 1 - copy a movieclip on stage into a bitmap and hide it
    • 2 - slice it up into 2x2 blocks (like this)
    • 3 - send the parts flying off in different directions based on their relation to the center of the original image - determine angle of new part from center of original image then calculate starting x and y speeds (like this with rotation removed)
    • 4 - apply gravity
    • 5 - BOOM!


    Before I struggle to marry all of these techniques together, I figured I'd ask if anyone out there has already done this

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center