Worms-like destructible terrain in Flash – Part 2

After having some troubles with the email, I am finally able to post Jordi Sanglas Molist‘s second step of Worms-like destructible terrain in Flash.

Now the character can jump and move. I’ve also solved a bug: in the last script I checked a collision using the feet, so if the terrain was between both feet the character would fall.

Now, instead of using a hitTest between a BitmapData and a point I use a hitTest between a BitmapData and a Rectangle.

We have to check more collisions, so I used four rectangles:

* The rectangle below the character is used to check if the character must fall

* The rectangle above the character is used to check if the character hit the ground (while he was jumping)

* The rectangles at the sides are used to check if the character can move.

These rectangles aren’t as long as the character, they’re 17 pixels height instead of 20. That’s because, if the character wants to move left and the obstacle isn’t high enough (high enough to reach the rectangle), the character will be able to move. A similar strategy is used in “Create a flash draw game like line rider or others – part 5“, where a point (the knee point) is used to check if the character can move.

I renamed the function “fall”: now it’s “move_character”. I only commented the new lines. Now I’m working on part 3, but I still don’t know how to calculate the explosion impulse. I looked for some information, but now I’m thinking about maths (an explosion is a growing circle).

This is the script:

And this is the result:

Move the player with left – right arrow keys and click on the terrain to create an explosion.

Download the source code. Now Jordi is trying to manage explosion impulse but he’s having some problems. Any blog-bomber reader willing to read?

  • jose

    i’m the first to comment and i think this is a great idea. This original and the weapons made Worms the famous game we now today. I had always asked myself how to now i only thank Jordi Snaglas.

  • Kaustav

    Hey, great tut and code!

    What exactly do you mean by ‘explosion impulse’? I may be able to help =)

    • Jordi Sanglas Molist

      When there’s an explosion near a worm, that one will move away from the centre. But how is this related to the distance?

      I first thought that: the distance from the explosion centre to the worm is inversely proportional to the impulse. But that doesn’t work: the explosion at 1cm from the worm wouldn’t move it at twice the speed that the explosion at 2cm.

      Sorry if there are mistakes, I haven’t revised the text.

  • MC

    @Jordi: you should use an explosion radio… if the enemy is between this radio it will explode… then, you should calculate “how close” is inside the radio (50% distance = half damage… 25% near= 75% damage, and so on… ) there are many ways, this is just one idea

    • Jordi Sanglas Molist

      Well, the problem was the impulse speed; however, your idea can also be applied: if the worm is 25% near, the speed will be 75%…

      Thanks, MC! Part 3 is coming.

  • Jordi Sanglas Molist

    I forgot some parentheses!

    new Sprite ; –> new Sprite();
    new Matrix ; –> new Matrix();

    I don’t know why it works without them.

  • Jesús

    I think that 2d character behaivor would be more easy to implement using box2d for example, what do you think about this?

    • Jordi Sanglas Molist

      If you use Box2d, you’ll be able to do more things. However, as I don’t want to create dynamic boxes (or something similar), I think that “custom physics” are simpler and less CPU consuming.

      But you can use Box2d, if you find it easier (or if you want to add special objects); although I don’t know how to “make holes” in Box2d boxes. Well, I don’t use Box2d.

      I wrote this text really quickly, so forgive me if there are mistakes.

  • Kaustav

    Oh that?? That’s simple. You just use the intensity formula:

    Force = constant/(Distance^2);

    (The constant is the power of the explosion)

    Now, the force is turned into acceleration through:

    Acc = Force/mass

    (mass is weight of character)

    So you just add the acc to the character_speed variable of your character on that frame.

    • Jordi Sanglas Molist

      I think you’re right, but if I wanted to get good results I would have to use that formula every frame (the explosion lasts some miliseconds). Otherwise, it will happen that:

      Explosion 1 pixel away from the character: speed=x;
      Explosion 2 pixels away from the character: speed=x/4;
      And the difference is only one pixel.

      However, I think I’ll use MC’s formula (less real, but enough for a game).

      Thank you, Kaustav. Sometimes I need help. Keep commenting!

  • Jodi Houareau

    nice code Jordi,

    I am trying to do the same thing with movieclips/vectors and am a bit stuck with getting the hittest from a new rectangle

    this is part of my move player function so far…

    if (!ground.hitTestPoint(player.x-6, player.y-30, true)) {

    but it needs to look more like this i think…

    if (!ground.hitTestPoint(0x01,new Rectangle(player.x-6,player.y-10,1,17))) {

    I’ve imported flash.geom.Rectangle. keep getting errors, any help would be appreciated!

    • Jordi Sanglas Molist

      I think that using movieclips you can’t do that kind of hitTest.

      MovieClip can use two hit tests:

      – hitTestPoint: to check a collision between a point and a MovieClip.
      – hitTestObject: to check a collision between two MovieClips (well, between the bounding boxes).

      If you want, you can use a for loop and the hitTestPoint method to check one by one the rectangle points, but maybe it will be much CPU consuming (I don’t know, I haven’t tried it).

  • yura_never_sleeps

    nice tutorial man!
    i know there it is just draft of the game but i m gonna say that there is a little bug:
    when u fall u can press space and the character will make a jump from air.
    i hope u understood my english :D

    • Thank you (I didn’t notice)! I’ll try to solve it in part 3.

      • Jordi Sanglas Molist

        Sorry I pressed ENTER before I finished my comment (and I copied part of my e-mail address to “website”). As I said, I’ll try to solve it in part 3. I think it may be easy: we won’t check if the character is jumping, but we will check if it’s in the air.

        I’ll also add the forgotten parentheses.

  • Pingback: Worms-like destructible terrain in Flash â?? Part 2 – Emanuele Feronato « Worms()

  • Someone

    How would you make platforms (i.e, two levels) of ground, or at least custom shapes? I tried, but it filled in the blank space with a white bitmap…

    • Jordi Sanglas Molist

      You could create a transparent bitmap and use the same function: BitmapData.draw(). Instead of ERASE, the BlendMode would be NORMAL. For example, you can try this code in a *.fla document:

      //Create the bitmap. It must be transparent.
      var terrain_bmpd:BitmapData=new BitmapData(500,500,true,0x00000000);
      var terrain_bmp:Bitmap=new Bitmap(terrain_bmpd);

      //The terrain will be a circle
      var circle:Sprite=new Sprite();

      var circle_matrix:Matrix=new Matrix();


      If you want platforms, you can use rectangles instead of circles.
      You can also use setPixel32() to set individual pixels.

  • Brilliant. I always wanted to make a worms type game. This will be very useful to me. Now just need to sit and pull it apart. Thanks again.

  • simranzenov

    Your collision rectangle should not have a gap. I tested it and got myself stuck on a piece of land. Just thought I would let you know.

  • simranzenov

    Strange I cant seem to duplicate it again. Maybe it was just my browser freezing up. :S

  • lionel

    hey im working with the same thing on flashpunk but am using tilemaps instead of bitmap data, would i beable to get the same kind of detection with tile maps?

  • Tony

    I love you Emanuele!! Thank you so much for posting all of these tutorials. It’s like a one stop learn-gasm for all of my action script programming needs. Keep up the good work!

  • Pingback: Cloudy Pixel Games » Tutorial #1:Pixel Perfect Collision with Flixel Part 1()